Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,13 @@ from PIL import Image
|
|
| 7 |
# === 模型定义 ===
|
| 8 |
llm = pipeline("text2text-generation", model="google/flan-t5-large")
|
| 9 |
stt = pipeline("automatic-speech-recognition", model="openai/whisper-small")
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# === Prompt-to-Prompt ===
|
| 14 |
def refine_prompt(user_input):
|
|
@@ -18,7 +23,7 @@ def refine_prompt(user_input):
|
|
| 18 |
|
| 19 |
# === Prompt-to-Image ===
|
| 20 |
def generate_image(prompt, model_choice, num_steps, guidance):
|
| 21 |
-
image =
|
| 22 |
return prompt, image
|
| 23 |
|
| 24 |
# === 总流程 ===
|
|
@@ -27,6 +32,7 @@ def process_all(user_input, model_choice, steps, guidance):
|
|
| 27 |
prompt, image = generate_image(refined, model_choice, steps, guidance)
|
| 28 |
return refined, image
|
| 29 |
|
|
|
|
| 30 |
def transcribe_audio(audio):
|
| 31 |
text = stt(audio)["text"]
|
| 32 |
return text
|
|
@@ -37,10 +43,10 @@ with gr.Blocks() as demo:
|
|
| 37 |
|
| 38 |
with gr.Row():
|
| 39 |
with gr.Column():
|
| 40 |
-
audio_input = gr.Audio(
|
| 41 |
voice_btn = gr.Button("使用语音输入")
|
| 42 |
textbox = gr.Textbox(label="描述一句画面", placeholder="比如:空中的魔法树屋")
|
| 43 |
-
model_choice = gr.Radio(["SD v1.4", "SDXL"], value="SDXL", label="选择模型")
|
| 44 |
steps_slider = gr.Slider(10, 50, value=30, step=5, label="推理步数")
|
| 45 |
guidance_slider = gr.Slider(5, 15, value=7.5, step=0.5, label="引导系数")
|
| 46 |
submit_btn = gr.Button("生成图像 🎨")
|
|
@@ -52,6 +58,6 @@ with gr.Blocks() as demo:
|
|
| 52 |
voice_btn.click(fn=transcribe_audio, inputs=audio_input, outputs=textbox)
|
| 53 |
submit_btn.click(fn=process_all, inputs=[textbox, model_choice, steps_slider, guidance_slider], outputs=[refined_out, image_out])
|
| 54 |
|
| 55 |
-
# === 启动 ===
|
| 56 |
demo.launch()
|
| 57 |
|
|
|
|
|
|
| 7 |
# === 模型定义 ===
|
| 8 |
llm = pipeline("text2text-generation", model="google/flan-t5-large")
|
| 9 |
stt = pipeline("automatic-speech-recognition", model="openai/whisper-small")
|
| 10 |
+
|
| 11 |
+
# 加载 Stable Diffusion v1.5 模型(去掉 float16)
|
| 12 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 13 |
+
"runwayml/stable-diffusion-v1-5",
|
| 14 |
+
safety_checker=None, # 可选:避免屏蔽图像
|
| 15 |
+
)
|
| 16 |
+
pipe = pipe.to("cpu") # 或者 "cuda" 如果你在 GPU 上部署
|
| 17 |
|
| 18 |
# === Prompt-to-Prompt ===
|
| 19 |
def refine_prompt(user_input):
|
|
|
|
| 23 |
|
| 24 |
# === Prompt-to-Image ===
|
| 25 |
def generate_image(prompt, model_choice, num_steps, guidance):
|
| 26 |
+
image = pipe(prompt, num_inference_steps=num_steps, guidance_scale=guidance).images[0]
|
| 27 |
return prompt, image
|
| 28 |
|
| 29 |
# === 总流程 ===
|
|
|
|
| 32 |
prompt, image = generate_image(refined, model_choice, steps, guidance)
|
| 33 |
return refined, image
|
| 34 |
|
| 35 |
+
# === 语音识别 ===
|
| 36 |
def transcribe_audio(audio):
|
| 37 |
text = stt(audio)["text"]
|
| 38 |
return text
|
|
|
|
| 43 |
|
| 44 |
with gr.Row():
|
| 45 |
with gr.Column():
|
| 46 |
+
audio_input = gr.Audio(type="filepath", label="🎤 或点击录音")
|
| 47 |
voice_btn = gr.Button("使用语音输入")
|
| 48 |
textbox = gr.Textbox(label="描述一句画面", placeholder="比如:空中的魔法树屋")
|
| 49 |
+
model_choice = gr.Radio(["SD v1.4", "SDXL"], value="SDXL", label="选择模型")
|
| 50 |
steps_slider = gr.Slider(10, 50, value=30, step=5, label="推理步数")
|
| 51 |
guidance_slider = gr.Slider(5, 15, value=7.5, step=0.5, label="引导系数")
|
| 52 |
submit_btn = gr.Button("生成图像 🎨")
|
|
|
|
| 58 |
voice_btn.click(fn=transcribe_audio, inputs=audio_input, outputs=textbox)
|
| 59 |
submit_btn.click(fn=process_all, inputs=[textbox, model_choice, steps_slider, guidance_slider], outputs=[refined_out, image_out])
|
| 60 |
|
|
|
|
| 61 |
demo.launch()
|
| 62 |
|
| 63 |
+
|