Update app.py
Browse files
app.py
CHANGED
|
@@ -246,24 +246,30 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 246 |
* 语音输入优先于文本输入。如果同时提供了两者,将使用语音输入。
|
| 247 |
* 模型加载和图像生成可能需要一些时间,尤其是在 CPU 或免费 Hugging Face Spaces 上。
|
| 248 |
* 请确保已在 Space Secrets 中配置 OpenAI API Key (`OPENAI_API_KEY`)。
|
|
|
|
| 249 |
"""
|
| 250 |
-
)
|
| 251 |
|
| 252 |
with gr.Row():
|
| 253 |
with gr.Column(scale=1):
|
| 254 |
# 输入控件
|
| 255 |
-
|
| 256 |
-
|
|
|
|
|
|
|
| 257 |
|
| 258 |
# Gradio 控件使用要求 (至少三种)
|
| 259 |
# 1. Textbox (上面已有)
|
| 260 |
# 2. Audio (上面已有)
|
| 261 |
# 3. Slider
|
| 262 |
-
|
|
|
|
| 263 |
# 4. Slider (另一个)
|
| 264 |
-
|
|
|
|
| 265 |
# 5. Textbox (负面提示词)
|
| 266 |
-
|
|
|
|
| 267 |
|
| 268 |
generate_button = gr.Button("✨ 生成图像", variant="primary")
|
| 269 |
|
|
@@ -277,42 +283,30 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 277 |
out_image = gr.Image(label="生成的图像", type="pil", interactive=False) # 使用 PIL 格式
|
| 278 |
|
| 279 |
# 将按钮点击连接到处理函数
|
| 280 |
-
# 使用 .then() 来链式调用,允许 UI 逐步更新
|
| 281 |
generate_button.click(
|
| 282 |
fn=process_input_and_generate,
|
| 283 |
inputs=[inp_short_prompt_text, inp_short_prompt_audio, inp_negative_prompt, inp_steps, inp_scale],
|
| 284 |
outputs=[out_final_short_prompt, out_detailed_prompt, out_image, status_output]
|
| 285 |
)
|
| 286 |
|
| 287 |
-
# 添加一些示例
|
| 288 |
gr.Examples(
|
| 289 |
examples=[
|
| 290 |
["a magical treehouse in the sky", None, "blurry, low quality", 30, 7.5],
|
| 291 |
["photo of a cute cat wearing sunglasses", None, "cartoon, drawing, sketch", 35, 8.0],
|
| 292 |
["cyberpunk city street at night, raining", None, "daytime, bright sun", 40, 7.0],
|
| 293 |
],
|
|
|
|
| 294 |
inputs=[inp_short_prompt_text, inp_short_prompt_audio, inp_negative_prompt, inp_steps, inp_scale],
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
|
|
|
| 298 |
)
|
| 299 |
|
| 300 |
|
| 301 |
# --- 启动 Gradio 应用 ---
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
print("程序将在 10 秒后退出...")
|
| 307 |
-
# time.sleep(10) # Optional delay
|
| 308 |
-
# exit() # Or let it run and show error in Gradio UI
|
| 309 |
-
|
| 310 |
-
# 检查模型是否加载成功
|
| 311 |
-
if not asr_pipeline:
|
| 312 |
-
print("\n警告:语音识别模型加载失败,语音输入功能将不可用。")
|
| 313 |
-
if not sd_pipeline:
|
| 314 |
-
print("\n警告:Stable Diffusion 模型加载失败,图像生成功能将不可用。")
|
| 315 |
-
|
| 316 |
-
# share=True 会创建一个公开链接(如果在本地运行)
|
| 317 |
-
# 在 Hugging Face Spaces 上运行时不需要 share=True
|
| 318 |
-
app.launch()
|
|
|
|
| 246 |
* 语音输入优先于文本输入。如果同时提供了两者,将使用语音输入。
|
| 247 |
* 模型加载和图像生成可能需要一些时间,尤其是在 CPU 或免费 Hugging Face Spaces 上。
|
| 248 |
* 请确保已在 Space Secrets 中配置 OpenAI API Key (`OPENAI_API_KEY`)。
|
| 249 |
+
* **提示**: 语音输入优先于文本;生成步数影响细节和时间;引导系数控制与提示词的符合度;负面提示词排除不想要的内容。
|
| 250 |
"""
|
| 251 |
+
) # Added essential info to the main markdown
|
| 252 |
|
| 253 |
with gr.Row():
|
| 254 |
with gr.Column(scale=1):
|
| 255 |
# 输入控件
|
| 256 |
+
# REMOVED info argument
|
| 257 |
+
inp_short_prompt_text = gr.Textbox(label="输入简短描述 (例如:空中的魔法树屋)")
|
| 258 |
+
# REMOVED info argument
|
| 259 |
+
inp_short_prompt_audio = gr.Audio(sources=["microphone"], type="filepath", label="或者,使用麦克风说出描述")
|
| 260 |
|
| 261 |
# Gradio 控件使用要求 (至少三种)
|
| 262 |
# 1. Textbox (上面已有)
|
| 263 |
# 2. Audio (上面已有)
|
| 264 |
# 3. Slider
|
| 265 |
+
# REMOVED info argument
|
| 266 |
+
inp_steps = gr.Slider(minimum=10, maximum=100, value=30, step=1, label="生成步数 (Steps)")
|
| 267 |
# 4. Slider (另一个)
|
| 268 |
+
# REMOVED info argument
|
| 269 |
+
inp_scale = gr.Slider(minimum=1.0, maximum=20.0, value=7.5, step=0.5, label="引导系数 (Guidance Scale)")
|
| 270 |
# 5. Textbox (负面提示词)
|
| 271 |
+
# REMOVED info argument
|
| 272 |
+
inp_negative_prompt = gr.Textbox(label="负面提示词 (Negative Prompt)", value="ugly, blurry, low quality, deformed, text, words, signature")
|
| 273 |
|
| 274 |
generate_button = gr.Button("✨ 生成图像", variant="primary")
|
| 275 |
|
|
|
|
| 283 |
out_image = gr.Image(label="生成的图像", type="pil", interactive=False) # 使用 PIL 格式
|
| 284 |
|
| 285 |
# 将按钮点击连接到处理函数
|
|
|
|
| 286 |
generate_button.click(
|
| 287 |
fn=process_input_and_generate,
|
| 288 |
inputs=[inp_short_prompt_text, inp_short_prompt_audio, inp_negative_prompt, inp_steps, inp_scale],
|
| 289 |
outputs=[out_final_short_prompt, out_detailed_prompt, out_image, status_output]
|
| 290 |
)
|
| 291 |
|
| 292 |
+
# 添加一些示例 (确保 inputs/outputs 匹配修改后的组件)
|
| 293 |
gr.Examples(
|
| 294 |
examples=[
|
| 295 |
["a magical treehouse in the sky", None, "blurry, low quality", 30, 7.5],
|
| 296 |
["photo of a cute cat wearing sunglasses", None, "cartoon, drawing, sketch", 35, 8.0],
|
| 297 |
["cyberpunk city street at night, raining", None, "daytime, bright sun", 40, 7.0],
|
| 298 |
],
|
| 299 |
+
# Ensure the order of inputs here matches the inputs list in the .click() call
|
| 300 |
inputs=[inp_short_prompt_text, inp_short_prompt_audio, inp_negative_prompt, inp_steps, inp_scale],
|
| 301 |
+
# Ensure the order of outputs here matches the outputs list in the .click() call
|
| 302 |
+
outputs=[out_final_short_prompt, out_detailed_prompt, out_image, status_output],
|
| 303 |
+
fn=process_input_and_generate,
|
| 304 |
+
cache_examples=False,
|
| 305 |
)
|
| 306 |
|
| 307 |
|
| 308 |
# --- 启动 Gradio 应用 ---
|
| 309 |
+
# (The rest of your file remains the same)
|
| 310 |
+
# if __name__ == "__main__":
|
| 311 |
+
# ... rest of the code ...
|
| 312 |
+
# app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|