Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,24 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline,
|
| 3 |
-
from diffusers import AutoPipelineForText2Image
|
| 4 |
import torch
|
|
|
|
| 5 |
|
| 6 |
-
# 使用
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def generate_prompt(description: str) -> str:
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
return prompt
|
| 13 |
|
| 14 |
-
# 使用
|
| 15 |
-
|
|
|
|
| 16 |
text2image_pipeline.to("cpu") # 使用CPU
|
| 17 |
|
| 18 |
def generate_image(prompt: str):
|
|
@@ -21,6 +27,7 @@ def generate_image(prompt: str):
|
|
| 21 |
return image
|
| 22 |
|
| 23 |
# 使用Whisper模型进行语音转文本
|
|
|
|
| 24 |
processor = WhisperProcessor.from_pretrained("openai/whisper-large")
|
| 25 |
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-large")
|
| 26 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline, T5ForConditionalGeneration, T5Tokenizer
|
|
|
|
| 3 |
import torch
|
| 4 |
+
import stable_diffusion_webnn # 假设stable-diffusion-v1.5-webnn的库名为 stable_diffusion_webnn
|
| 5 |
|
| 6 |
+
# 使用T5模型生成文本描述
|
| 7 |
+
model_name = "t5-large" # 可以根据需求选择不同版本的T5
|
| 8 |
+
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 9 |
+
t5_model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 10 |
|
| 11 |
def generate_prompt(description: str) -> str:
|
| 12 |
+
# 使用T5模型生成详细的图像生成提示
|
| 13 |
+
input_text = f"将这个描述扩展为一个详细的图像生成提示:{description}"
|
| 14 |
+
inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
|
| 15 |
+
outputs = t5_model.generate(inputs["input_ids"], max_length=150, num_beams=5, early_stopping=True)
|
| 16 |
+
prompt = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 17 |
return prompt
|
| 18 |
|
| 19 |
+
# 使用 stable-diffusion-v1.5-webnn 库加载 Stable Diffusion 模型
|
| 20 |
+
# 这里假设 stable_diffusion_webnn 可以直接加载模型并生成图像
|
| 21 |
+
text2image_pipeline = stable_diffusion_webnn.StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base")
|
| 22 |
text2image_pipeline.to("cpu") # 使用CPU
|
| 23 |
|
| 24 |
def generate_image(prompt: str):
|
|
|
|
| 27 |
return image
|
| 28 |
|
| 29 |
# 使用Whisper模型进行语音转文本
|
| 30 |
+
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
| 31 |
processor = WhisperProcessor.from_pretrained("openai/whisper-large")
|
| 32 |
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-large")
|
| 33 |
|