concauu commited on
Commit
45d77db
·
verified ·
1 Parent(s): 2d73b6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -56
app.py CHANGED
@@ -14,8 +14,8 @@ from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_
14
  from io import BytesIO
15
  import base64
16
  from diffusers.pipelines.flux.pipeline_flux import FluxPipeline
17
- # For voice transcription
18
- import speech_recognition as sr
19
  os.environ['HF_HUB_DOWNLOAD_TIMEOUT'] = '120'
20
  dtype = torch.bfloat16
21
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -149,65 +149,56 @@ Lighting: Time of day, intensity, direction (e.g., backlighting).
149
  except Exception as e:
150
  enhanced = f"Error enhancing prompt: {str(e)}"
151
  return enhanced
152
- # --- Voice Transcription Function ---
153
- def transcribe_audio(audio_file):
154
- r = sr.Recognizer()
155
- try:
156
- with sr.AudioFile(audio_file) as source:
157
- audio_data = r.record(source)
158
- text = r.recognize_google(audio_data)
159
- except Exception as e:
160
- text = f"Transcription error: {str(e)}"
161
- return text
162
-
163
- # --- Gradio Interface with Enhanced UI and Voice Recognition ---
164
- custom_css = """
165
  #col-container {
166
  margin: 0 auto;
167
- max-width: 600px;
168
- padding: 20px;
169
  }
170
- .user-msg { background: #e3f2fd; border-radius: 15px; padding: 10px; margin: 5px; }
171
- .bot-msg { background: #f5f5f5; border-radius: 15px; padding: 10px; margin: 5px; }
172
  """
173
 
