Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,67 +5,63 @@ from gtts import gTTS
|
|
5 |
import os
|
6 |
os.system("pip install transformers==4.36.2")
|
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 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
# Save image locally | 保存圖片到本地
|
66 |
-
image_path = "uploaded_image.jpg"
|
67 |
-
with open(image_path, "wb") as f:
|
68 |
-
f.write(uploaded_file.getbuffer())
|
69 |
|
70 |
# Load and Display Image | 讀取並顯示圖片
|
71 |
image = Image.open(image_path)
|
@@ -92,3 +88,7 @@ if uploaded_file:
|
|
92 |
# Download Audio | 下載按钮
|
93 |
with open(audio_file, "rb") as file:
|
94 |
st.download_button("📥 Download Audio", file, file_name="story.mp3")
|
|
|
|
|
|
|
|
|
|
5 |
import os
|
6 |
os.system("pip install transformers==4.36.2")
|
7 |
|
8 |
+
def main():
|
9 |
+
st.set_page_config(page_title="AI Storyteller", page_icon="📖")
|
10 |
+
|
11 |
+
# Beautify | UI 美化
|
12 |
+
page_bg_img = """
|
13 |
+
<style>
|
14 |
+
[data-testid="stAppViewContainer"] {
|
15 |
+
background-image: url("https://your-background-image-url.com");
|
16 |
+
background-size: cover;
|
17 |
+
}
|
18 |
+
h1, h3 {
|
19 |
+
text-align: center;
|
20 |
+
color: white;
|
21 |
+
}
|
22 |
+
</style>
|
23 |
+
"""
|
24 |
+
st.markdown(page_bg_img, unsafe_allow_html=True)
|
25 |
+
|
26 |
+
# 🎯 Load Hugging Face Models | 加載 Hugging Face 模型
|
27 |
+
image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
28 |
+
story_generator = pipeline("text-generation", model="facebook/opt-350m", device=-1)
|
29 |
+
|
30 |
+
# Image to Text (Generate Caption) | 圖片 → 文字(生成描述)
|
31 |
+
def img2text(image_path):
|
32 |
+
text = image_to_text_model(image_path)[0]["generated_text"]
|
33 |
+
return text
|
34 |
+
|
35 |
+
# Text to Story (Generate a Complete Story) | 文字 → 故事(生成完整故事)
|
36 |
+
def text2story(text):
|
37 |
+
prompt = f"Write a fun and magical children's story based on this idea: {text}.\n\nOnce upon a time..."
|
38 |
+
story = story_generator(prompt, max_length=300, do_sample=True, temperature=0.8, top_p=0.9, repetition_penalty=1.2)[0]['generated_text']
|
39 |
+
return story
|
40 |
+
|
41 |
+
# Story to Speech (TTS) | 故事 → 語音(TTS)
|
42 |
+
def text2audio_gtts(story_text, filename="story.mp3"):
|
43 |
+
# Avoid filename conflicts | 避免文件冲突
|
44 |
+
if os.path.exists(filename):
|
45 |
+
os.remove(filename)
|
46 |
+
# Limit text length (to prevent gTTS crashes)| 限制 TTS 文本長度(避免 gTTS 崩潰)
|
47 |
+
max_chars = 500 # gTTS 可能不支持過長文本
|
48 |
+
story_text = story_text[:max_chars]
|
49 |
+
|
50 |
+
# Generate Speech | 生成语音
|
51 |
+
tts = gTTS(text=story_text, lang="en")
|
52 |
+
tts.save(filename)
|
53 |
+
return filename
|
54 |
+
|
55 |
+
# Streamlit Web UI
|
56 |
+
st.header("📖 AI Storyteller: Turn Your Image into a Story with Audio")
|
57 |
+
|
58 |
+
uploaded_file = st.file_uploader("Upload an Image...", type=["jpg", "png"])
|
59 |
+
|
60 |
+
if uploaded_file:
|
61 |
+
# Save image locally | 保存圖片到本地
|
62 |
+
image_path = "uploaded_image.jpg"
|
63 |
+
with open(image_path, "wb") as f:
|
64 |
+
f.write(uploaded_file.getbuffer())
|
|
|
|
|
|
|
|
|
65 |
|
66 |
# Load and Display Image | 讀取並顯示圖片
|
67 |
image = Image.open(image_path)
|
|
|
88 |
# Download Audio | 下載按钮
|
89 |
with open(audio_file, "rb") as file:
|
90 |
st.download_button("📥 Download Audio", file, file_name="story.mp3")
|
91 |
+
|
92 |
+
# 🆕 加入 Python 標準入口檢查,讓程式從 main() 開始執行
|
93 |
+
if __name__ == "__main__":
|
94 |
+
main()
|