Dax451 commited on
Commit
59291b3
·
1 Parent(s): 90a8cd5

Modernizzata l'interfaccia utente con gr.Blocks per un design più pulito e reattivo

Browse files
Files changed (1) hide show
  1. app.py +144 -47
app.py CHANGED
@@ -117,40 +117,154 @@ class FluxNineteenGenerator:
117
  return None, error_message
118
 
119
  def create_ui(generator):
120
- """Crea l'interfaccia utente Gradio"""
121
- with gr.Blocks(title="FLUX Image Generator") as interface:
122
- gr.Markdown("# 🎨 FLUX Image Generator")
123
- gr.Markdown("Genera immagini creative utilizzando il modello FLUX.1-schnell di Nineteen.ai")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
125
  with gr.Row():
126
- with gr.Column(scale=4):
127
- prompt = gr.Textbox(
128
  label="Prompt",
129
- placeholder="Descrivi l'immagine che vuoi generare...",
130
  lines=3
131
  )
132
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  with gr.Row():
134
- submit_btn = gr.Button("🎨 Genera", variant="primary")
135
- clear_btn = gr.Button("🗑️ Pulisci")
136
-
137
- with gr.Column(scale=6):
138
- image_output = gr.Image(label="Immagine Generata", type="filepath")
 
 
 
 
 
 
 
 
 
139
 
