TLH01 commited on
Commit
d55766c
·
verified ·
1 Parent(s): a1ad6c4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +144 -0
app.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ 儿童故事生成器 (Children's Story Generator)
3
+ 功能:上传图片 → 生成描述 → 创作故事 → 语音朗读
4
+ """
5
+
6
+ # ============ 导入模块 ============
7
+ import streamlit as st
8
+ from PIL import Image
9
+ import tempfile
10
+ from transformers import pipeline
11
+ import torch
12
+ import os
13
+
14
+ # ============ 第一阶段:图片描述生成 ============
15
+ @st.cache_resource # 缓存模型避免重复加载
16
+ def load_image_captioner():
17
+ """加载图片描述模型(BLIP模型)"""
18
+ return pipeline(
19
+ "image-to-text",
20
+ model="Salesforce/blip-image-captioning-base",
21
+ device="cuda" if torch.cuda.is_available() else "cpu" # 自动检测GPU
22
+ )
23
+
24
+ def generate_caption(_pipeline, image):
25
+ """生成图片英文描述"""
26
+ try:
27
+ result = _pipeline(image, max_new_tokens=50) # 限制生成长度
28
+ return result[0]['generated_text']
29
+ except Exception as e:
30
+ st.error(f"生成描述失败: {str(e)}")
31
+ return None
32
+
33
+ # ============ 第二阶段:故事创作 ============
34
+ @st.cache_resource
35
+ def load_story_generator():
36
+ """加载儿童故事生成模型"""
37
+ return pipeline(
38
+ "text-generation",
39
+ model="pranavpsv/gpt2-genre-story-generator",
40
+ device="cuda" if torch.cuda.is_available() else "cpu"
41
+ )
42
+
43
+ def generate_story(_pipeline, keywords):
44
+ """根据关键词生成儿童故事"""
45
+ prompt = f"""Generate a children's story (60-80 words) in English about: {keywords}
46
+ Requirements:
47
+ - Use simple words
48
+ - Include magical elements
49
+ - Happy ending
50
+ Story:"""
51
+
52
+ try:
53
+ story = _pipeline(
54
+ prompt,
55
+ max_length=200,
56
+ temperature=0.7 # 控制创意程度
57
+ )[0]['generated_text']
58
+ return story.replace(prompt, "").strip()
59
+ except Exception as e:
60
+ st.error(f"生成故事失败: {str(e)}")
61
+ return None
62
+
63
+ # ============ 第三阶段:语音合成 ============
64
+ @st.cache_resource
65
+ def load_tts():
66
+ """加载文本转语音模型"""
67
+ return pipeline(
68
+ "text-to-speech",
69
+ model="facebook/mms-tts-eng",
70
+ device="cuda" if torch.cuda.is_available() else "cpu"
71
+ )
72
+
73
+ def text_to_speech(_pipeline, text):
74
+ """将文本转为语音"""
75
+ try:
76
+ audio = _pipeline(text)
77
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
78
+ import soundfile as sf
79
+ sf.write(f.name, audio["audio"].squeeze().numpy(), audio["sampling_rate"])
80
+ return f.name
81
+ except Exception as e:
82
+ st.error(f"语音生成失败: {str(e)}")
83
+ return None
84
+
85
+ # ============ 主界面 ============
86
+ def main():
87
+ # 界面设置
88
+ st.set_page_config(
89
+ page_title="魔法故事生成器",
90
+ page_icon="🧚",
91
+ layout="wide"
92
+ )
93
+
94
+ # 儿童风格CSS
95
+ st.markdown("""
96
+ <style>
97
+ .main { background-color: #FFF5E6 }
98
+ h1 { color: #FF6B6B; font-family: 'Comic Sans MS' }
99
+ .stButton>button { background-color: #4CAF50; border-radius: 20px }
100
+ </style>
101
+ """, unsafe_allow_html=True)
102
+
103
+ st.title("🧚 魔法故事生成器")
104
+ st.write("上传小朋友的照片,AI会生成专属故事并朗读!")
105
+
106
+ # 图片上传
107
+ uploaded_file = st.file_uploader("选择照片", type=["jpg", "png"])
108
+
109
+ if not uploaded_file:
110
+ st.info("请先上传照片")
111
+ return
112
+
113
+ image = Image.open(uploaded_file)
114
+ st.image(image, use_column_width=True)
115
+
116
+ # 加载模型
117
+ with st.spinner("正在准备魔法..."):
118
+ caption_pipe = load_image_captioner()
119
+ story_pipe = load_story_generator()
120
+ tts_pipe = load_tts()
121
+
122
+ # 第一阶段
123
+ with st.spinner("正在分析图片..."):
124
+ caption = generate_caption(caption_pipe, image)
125
+ if caption:
126
+ st.success(f"图片描述: {caption}")
127
+
128
+ # 第二阶段
129
+ if caption:
130
+ with st.spinner("正在创作故事..."):
131
+ story = generate_story(story_pipe, caption)
132
+ if story:
133
+ st.subheader("你的故事")
134
+ st.markdown(f'<div style="background-color:#FFF0F5; padding:20px; border-radius:15px">{story}</div>', unsafe_allow_html=True)
135
+
136
+ # 第三阶段
137
+ with st.spinner("正在生成语音..."):
138
+ audio_path = text_to_speech(tts_pipe, story)
139
+ if audio_path:
140
+ st.audio(audio_path, format="audio/wav")
141
+
142
+ if __name__ == "__main__":
143
+ os.environ["HF_HUB_CACHE"] = "/tmp/huggingface" # 设置缓存路径
144
+ main()