Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,44 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
from
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def generate_artwork(description, style, enhance_details, audio_file):
|
| 7 |
# 如果上传了音频文件,进行语音转文本
|
| 8 |
if audio_file is not None:
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
from diffusers import StableDiffusionPipeline
|
| 5 |
+
import whisper
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
# 加载 Whisper 模型
|
| 9 |
+
whisper_model = whisper.load_model("base")
|
| 10 |
+
|
| 11 |
+
# 使用 GPT-Neo 生成中文提示词
|
| 12 |
+
model_name = "EleutherAI/gpt-neo-2.7B" # 你可以选择其它的模型
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 14 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 15 |
+
|
| 16 |
+
def generate_prompt(user_input):
|
| 17 |
+
# 编码输入并生成提示
|
| 18 |
+
inputs = tokenizer.encode(f"根据以下描述生成一个结构良好的提示,适用于稳定扩散图像生成:'{user_input}'", return_tensors="pt")
|
| 19 |
+
|
| 20 |
+
# 使用模型生成响应
|
| 21 |
+
outputs = model.generate(inputs, max_length=100, num_return_sequences=1)
|
| 22 |
+
|
| 23 |
+
# 解码输出并返回
|
| 24 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 25 |
+
|
| 26 |
+
# 使用 Stable Diffusion 生成图像
|
| 27 |
+
def generate_image(prompt):
|
| 28 |
+
# 使用 Stable Diffusion 生成图像
|
| 29 |
+
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
|
| 30 |
+
pipe.to("cpu") # 使用 CPU 生成图像(如有 GPU 可调整为 "cuda")
|
| 31 |
+
|
| 32 |
+
# 生成图像
|
| 33 |
+
image = pipe(prompt).images[0]
|
| 34 |
+
return image
|
| 35 |
+
|
| 36 |
+
# 语音转文本
|
| 37 |
+
def transcribe_audio(audio_file):
|
| 38 |
+
result = whisper_model.transcribe(audio_file, language="zh") # 指定中文识别
|
| 39 |
+
return result['text']
|
| 40 |
+
|
| 41 |
+
# 生成艺术作品
|
| 42 |
def generate_artwork(description, style, enhance_details, audio_file):
|
| 43 |
# 如果上传了音频文件,进行语音转文本
|
| 44 |
if audio_file is not None:
|