JeCabrera commited on
Commit
859dc85
·
verified ·
1 Parent(s): 357533f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -127
app.py CHANGED
@@ -1,62 +1,3 @@
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
  css = """
61
  #app-container {
62
  max-width: 100%;
@@ -145,71 +86,3 @@ h1, h2, h3, h4, h5, h6 {
145
  }
146
  }
147
  """
148
-
149
- examples = [
150
- "Minions con gafas atrapados en un bloque de hielo transparente, mostrando expresiones sorprendidas. Hielo escarchado y agrietado refleja luz azul. Copos de nieve caen suavemente en un paisaje invernal.",
151
- "Un caballo castaño dorado trotando alegremente en realismo 8K contra un cielo vibrante con nubes multicolores en azul, rosa y naranja.",
152
- "El casco de Kylo Ren en forma de gato con texturas rugosas. Renderizado 3D ultra detallado con iluminación de borde y texturas hiper-realistas. Tonos pulidos de los colores de Kylo Ren, renderizado en 8K con fondo contrastante."
153
- ]
154
-
155
- with gr.Blocks(css=css) as app:
156
- gr.HTML("""
157
- <center>
158
- <h1>Generador de Sueños con Flux</h1>
159
- <h2>Transforma tus sueños en imágenes vibrantes con un solo clic.</h2>
160
- </center>
161
- """)
162
-
163
- with gr.Row():
164
- with gr.Column(scale=1):
165
- text_prompt = gr.Textbox(
166
- label="¿Cuál fue tu sueño?",
167
- placeholder="Describe lo que soñaste, no omitas ningún detalle.",
168
- lines=2,
169
- elem_id="prompt-text-input"
170
- )
171
-
172
- with gr.Accordion("Opciones Avanzadas", open=False):
173
- negative_prompt = gr.Textbox(
174
- label="¿Qué elementos no deseas que se plasmen de tu sueño?",
175
- placeholder="Describe en detalle lo que quieres que aparezca.",
176
- 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",
177
- lines=3,
178
- elem_id="negative-prompt-text-input"
179
- )
180
- steps = gr.Slider(
181
- label="Profundidad del sueño (Pasos de muestreo)",
182
- value=35,
183
- minimum=1,
184
- maximum=100,
185
- step=1
186
- )
187
- cfg = gr.Slider(
188
- label="Claridad del sueño (Nivel de detalle)",
189
- value=7,
190
- minimum=1,
191
- maximum=20,
192
- step=1
193
- )
194
- strength = gr.Slider(
195
- label="Intensidad del sueño (Fuerza de transformación)",
196
- value=0.7,
197
- minimum=0,
198
- maximum=1,
199
- step=0.001
200
- )
201
-
202
- # Mover el componente de ejemplos aquí, justo debajo de Opciones Avanzadas
203
- gr.Examples(examples=examples, inputs=text_prompt, label="Ejemplos de otros sueños")
204
-
205
- with gr.Row(elem_id="button-container"):
206
- generate_button = gr.Button("QUIERO VER MI SUEÑO", elem_id="generate-button", variant="primary")
207
- reset_button = gr.Button("REINICIAR MI SUEÑO", elem_id="reset-button", variant="secondary")
208
-
209
- with gr.Column(scale=1):
210
- image_output = gr.Image(type="pil", label="Imagen Resultado", elem_id="gallery")
211
-
212
- generate_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, strength], outputs=image_output)
213
- reset_button.click(lambda: gr.update(text_prompt="", 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)"), outputs=[text_prompt, negative_prompt])
214
-
215
- app.launch(show_api=False, share=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  css = """
2
  #app-container {
3
  max-width: 100%;
 
86
  }
87
  }
88
  """