174
- with gr.Blocks(css=custom_css, title="FLUX.1 [dev] Enhanced UI with Voice Recognition") as demo:
175
- gr.Markdown("# FLUX.1 [dev] with Enhanced UI and Voice Recognition")
176
- # Using Tabs to separate functionalities
177
- with gr.Tabs():
178
- with gr.Tab("Prompt Enhancement"):
179
- gr.Markdown("### Step 1: Enhance Your Prompt")
180
- original_prompt = gr.Textbox(label="Original Prompt", placeholder="Enter your creative idea here...", lines=3)
181
- enhance_button = gr.Button("Enhance Prompt", variant="secondary")
182
- enhanced_prompt = gr.Textbox(label="Enhanced Prompt (Editable)", placeholder="Enhanced prompt appears here...", lines=3)
183
- enhance_button.click(enhance_prompt, original_prompt, enhanced_prompt)
184
-
185
- with gr.Tab("Voice Recognition"):
186
- gr.Markdown("### Step 1A: Record Your Prompt")
187
- audio_input = gr.Audio(source=["microphone"], type="filepath", label="Record your prompt")
188
- transcribe_button = gr.Button("Transcribe Audio", variant="secondary")
189
- voice_text = gr.Textbox(label="Transcribed Prompt", placeholder="Your spoken prompt will appear here...", lines=3)
190
- transcribe_button.click(transcribe_audio, audio_input, voice_text)
191
-
192
- with gr.Tab("Generate Image"):
193
- gr.Markdown("### Step 2: Generate Image")
194
- with gr.Row():
195
- run_button = gr.Button("Generate Image", variant="primary")
196
- clear_history_button = gr.Button("Clear History", variant="secondary")
197
- result = gr.Image(label="Generated Image", show_label=False)
198
- with gr.Accordion("Advanced Settings", open=False):
199
- seed = gr.Slider(0, MAX_SEED, value=0, label="Seed", info="Seed for reproducibility")
200
- randomize_seed = gr.Checkbox(True, label="Randomize Seed")
201
- with gr.Row():
202
- width = gr.Slider(256, MAX_IMAGE_SIZE, 1024, step=32, label="Width")
203
- height = gr.Slider(256, MAX_IMAGE_SIZE, 1024, step=32, label="Height")
204
- with gr.Row():
205
- guidance_scale = gr.Slider(1, 15, 3.5, step=0.1, label="Guidance Scale")
206
- num_inference_steps = gr.Slider(1, 50, 28, step=1, label="Inference Steps")
207
- with gr.Accordion("Generation History", open=False):
208
- history_display = gr.HTML("<p style='margin: 20px;'>No generations yet</p>")
209
- # State to track generation history
210
- history_state = gr.State([])
 
 
211
  generation_event = run_button.click(
212
  fn=infer,
213
  inputs=[enhanced_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
 
14
  from io import BytesIO
15
  import base64
16
  from diffusers.pipelines.flux.pipeline_flux import FluxPipeline
17
+ import torch.nn.functional as F
18
+ import torch.nn as nn
19
  os.environ['HF_HUB_DOWNLOAD_TIMEOUT'] = '120'
20
  dtype = torch.bfloat16
21
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
149
  except Exception as e:
150
  enhanced = f"Error enhancing prompt: {str(e)}"
151
  return enhanced
152
+ def fake_typing():
153
+ for _ in range(3):
154
+ yield {"visible": False}, {"visible": True}
155
+ time.sleep(0.5)
156
+ css = """
 
 
 
 
 
 
 
 
157
  #col-container {
158
  margin: 0 auto;
159
+ max-width: 520px;
 
160
  }
 
 
161
  """
162
 
163
+ with gr.Blocks(css="""
164
+ .user-msg { background: #e3f2fd; border-radius: 15px; padding: 10px; margin: 5px; }
165
+ .bot-msg { background: #f5f5f5; border-radius: 15px; padding: 10px; margin: 5px; }
166
+ """) as demo:
167
+ chatbot = gr.Chatbot(elem_classes=["user-msg", "bot-msg"])
168
+ history_state = gr.State([])
169
+ with gr.Column(elem_id="col-container"):
170
+ gr.Markdown("# FLUX.1 [dev] with History Tracking")
171
+ gr.Markdown("### Step 1: Enhance Your Prompt")
172
+ original_prompt = gr.Textbox(label="Original Prompt", lines=2)
173
+ enhance_button = gr.Button("Enhance Prompt")
174
+ enhanced_prompt = gr.Textbox(label="Enhanced Prompt (Editable)", lines=2)
175
+ enhance_button.click(enhance_prompt, original_prompt, enhanced_prompt)
176
+ gr.Markdown("### Step 2: Generate Image")
177
+ with gr.Row():
178
+ run_button = gr.Button("Generate Image", variant="primary")
179
+ result = gr.Image(label="Result", show_label=False)
180
+ with gr.Accordion("Advanced Settings"):
181
+ seed = gr.Slider(0, MAX_SEED, value=0, label="Seed")
182
+ randomize_seed = gr.Checkbox(True, label="Randomize seed")
183
+ with gr.Row():
184
+ width = gr.Slider(256, MAX_IMAGE_SIZE, 1024, step=32, label="Width")
185
+ height = gr.Slider(256, MAX_IMAGE_SIZE, 1024, step=32, label="Height")
186
+ with gr.Row():
187
+ guidance_scale = gr.Slider(1, 15, 3.5, step=0.1, label="Guidance Scale")
188
+ num_inference_steps = gr.Slider(1, 50, 28, step=1, label="Inference Steps")
189
+ with gr.Accordion("Generation History", open=False):
190
+ history_display = gr.HTML("<p style='margin: 20px;'>No generations yet</p>")
191
+ gr.Examples(
192
+ examples=[
193
+ "a tiny astronaut hatching from an egg on the moon",
194
+ "a cat holding a sign that says hello world",
195
+ "an anime illustration of a wiener schnitzel",
196
+ ],
197
+ inputs=enhanced_prompt,
198
+ outputs=[result, seed],
199
+ fn=infer,
200
+ cache_examples="lazy"
201
+ )
202
  generation_event = run_button.click(
203
  fn=infer,
204
  inputs=[enhanced_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],