Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
-
import torch
|
|
|
|
| 3 |
from transformers import pipeline
|
| 4 |
from diffusers import StableDiffusionPipeline
|
| 5 |
|
|
@@ -41,16 +42,20 @@ def transcribe(audio_path):
|
|
| 41 |
|
| 42 |
def generate(description, model_choice, guidance_scale, negative_prompt, style):
|
| 43 |
# 构造给 LLM 的指令
|
| 44 |
-
instruction =
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
result = llm(instruction, max_length=128)[0]["generated_text"].strip()
|
| 48 |
prompt = result
|
| 49 |
# 根据模型选择生成图像
|
| 50 |
-
if model_choice == "SDXL"
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
return prompt, image
|
| 55 |
|
| 56 |
# Step 3: 构建 Gradio 界面
|
|
@@ -83,8 +88,15 @@ with gr.Blocks(title="Prompt-to-Image Generator") as demo:
|
|
| 83 |
prompt_output = gr.Textbox(label="生成的提示词")
|
| 84 |
image_output = gr.Image(label="生成的图像")
|
| 85 |
|
| 86 |
-
# 绑定语音转文字
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
# 点击按钮生成提示词并绘图
|
| 89 |
generate_btn.click(
|
| 90 |
fn=generate,
|
|
@@ -94,4 +106,4 @@ with gr.Blocks(title="Prompt-to-Image Generator") as demo:
|
|
| 94 |
|
| 95 |
# Step 4: 启动应用
|
| 96 |
if __name__ == "__main__":
|
| 97 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
from transformers import pipeline
|
| 5 |
from diffusers import StableDiffusionPipeline
|
| 6 |
|
|
|
|
| 42 |
|
| 43 |
def generate(description, model_choice, guidance_scale, negative_prompt, style):
|
| 44 |
# 构造给 LLM 的指令
|
| 45 |
+
instruction = (
|
| 46 |
+
f"请将以下简短描述扩展为 Stable Diffusion 友好的提示词,包含细节和风格:\n"
|
| 47 |
+
f"描述: '{description}'\n"
|
| 48 |
+
f"风格: '{style}'"
|
| 49 |
+
)
|
| 50 |
result = llm(instruction, max_length=128)[0]["generated_text"].strip()
|
| 51 |
prompt = result
|
| 52 |
# 根据模型选择生成图像
|
| 53 |
+
pipeline = sd_xl if model_choice == "SDXL" else sd_v15
|
| 54 |
+
image = pipeline(
|
| 55 |
+
prompt,
|
| 56 |
+
guidance_scale=guidance_scale,
|
| 57 |
+
negative_prompt=negative_prompt
|
| 58 |
+
).images[0]
|
| 59 |
return prompt, image
|
| 60 |
|
| 61 |
# Step 3: 构建 Gradio 界面
|
|
|
|
| 88 |
prompt_output = gr.Textbox(label="生成的提示词")
|
| 89 |
image_output = gr.Image(label="生成的图像")
|
| 90 |
|
| 91 |
+
# 绑定语音转文字(仅当启用时)
|
| 92 |
+
def conditional_transcribe(audio_path, use_voice_flag):
|
| 93 |
+
return transcribe(audio_path) if use_voice_flag else None
|
| 94 |
+
|
| 95 |
+
audio_input.change(
|
| 96 |
+
fn=conditional_transcribe,
|
| 97 |
+
inputs=[audio_input, use_voice],
|
| 98 |
+
outputs=desc_input
|
| 99 |
+
)
|
| 100 |
# 点击按钮生成提示词并绘图
|
| 101 |
generate_btn.click(
|
| 102 |
fn=generate,
|
|
|
|
| 106 |
|
| 107 |
# Step 4: 启动应用
|
| 108 |
if __name__ == "__main__":
|
| 109 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|