shenyugan commited on
Commit
9a172c5
·
verified ·
1 Parent(s): de964a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -10
app.py CHANGED
@@ -153,6 +153,7 @@ def generate_image(prompt, negative_prompt, steps, scale):
153
  torch.cuda.empty_cache()
154
  return None, f"错误:图像生成失败 - {e}"
155
 
 
156
  # --- Gradio 应用主逻辑 ---
157
  def process_input_and_generate(short_prompt_text, short_prompt_audio, negative_prompt, steps, scale):
158
  """
@@ -163,6 +164,8 @@ def process_input_and_generate(short_prompt_text, short_prompt_audio, negative_p
163
 
164
  final_short_prompt = ""
165
  status_updates = []
 
 
166
 
167
  # 1. 确定输入源 (优先使用语音)
168
  if short_prompt_audio is not None:
@@ -170,7 +173,9 @@ def process_input_and_generate(short_prompt_text, short_prompt_audio, negative_p
170
  transcribed_text, status = transcribe_audio(short_prompt_audio)
171
  status_updates.append(status)
172
  if "错误" in transcribed_text:
173
- yield None, None, None, f"状态:处理失败\n{status}"
 
 
174
  return
175
  final_short_prompt = transcribed_text
176
  yield f"语音识别结果: {final_short_prompt}", None, None, "状态:语音识别完成"
@@ -186,26 +191,49 @@ def process_input_and_generate(short_prompt_text, short_prompt_audio, negative_p
186
  yield "错误:无法获取有效的简短描述。", None, None, "状态:错误 - 无有效输入"
187
  return
188
 
 
 
 
 
 
189
  # 2. 生成详细 Prompt
190
- yield final_short_prompt, "正在生成详细提示词...", None, f"状态:正在调用 LLM 生成详细提示词...\n{'\n'.join(status_updates)}"
191
  detailed_prompt, status = generate_detailed_prompt(final_short_prompt)
192
  status_updates.append(status)
 
193
  if "错误" in detailed_prompt:
194
- yield final_short_prompt, detailed_prompt, None, f"状态:处理失败\n{'\n'.join(status_updates)}"
 
 
 
195
  return
196
- yield final_short_prompt, detailed_prompt, None, f"状态:详细提示词生成成功,准备生成图像...\n{'\n'.join(status_updates)}"
 
 
 
197
 
198
  # 3. 生成图像
199
- yield final_short_prompt, detailed_prompt, "正在生成图像...", f"状态:正在调用 Stable Diffusion...\n{'\n'.join(status_updates)}"
200
- image, status = generate_image(detailed_prompt, negative_prompt, steps, scale)
 
 
 
 
 
201
  status_updates.append(status)
202
- if image is None:
203
- yield final_short_prompt, detailed_prompt, None, f"状态:处理失败\n{'\n'.join(status_updates)}"
 
 
 
 
 
204
  return
205
 
206
  # 4. 显示最终结果
207
- yield final_short_prompt, detailed_prompt, image, f"状态:完成!\n{'\n'.join(status_updates)}"
208
-
 
 
209
 
210
  # --- Gradio 界面构建 ---
211
  with gr.Blocks(theme=gr.themes.Soft()) as app:
 
153
  torch.cuda.empty_cache()
154
  return None, f"错误:图像生成失败 - {e}"
155
 
156
+ # --- Gradio 应用主逻辑 ---
157
  # --- Gradio 应用主逻辑 ---
158
  def process_input_and_generate(short_prompt_text, short_prompt_audio, negative_prompt, steps, scale):
159
  """
 
164
 
165
  final_short_prompt = ""
166
  status_updates = []
167
+ image_result = None # Initialize image result
168
+ detailed_prompt_result = None # Initialize detailed prompt result
169
 
170
  # 1. 确定输入源 (优先使用语音)
171
  if short_prompt_audio is not None:
 
173
  transcribed_text, status = transcribe_audio(short_prompt_audio)
174
  status_updates.append(status)
175
  if "错误" in transcribed_text:
176
+ # FIX: Separate f-string and newline concatenation
177
+ status_message = f"状态:处理失败\n{status}"
178
+ yield None, None, None, status_message
179
  return
180
  final_short_prompt = transcribed_text
181
  yield f"语音识别结果: {final_short_prompt}", None, None, "状态:语音识别完成"
 
191
  yield "错误:无法获取有效的简短描述。", None, None, "状态:错误 - 无有效输入"
192
  return
193
 
194
+ # --- Update the display of the short prompt used ---
195
+ # We yield the final_short_prompt along with other updates from now on
196
+ detailed_prompt_result = "正在生成详细提示词..."
197
+ yield final_short_prompt, detailed_prompt_result, image_result, f"状态:正在调用 LLM 生成详细提示词...\n{status_updates[-1] if status_updates else ''}" # Show last status
198
+
199
  # 2. 生成详细 Prompt
 
200
  detailed_prompt, status = generate_detailed_prompt(final_short_prompt)
201
  status_updates.append(status)
202
+ detailed_prompt_result = detailed_prompt # Update the result holder
203
  if "错误" in detailed_prompt:
204
+ # FIX: Separate f-string and newline concatenation
205
+ joined_updates = "\n".join(status_updates)
206
+ status_message = f"状态:处理失败\n{joined_updates}"
207
+ yield final_short_prompt, detailed_prompt_result, image_result, status_message
208
  return
209
+ # FIX: Separate f-string and newline concatenation
210
+ joined_updates = "\n".join(status_updates)
211
+ status_message = f"状态:详细提示词生成成功,准备生成图像...\n{joined_updates}"
212
+ yield final_short_prompt, detailed_prompt_result, image_result, status_message # Keep image empty for now
213
 
214
  # 3. 生成图像
215
+ image_result = "正在生成图像..." # Placeholder text while generating
216
+ # FIX: Separate f-string and newline concatenation
217
+ joined_updates = "\n".join(status_updates)
218
+ status_message = f"状态:正在调用 Stable Diffusion...\n{joined_updates}"
219
+ yield final_short_prompt, detailed_prompt_result, image_result, status_message
220
+
221
+ image, status = generate_image(detailed_prompt_result, negative_prompt, steps, scale)
222
  status_updates.append(status)
223
+ image_result = image # Update with actual image or None
224
+ if image_result is None:
225
+ # FIX: Separate f-string and newline concatenation
226
+ joined_updates = "\n".join(status_updates)
227
+ status_message = f"状态:处理失败\n{joined_updates}"
228
+ # Yield the error message in the image spot temporarily? Or keep it None. Let's keep it None.
229
+ yield final_short_prompt, detailed_prompt_result, None, status_message
230
  return
231
 
232
  # 4. 显示最终结果
233
+ # FIX: Separate f-string and newline concatenation
234
+ joined_updates = "\n".join(status_updates)
235
+ status_message = f"状态:完成!\n{joined_updates}"
236
+ yield final_short_prompt, detailed_prompt_result, image_result, status_message
237
 
238
  # --- Gradio 界面构建 ---
239
  with gr.Blocks(theme=gr.themes.Soft()) as app: