Spaces:
Runtime error
Runtime error
loztcontrol
commited on
Commit
•
e44dedb
1
Parent(s):
b772702
Updating app.py and txt
Browse files- app.py +46 -136
- requirements.txt +2 -4
app.py
CHANGED
@@ -1,142 +1,52 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
-
import random
|
4 |
-
#import spaces #[uncomment to use ZeroGPU]
|
5 |
-
from diffusers import DiffusionPipeline
|
6 |
import torch
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
negative_prompt = negative_prompt,
|
33 |
-
guidance_scale = guidance_scale,
|
34 |
-
num_inference_steps = num_inference_steps,
|
35 |
-
width = width,
|
36 |
-
height = height,
|
37 |
-
generator = generator
|
38 |
-
).images[0]
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
examples = [
|
43 |
-
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
44 |
-
"An astronaut riding a green horse",
|
45 |
-
"A delicious ceviche cheesecake slice",
|
46 |
-
]
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
margin: 0 auto;
|
51 |
-
max-width: 640px;
|
52 |
-
}
|
53 |
-
"""
|
54 |
-
|
55 |
-
with gr.Blocks(css=css) as demo:
|
56 |
-
|
57 |
-
with gr.Column(elem_id="col-container"):
|
58 |
-
gr.Markdown(f"""
|
59 |
-
# Text-to-Image Gradio Template
|
60 |
-
""")
|
61 |
-
|
62 |
-
with gr.Row():
|
63 |
-
|
64 |
-
prompt = gr.Text(
|
65 |
-
label="Prompt",
|
66 |
-
show_label=False,
|
67 |
-
max_lines=1,
|
68 |
-
placeholder="Enter your prompt",
|
69 |
-
container=False,
|
70 |
-
)
|
71 |
-
|
72 |
-
run_button = gr.Button("Run", scale=0)
|
73 |
-
|
74 |
-
result = gr.Image(label="Result", show_label=False)
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
negative_prompt = gr.Text(
|
79 |
-
label="Negative prompt",
|
80 |
-
max_lines=1,
|
81 |
-
placeholder="Enter a negative prompt",
|
82 |
-
visible=False,
|
83 |
-
)
|
84 |
-
|
85 |
-
seed = gr.Slider(
|
86 |
-
label="Seed",
|
87 |
-
minimum=0,
|
88 |
-
maximum=MAX_SEED,
|
89 |
-
step=1,
|
90 |
-
value=0,
|
91 |
-
)
|
92 |
-
|
93 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
94 |
-
|
95 |
-
with gr.Row():
|
96 |
-
|
97 |
-
width = gr.Slider(
|
98 |
-
label="Width",
|
99 |
-
minimum=256,
|
100 |
-
maximum=MAX_IMAGE_SIZE,
|
101 |
-
step=32,
|
102 |
-
value=1024, #Replace with defaults that work for your model
|
103 |
-
)
|
104 |
-
|
105 |
-
height = gr.Slider(
|
106 |
-
label="Height",
|
107 |
-
minimum=256,
|
108 |
-
maximum=MAX_IMAGE_SIZE,
|
109 |
-
step=32,
|
110 |
-
value=1024, #Replace with defaults that work for your model
|
111 |
-
)
|
112 |
-
|
113 |
-
with gr.Row():
|
114 |
-
|
115 |
-
guidance_scale = gr.Slider(
|
116 |
-
label="Guidance scale",
|
117 |
-
minimum=0.0,
|
118 |
-
maximum=10.0,
|
119 |
-
step=0.1,
|
120 |
-
value=0.0, #Replace with defaults that work for your model
|
121 |
-
)
|
122 |
-
|
123 |
-
num_inference_steps = gr.Slider(
|
124 |
-
label="Number of inference steps",
|
125 |
-
minimum=1,
|
126 |
-
maximum=50,
|
127 |
-
step=1,
|
128 |
-
value=2, #Replace with defaults that work for your model
|
129 |
-
)
|
130 |
-
|
131 |
-
gr.Examples(
|
132 |
-
examples = examples,
|
133 |
-
inputs = [prompt]
|
134 |
-
)
|
135 |
-
gr.on(
|
136 |
-
triggers=[run_button.click, prompt.submit],
|
137 |
-
fn = infer,
|
138 |
-
inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
139 |
-
outputs = [result, seed]
|
140 |
-
)
|
141 |
|
142 |
-
demo.queue().launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from diffusers import StableDiffusionPipeline
|
4 |
+
|
5 |
+
# Cargar el modelo Stable Diffusion
|
6 |
+
model_id = "stabilityai/stable-diffusion-2-1"
|
7 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id)
|
8 |
+
pipe.to("cuda") # Si estás usando GPU
|
9 |
+
|
10 |
+
# Función para generar imágenes
|
11 |
+
def generate_image(prompt, style, width, height, guidance_scale, steps, seed):
|
12 |
+
# Configurar el generador de semillas
|
13 |
+
generator = torch.manual_seed(seed) if seed is not None else None
|
14 |
+
|
15 |
+
# Ajustar el estilo en función del input del usuario
|
16 |
+
if style == "Realismo":
|
17 |
+
prompt += ", realistic, high detail, photorealistic"
|
18 |
+
elif style == "Arte digital":
|
19 |
+
prompt += ", digital art, sharp details, vibrant colors"
|
20 |
+
elif style == "Ciencia Ficción":
|
21 |
+
prompt += ", sci-fi, futuristic, high-tech"
|
22 |
+
elif style == "Surrealismo":
|
23 |
+
prompt += ", surreal, dream-like, abstract"
|
24 |
+
|
25 |
+
# Generar la imagen con Stable Diffusion
|
26 |
+
image = pipe(prompt, height=height, width=width, guidance_scale=guidance_scale,
|
27 |
+
num_inference_steps=steps, generator=generator).images[0]
|
28 |
+
return image
|
29 |
+
|
30 |
+
# Configuración de la interfaz de Gradio
|
31 |
+
with gr.Blocks() as demo:
|
32 |
+
gr.Markdown("# Generador de Imágenes estilo MidJourney")
|
33 |
+
prompt = gr.Textbox(label="Descripción (Prompt)", placeholder="Describe la imagen...")
|
34 |
+
style = gr.Dropdown(choices=["Realismo", "Arte digital", "Ciencia Ficción", "Surrealismo"], label="Estilo", value="Realismo")
|
35 |
+
width = gr.Slider(512, 1024, step=64, label="Ancho", value=768)
|
36 |
+
height = gr.Slider(512, 1024, step=64, label="Altura", value=768)
|
37 |
+
guidance_scale = gr.Slider(5, 20, step=0.5, label="Nivel de guía del prompt", value=7.5)
|
38 |
+
steps = gr.Slider(10, 100, step=5, label="Número de pasos de inferencia", value=50)
|
39 |
+
seed = gr.Number(label="Semilla (dejar vacío para aleatorio)", value=None, optional=True)
|
40 |
|
41 |
+
# Botón para generar la imagen
|
42 |
+
btn = gr.Button("Generar imagen")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
# Salida de la imagen generada
|
45 |
+
output = gr.Image(label="Imagen generada")
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
# Vincular los inputs y outputs
|
48 |
+
btn.click(generate_image, inputs=[prompt, style, width, height, guidance_scale, steps, seed], outputs=output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
# Lanzar la aplicación Gradio
|
51 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
|
requirements.txt
CHANGED
@@ -1,6 +1,4 @@
|
|
1 |
-
|
2 |
diffusers
|
3 |
-
invisible_watermark
|
4 |
-
torch
|
5 |
transformers
|
6 |
-
|
|
|
1 |
+
torch>=1.9.0
|
2 |
diffusers
|
|
|
|
|
3 |
transformers
|
4 |
+
gradio
|