Leo0129 commited on
Commit
0a62714
·
verified ·
1 Parent(s): 60aafe0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +160 -0
app.py ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import part
2
+ import streamlit as st
3
+ from transformers import pipeline
4
+
5
+ # 儿童友好主题配置
6
+ def set_child_theme():
7
+ st.markdown("""
8
+ <style>
9
+ .stApp {
10
+ background-color: #F9F3E6;
11
+ }
12
+ .stButton>button {
13
+ background-color: #FFB6C1 !important;
14
+ color: white !important;
15
+ border-radius: 20px !important;
16
+ padding: 10px 24px !important;
17
+ font-size: 20px !important;
18
+ font-family: 'Comic Sans MS';
19
+ }
20
+ h1, h2, h3 {
21
+ color: #6A4C93 !important;
22
+ font-family: 'Comic Sans MS';
23
+ }
24
+ .st-emotion-cache-1kyxreq {
25
+ justify-content: center;
26
+ }
27
+ </style>
28
+ """, unsafe_allow_html=True)
29
+
30
+ # function part
31
+ # img2text
32
+ def img2text(url):
33
+ image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
34
+ text = image_to_text_model(url)[0]["generated_text"]
35
+ return text
36
+
37
+ # text2story(儿童内容优化版)
38
+ def text2story(scenario):
39
+ child_prompt = f"Create a children's story suitable for 3-10 years old about {scenario}. " \
40
+ "Include talking animals and magical elements. " \
41
+ "Use simple words and happy ending. " \
42
+ "Story structure: Beginning -> Problem -> Solution -> Happy ending.\n\n"
43
+
44
+ pipe = pipeline("text-generation",
45
+ model="pranavpsv/genre-story-generator-v2",
46
+ max_new_tokens=200,
47
+ truncation=True,
48
+ temperature=0.7, # 降低随机性
49
+ top_p=0.9,
50
+ repetition_penalty=1.2)
51
+
52
+ full_story = pipe(child_prompt + scenario,
53
+ do_sample=True,
54
+ num_return_sequences=1)[0]['generated_text']
55
+
56
+ # 过滤不合适内容
57
+ safe_story = full_story.replace("violence", "").replace("scary", "").replace("died", "solved")
58
+
59
+ # 确保以快乐结局结束
60
+ happy_endings = ["happily ever after", "big smile", "learned a good lesson"]
61
+ if not any(ending in safe_story.lower() for ending in happy_endings):
62
+ safe_story += " And they all lived happily ever after!"
63
+
64
+ # 添加表情符号装饰
65
+ return "🌈 " + safe_story[:safe_story.rfind(".")+1] + " 🎉"
66
+
67
+ # text2audio(儿童语音优化)
68
+ def text2audio(story_text):
69
+ pipe = pipeline("text-to-audio",
70
+ model="Matthijs/mms-tts-eng",
71
+ vocoder="edresson/wavegrad-ljspeech")
72
+ audio_data = pipe(story_text,
73
+ # 调整语音参数
74
+ speaker_embeddings={"pitch_scale": 1.5, "energy_scale": 1.2})
75
+ return audio_data
76
+
77
+ def main():
78
+ set_child_theme()
79
+ st.set_page_config(page_title="Magic Story Maker", page_icon="🧚")
80
+
81
+ st.title("🧚✨ Magic Picture Story Maker ✨🧚")
82
+
83
+ with st.expander("👀 How to use?"):
84
+ st.write("""
85
+ 1. Upload any picture (pets, toys, drawings)
86
+ 2. Watch magic describe the picture
87
+ 3. Get a magical story
88
+ 4. Listen to the fairy tale!
89
+ """)
90
+
91
+ uploaded_file = st.file_uploader("📷 Take a photo of your favorite thing...", type=["jpg", "png"])
92
+
93
+ if uploaded_file is not None:
94
+ # 显示上传的图片(圆形裁剪)
95
+ st.markdown(f"""
96
+ <div style="display: flex; justify-content: center;">
97
+ <img src="data:image/png;base64,{uploaded_file.getvalue().encode("base64").decode()}"
98
+ style="border-radius: 50%; width: 200px; height: 200px; object-fit: cover;">
99
+ </div>
100
+ """, unsafe_allow_html=True)
101
+
102
+ # Processing stages
103
+ stages = {
104
+ "🔮 Reading Magic Picture...": img2text,
105
+ "📖 Writing Fairy Tale...": text2story,
106
+ "🎧 Preparing Story Song...": text2audio
107
+ }
108
+
109
+ results = {}
110
+ current_stage = None
111
+ try:
112
+ for stage_name, stage_func in stages.items():
113
+ current_stage = stage_name
114
+ with st.spinner(f"{stage_name}"):
115
+ if stage_name == "🔮 Reading Magic Picture...":
116
+ results["scenario"] = stage_func(uploaded_file.name)
117
+ elif stage_name == "📖 Writing Fairy Tale...":
118
+ results["story"] = stage_func(results["scenario"])
119
+ else:
120
+ results["audio"] = stage_func(results["story"])
121
+
122
+ # 添加阶段完成动画
123
+ st.success(f"✅ {stage_name.replace('...','')} Complete! ✨")
124
+
125
+ except Exception as e:
126
+ st.error(f"Oops! Magic wand broke at {current_stage} 🪄💥 Please try again!")
127
+ return
128
+
129
+ # 结果显示
130
+ st.balloons()
131
+ with st.container():
132
+ st.subheader("🧚 Your Magic Story")
133
+ st.markdown(f"""
134
+ <div style="
135
+ background: white;
136
+ padding: 20px;
137
+ border-radius: 15px;
138
+ box-shadow: 0 4px 6px rgba(0,0,0,0.1);
139
+ font-family: 'Comic Sans MS';
140
+ font-size: 18px;
141
+ color: #4B4453;
142
+ ">
143
+ {results['story']}
144
+ </div>
145
+ """, unsafe_allow_html=True)
146
+
147
+ # 自动播放音频
148
+ st.subheader("🎶 Story Song")
149
+ st.audio(results['audio']['audio'],
150
+ format="audio/wav",
151
+ sample_rate=results['audio']['sampling_rate'])
152
+
153
+ # 下载按钮
154
+ st.download_button("📥 Save Fairy Tale",
155
+ results['story'],
156
+ file_name="magic_story.txt",
157
+ mime="text/plain")
158
+
159
+ if __name__ == "__main__":
160
+ main()