Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
css = """
|
2 |
#app-container {
|
3 |
max-width: 100%;
|
4 |
margin: 0 auto;
|
5 |
padding: 20px;
|
6 |
-
overflow-y: auto; /* Asegura la barra de desplazamiento vertical si el contenido es demasiado alto */
|
7 |
-
height: 100vh; /* Utiliza el 100% de la altura de la ventana de visualización */
|
8 |
-
box-sizing: border-box; /* Incluye el padding y el borde en el tamaño total */
|
9 |
}
|
10 |
|
11 |
input, textarea, select {
|
@@ -69,10 +134,6 @@ h1, h2, h3, h4, h5, h6 {
|
|
69 |
width: auto;
|
70 |
margin-top: 10px;
|
71 |
}
|
72 |
-
|
73 |
-
#reset-button {
|
74 |
-
display: none; /* Oculta el botón de reinicio en pantallas móviles */
|
75 |
-
}
|
76 |
}
|
77 |
|
78 |
@media (min-width: 769px) {
|
@@ -86,3 +147,70 @@ h1, h2, h3, h4, h5, h6 {
|
|
86 |
}
|
87 |
}
|
88 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import io
|
4 |
+
import random
|
5 |
+
import os
|
6 |
+
from PIL import Image
|
7 |
+
from deep_translator import GoogleTranslator
|
8 |
+
|
9 |
+
# Project by Nymbo
|
10 |
+
|
11 |
+
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
12 |
+
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
13 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
14 |
+
timeout = 100
|
15 |
+
|
16 |
+
def query(prompt, is_negative=False, steps=30, cfg_scale=7, strength=0.7):
|
17 |
+
if not prompt:
|
18 |
+
return None
|
19 |
+
|
20 |
+
key = random.randint(0, 999)
|
21 |
+
|
22 |
+
# Detectar el idioma del prompt y traducirlo al inglés
|
23 |
+
translator = GoogleTranslator(target='en')
|
24 |
+
try:
|
25 |
+
prompt = translator.translate(prompt)
|
26 |
+
except Exception as e:
|
27 |
+
print(f"Error during translation: {e}")
|
28 |
+
return None
|
29 |
+
|
30 |
+
print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
|
31 |
+
|
32 |
+
prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
33 |
+
print(f'\033[1mGeneration {key}:\033[0m {prompt}')
|
34 |
+
|
35 |
+
payload = {
|
36 |
+
"inputs": prompt,
|
37 |
+
"is_negative": is_negative,
|
38 |
+
"steps": steps,
|
39 |
+
"cfg_scale": cfg_scale,
|
40 |
+
"strength": strength
|
41 |
+
}
|
42 |
+
|
43 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout)
|
44 |
+
if response.status_code != 200:
|
45 |
+
print(f"Error: Failed to get image. Response status: {response.status_code}")
|
46 |
+
print(f"Response content: {response.text}")
|
47 |
+
if response.status_code == 503:
|
48 |
+
raise gr.Error(f"{response.status_code} : The model is being loaded")
|
49 |
+
raise gr.Error(f"{response.status_code}")
|
50 |
+
|
51 |
+
try:
|
52 |
+
image_bytes = response.content
|
53 |
+
image = Image.open(io.BytesIO(image_bytes))
|
54 |
+
print(f'\033[1mGeneration {key} completed!\033[0m ({prompt})')
|
55 |
+
return image
|
56 |
+
except Exception as e:
|
57 |
+
print(f"Error when trying to open the image: {e}")
|
58 |
+
return None
|
59 |
+
|
60 |
+
def reset_prompt():
|
61 |
+
default_text_prompt = "" # Valor por defecto para el campo de texto
|
62 |
+
default_negative_prompt = "(deformed, distorted, disfigured), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, misspellings, typos" # Valor por defecto para el campo de texto negativo
|
63 |
+
default_steps = 35 # Valor por defecto para el slider de pasos de muestreo
|
64 |
+
default_cfg = 7 # Valor por defecto para el slider de nivel de detalle
|
65 |
+
default_strength = 0.7 # Valor por defecto para el slider de fuerza de transformación
|
66 |
+
|
67 |
+
return default_text_prompt, default_negative_prompt, default_steps, default_cfg, default_strength
|
68 |
+
|
69 |
css = """
|
70 |
#app-container {
|
71 |
max-width: 100%;
|
72 |
margin: 0 auto;
|
73 |
padding: 20px;
|
|
|
|
|
|
|
74 |
}
|
75 |
|
76 |
input, textarea, select {
|
|
|
134 |
width: auto;
|
135 |
margin-top: 10px;
|
136 |
}
|
|
|
|
|
|
|
|
|
137 |
}
|
138 |
|
139 |
@media (min-width: 769px) {
|
|
|
147 |
}
|
148 |
}
|
149 |
"""
|
150 |
+
|
151 |
+
examples = [
|
152 |
+
"Minions con gafas atrapados en un bloque de hielo transparente, mostrando expresiones de sorpresa. Hielo escarchado y agrietado que refleja luz azul. Copos de nieve caen suavemente en un paisaje invernal.",
|
153 |
+
"Un caballo marrón dorado trotando alegremente en realismo 8K contra un cielo vibrante con nubes multicolores en azul, rosa y naranja.",
|
154 |
+
"Casco de Kylo Ren en forma de gato con texturas rugosas. Renderizado 3D ultra detallado con iluminación de borde y texturas hiperrealistas. Tonos pulidos según los colores de Kylo Ren, renderizado octano en 8K, fondo contrastado."
|
155 |
+
]
|
156 |
+
|
157 |
+
with gr.Blocks(css=css) as app:
|
158 |
+
gr.HTML("""
|
159 |
+
<center>
|
160 |
+
<h1>Dream Generator with Flux</h1>
|
161 |
+
<h2>Transforma tus sueños en imágenes vibrantes con un solo clic.</h2>
|
162 |
+
</center>
|
163 |
+
""")
|
164 |
+
|
165 |
+
with gr.Row():
|
166 |
+
with gr.Column(scale=1):
|
167 |
+
text_prompt = gr.Textbox(
|
168 |
+
label="¿Cuál fue tu sueño?",
|
169 |
+
placeholder="Describe lo que soñaste, no omitas ningún detalle.",
|
170 |
+
lines=2,
|
171 |
+
elem_id="prompt-text-input"
|
172 |
+
)
|
173 |
+
|
174 |
+
with gr.Accordion("Opciones Avanzadas", open=False):
|
175 |
+
negative_prompt = gr.Textbox(
|
176 |
+
label="¿Qué elementos no deseas que se plasmen de tu sueño?",
|
177 |
+
placeholder="Describe en detalle lo que quieres que aparezca.",
|
178 |
+
value="(deformed, distorted, disfigured), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, misspellings, typos",
|
179 |
+
lines=3,
|
180 |
+
elem_id="negative-prompt-text-input"
|
181 |
+
)
|
182 |
+
steps = gr.Slider(
|
183 |
+
label="Profundidad del sueño (Pasos de muestreo)",
|
184 |
+
value=35,
|
185 |
+
minimum=1,
|
186 |
+
maximum=100,
|
187 |
+
step=1
|
188 |
+
)
|
189 |
+
cfg = gr.Slider(
|
190 |
+
label="Claridad del sueño (Nivel de detalle)",
|
191 |
+
value=7,
|
192 |
+
minimum=1,
|
193 |
+
maximum=20,
|
194 |
+
step=1
|
195 |
+
)
|
196 |
+
strength = gr.Slider(
|
197 |
+
label="Intensidad del sueño (Fuerza de transformación)",
|
198 |
+
value=0.7,
|
199 |
+
minimum=0,
|
200 |
+
maximum=1,
|
201 |
+
step=0.001
|
202 |
+
)
|
203 |
+
|
204 |
+
gr.Examples(examples=examples, inputs=text_prompt, label="Ejemplos de otros sueños")
|
205 |
+
|
206 |
+
with gr.Row(elem_id="button-container"):
|
207 |
+
generate_button = gr.Button("QUIERO VER MI SUEÑO", elem_id="generate-button", variant="primary")
|
208 |
+
reset_button = gr.Button("REINICIAR MI SUEÑO", elem_id="reset-button", variant="secondary")
|
209 |
+
|
210 |
+
with gr.Column(scale=1):
|
211 |
+
image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
|
212 |
+
|
213 |
+
generate_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, strength], outputs=image_output)
|
214 |
+
reset_button.click(reset_prompt, outputs=[text_prompt, negative_prompt, steps, cfg, strength])
|
215 |
+
|
216 |
+
app.launch(show_api=False, share=False)
|