140
- # Eventi
141
- submit_btn.click(
142
- fn=generator.generate_image,
143
- inputs=prompt,
144
- outputs=image_output
145
- )
146
- clear_btn.click(
147
- fn=lambda: (None, ""),
148
- inputs=None,
149
- outputs=[image_output, prompt]
150
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
 
152
- return interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
 
 
154
  def main():
155
  """Funzione principale"""
156
  # Crea il generatore
@@ -159,29 +273,12 @@ def main():
159
  # Crea l'interfaccia
160
  interface = create_ui(generator)
161
 
162
- # Ottieni le credenziali dalle variabili d'ambiente
163
- username = os.getenv("GRADIO_USERNAME")
164
- password = os.getenv("GRADIO_PASSWORD")
165
-
166
- # Parametri di base per il lancio
167
- launch_kwargs = {
168
- "server_name": "0.0.0.0",
169
- }
170
-
171
- # Verifica le credenziali e configura l'autenticazione se necessario
172
- if username and password:
173
- print(f"Autenticazione configurata con username: {username}")
174
- launch_kwargs["auth"] = [(username, password)]
175
- else:
176
- print("Autenticazione disabilitata. Su HF Spaces, imposta GRADIO_USERNAME e GRADIO_PASSWORD.")
177
-
178
- # Rileva se siamo su Hugging Face Spaces
179
- if os.getenv("SPACE_ID") is None:
180
- # Non siamo su HF Spaces, aggiungi share per renderlo accessibile
181
- launch_kwargs["share"] = True
182
-
183
- # Avvia l'interfaccia
184
- interface.launch(**launch_kwargs)
185
 
186
  if __name__ == "__main__":
187
  main()
 
117
  return None, error_message
118
 
119
  def create_ui(generator):
120
+ """
121
+ Crea l'interfaccia utente Gradio.
122
+
123
+ Args:
124
+ generator (FluxNineteenGenerator): Istanza del generatore
125
+
126
+ Returns:
127
+ gradio.Interface: L'interfaccia Gradio
128
+ """
129
+
130
+ def generate_image_ui(prompt, model, steps, cfg_scale, height, width, negative_prompt):
131
+ """Funzione per generare immagini dall'interfaccia"""
132
+ # Validazione dei parametri
133
+ if not prompt or len(prompt.strip()) == 0:
134
+ return None, "Il prompt non può essere vuoto."
135
+
136
+ try:
137
+ # Converti i parametri al tipo corretto
138
+ steps = int(steps)
139
+ cfg_scale = float(cfg_scale)
140
+ height = int(height)
141
+ width = int(width)
142
+
143
+ # Validazione dei valori
144
+ if steps < 1 or steps > 50:
145
+ return None, "Il numero di passi deve essere compreso tra 1 e 50."
146
+ if cfg_scale < 1 or cfg_scale > 30:
147
+ return None, "La guidance scale deve essere compresa tra 1 e 30."
148
+ if height < 512 or height > 1536:
149
+ return None, "L'altezza deve essere compresa tra 512 e 1536 pixel."
150
+ if width < 512 or width > 1536:
151
+ return None, "La larghezza deve essere compresa tra 512 e 1536 pixel."
152
+
153
+ # Genera l'immagine
154
+ return generator.generate_image(
155
+ prompt=prompt,
156
+ model=model,
157
+ steps=steps,
158
+ cfg_scale=cfg_scale,
159
+ height=height,
160
+ width=width,
161
+ negative_prompt=negative_prompt
162
+ )
163
+ except Exception as e:
164
+ return None, f"Errore: {str(e)}"
165
+
166
+ # Crea i componenti dell'interfaccia
167
+ with gr.Blocks(title="FLUX Nineteen.ai Image Generator") as interface:
168
+ gr.Markdown("# FLUX Nineteen.ai Image Generator")
169
+ gr.Markdown("Genera immagini utilizzando il modello FLUX.1-schnell tramite l'API di Nineteen.ai")
170
 
171
  with gr.Row():
172
+ with gr.Column(scale=3):
173
+ prompt_input = gr.Textbox(
174
  label="Prompt",
175
+ placeholder="Descrivi l'immagine che desideri generare...",
176
  lines=3
177
  )
178
 
179
+ negative_prompt_input = gr.Textbox(
180
+ label="Prompt Negativo (opzionale)",
181
+ placeholder="Elementi da escludere dall'immagine...",
182
+ lines=2
183
+ )
184
+
185
+ model_input = gr.Dropdown(
186
+ generator.models,
187
+ label="Modello",
188
+ value=generator.default_model
189
+ )
190
+
191
  with gr.Row():
192
+ steps_input = gr.Slider(
193
+ minimum=1,
194
+ maximum=50,
195
+ value=8,
196
+ step=1,
197
+ label="Passi di Inferenza"
198
+ )
199
+ cfg_input = gr.Slider(
200
+ minimum=1.0,
201
+ maximum=30.0,
202
+ value=3.0,
203
+ step=0.1,
204
+ label="Guidance Scale (CFG)"
205
+ )
206
 
207
+ with gr.Row():
208
+ height_input = gr.Slider(
209
+ minimum=512,
210
+ maximum=1536,
211
+ value=1024,
212
+ step=64,
213
+ label="Altezza (px)"
214
+ )
215
+ width_input = gr.Slider(
216
+ minimum=512,
217
+ maximum=1536,
218
+ value=1024,
219
+ step=64,
220
+ label="Larghezza (px)"
221
+ )
222
+
223
+ generate_button = gr.Button("Genera Immagine", variant="primary")
224
+
225
+ with gr.Column(scale=4):
226
+ output_image = gr.Image(label="Immagine Generata", type="pil")
227
+ output_status = gr.Textbox(label="Stato", interactive=False)
228
+
229
+ # Esempi di prompt
230
+ with gr.Accordion("Esempi di Prompt", open=False):
231
+ gr.Markdown("""
232
+ ### Esempi di prompt ottimizzati per FLUX.1-schnell
233
+
234
+ Clicca su uno degli esempi per utilizzarlo:
235
+ """)
236
+
237
+ examples = [
238
+ ["A breathtaking view of the Dolomites at sunrise, golden light illuminating the jagged peaks, morning mist rising from the valley below, ultra-detailed, cinematic, 8K resolution, photorealistic"],
239
+ ["Futuristic Tokyo cityscape at night, neon lights reflecting on wet streets after rain, towering skyscrapers with holographic advertisements, flying vehicles, photorealistic, cinematic lighting, 8K"],
240
+ ["Portrait of a weathered old fisherman with deep wrinkles and piercing blue eyes, wearing a cable-knit sweater, salt and pepper beard, golden hour lighting, ultra-detailed skin texture, photorealistic"],
241
+ ["Massive space station orbiting Jupiter, with Earth visible in the distance, detailed mechanical structures, solar panels, docking bays with spacecraft, photorealistic, NASA quality, 8K"],
242
+ ["Bioluminescent forest at night with giant mushrooms, glowing plants, mystical atmosphere, small magical creatures, ultra-detailed vegetation, photorealistic textures, fantasy world with realistic lighting"]
243
+ ]
244
+
245
+ gr.Examples(
246
+ examples=examples,
247
+ inputs=prompt_input
248
+ )
249
 
250
+ # Collega il pulsante di generazione
251
+ generate_button.click(
252
+ generate_image_ui,
253
+ inputs=[
254
+ prompt_input,
255
+ model_input,
256
+ steps_input,
257
+ cfg_input,
258
+ height_input,
259
+ width_input,
260
+ negative_prompt_input
261
+ ],
262
+ outputs=[output_image, output_status]
263
+ )
264
+
265
+ return interface
266
 
267
+ # Funzione principale
268
  def main():
269
  """Funzione principale"""
270
  # Crea il generatore
 
273
  # Crea l'interfaccia
274
  interface = create_ui(generator)
275
 
276
+ # Avvia l'interfaccia senza autenticazione (l'autenticazione sarà gestita da Hugging Face Spaces)
277
+ # Utilizziamo i parametri necessari per Hugging Face Spaces
278
+ interface.launch(
279
+ server_name="0.0.0.0",
280
+ share=False
281
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
282
 
283
  if __name__ == "__main__":
284
  main()