Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
from transformers import pipeline
|
4 |
+
from gtts import gTTS
|
5 |
+
import os
|
6 |
+
|
7 |
+
# 加载 Hugging Face 模型
|
8 |
+
image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
9 |
+
story_generator = pipeline("text-generation", model="facebook/opt-1.3b")
|
10 |
+
|
11 |
+
# 图片 → 文字(生成描述)
|
12 |
+
def img2text(image_path):
|
13 |
+
text = image_to_text_model(image_path)[0]["generated_text"]
|
14 |
+
return text
|
15 |
+
|
16 |
+
# 文字 → 故事(生成完整故事)
|
17 |
+
def text2story(text):
|
18 |
+
prompt = f"Write a fun and magical children's story based on this idea: {text}.\n\nOnce upon a time..."
|
19 |
+
story = story_generator(prompt, max_length=250, do_sample=True, temperature=0.8, top_p=0.9, repetition_penalty=1.2, truncation=True)[0]['generated_text']
|
20 |
+
return story
|
21 |
+
|
22 |
+
# 故事 → 语音(TTS)
|
23 |
+
def text2audio_gtts(story_text, filename="story.mp3"):
|
24 |
+
# 避免文件冲突
|
25 |
+
if os.path.exists(filename):
|
26 |
+
os.remove(filename)
|
27 |
+
|
28 |
+
# 限制 TTS 文本长度
|
29 |
+
max_chars = 500 # gTTS 可能不支持过长文本
|
30 |
+
story_text = story_text[:max_chars]
|
31 |
+
|
32 |
+
# 生成语音
|
33 |
+
tts = gTTS(text=story_text, lang="en")
|
34 |
+
tts.save(filename)
|
35 |
+
|
36 |
+
return filename
|
37 |
+
|
38 |
+
# Streamlit Web UI
|
39 |
+
st.set_page_config(page_title="AI Storyteller", page_icon="📖")
|
40 |
+
st.header("📖 AI Storyteller: Turn Your Image into a Story with Audio")
|
41 |
+
|
42 |
+
uploaded_file = st.file_uploader("Upload an Image...", type=["jpg", "png"])
|
43 |
+
|
44 |
+
if uploaded_file:
|
45 |
+
# 保存图片到本地
|
46 |
+
image_path = "uploaded_image.jpg"
|
47 |
+
with open(image_path, "wb") as f:
|
48 |
+
f.write(uploaded_file.getbuffer())
|
49 |
+
|
50 |
+
# 读取并显示图片
|
51 |
+
image = Image.open(image_path)
|
52 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
53 |
+
|
54 |
+
# 生成图片描述
|
55 |
+
st.text("🔍 Generating image caption...")
|
56 |
+
caption = img2text(image_path) # 这里改成文件路径
|
57 |
+
st.write("**Image Description:**", caption)
|
58 |
+
|
59 |
+
# 生成故事
|
60 |
+
st.text("📝 Generating story...")
|
61 |
+
story = text2story(caption)
|
62 |
+
st.write("**Generated Story:**")
|
63 |
+
st.write(story)
|
64 |
+
|
65 |
+
# 生成语音
|
66 |
+
st.text("🔊 Generating audio...")
|
67 |
+
audio_file = text2audio_gtts(story)
|
68 |
+
|
69 |
+
# 播放音频
|
70 |
+
st.audio(audio_file, format="audio/mp3")
|
71 |
+
|
72 |
+
# 下载按钮
|
73 |
+
with open(audio_file, "rb") as file:
|
74 |
+
st.download_button("📥 Download Audio", file, file_name="story.mp3")
|