Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,36 @@
|
|
| 1 |
-
import
|
| 2 |
-
from transformers import pipeline
|
| 3 |
-
from
|
| 4 |
-
import numpy as np
|
| 5 |
import torch
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
|
| 39 |
-
|
| 40 |
-
# Stage 1: Image to Text
|
| 41 |
-
st.text('Processing img2text...')
|
| 42 |
-
scenario = img2text(uploaded_file.name)
|
| 43 |
-
st.write(scenario)
|
| 44 |
-
|
| 45 |
-
# Stage 2: Text to Story
|
| 46 |
-
st.text('Generating a story...')
|
| 47 |
-
story = text2story(scenario)
|
| 48 |
-
st.write(story)
|
| 49 |
-
|
| 50 |
-
# Stage 3: Story to Audio
|
| 51 |
-
st.text('Generating audio...')
|
| 52 |
-
audio_array, sample_rate = text2audio(story)
|
| 53 |
-
|
| 54 |
-
audio_file = "output_audio.wav"
|
| 55 |
-
write_wav(audio_file, sample_rate, audio_array)
|
| 56 |
-
st.audio(audio_file)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from diffusers import StableDiffusionPipeline
|
|
|
|
| 4 |
import torch
|
| 5 |
|
| 6 |
+
# 文本生成模型:使用 GPT-2 或其他公开模型
|
| 7 |
+
text_generator = pipeline("text-generation", model="gpt2")
|
| 8 |
+
|
| 9 |
+
# 图像生成模型:使用 Stable Diffusion
|
| 10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
+
image_generator = StableDiffusionPipeline.from_pretrained(
|
| 12 |
+
"runwayml/stable-diffusion-v1-5",
|
| 13 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 14 |
+
).to(device)
|
| 15 |
+
|
| 16 |
+
def generate_text_and_image(keyword):
|
| 17 |
+
# Step 1: 根据关键词生成文本
|
| 18 |
+
prompt = f"Write a short paragraph about {keyword}."
|
| 19 |
+
text_output = text_generator(prompt, max_length=100, do_sample=True, temperature=0.8)[0]["generated_text"]
|
| 20 |
+
|
| 21 |
+
# Step 2: 使用生成的文本生成图像
|
| 22 |
+
image = image_generator(text_output).images[0]
|
| 23 |
+
|
| 24 |
+
return text_output, image
|
| 25 |
+
|
| 26 |
+
# Gradio界面
|
| 27 |
+
iface = gr.Interface(
|
| 28 |
+
fn=generate_text_and_image,
|
| 29 |
+
inputs=gr.Textbox(lines=1, placeholder="请输入关键词,例如:a futuristic city"),
|
| 30 |
+
outputs=[gr.Textbox(label="生成的文字"), gr.Image(label="根据文字生成的图像")],
|
| 31 |
+
title="关键词到文字 + 图像生成器",
|
| 32 |
+
description="输入关键词,先生成一段文字,再根据这段文字生成图像(使用Hugging Face模型)。"
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|