Spaces:
Build error
Build error
Leo Liu commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,47 +31,65 @@ def main():
|
|
| 31 |
<h4 style="color:#4ECDC4;">Upload any photo to Get a fairy tale!</h4>
|
| 32 |
</div>
|
| 33 |
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
uploaded_file = st.file_uploader("🌈 Choose your magic picture...", type=["jpg", "png"])
|
| 35 |
|
| 36 |
if uploaded_file is not None:
|
| 37 |
-
#
|
|
|
|
|
|
|
|
|
|
| 38 |
bytes_data = uploaded_file.getvalue()
|
| 39 |
with open(uploaded_file.name, "wb") as file:
|
| 40 |
file.write(bytes_data)
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
status_container = st.empty()
|
| 45 |
progress_bar = st.progress(0)
|
| 46 |
|
| 47 |
# Stage 1: Image to Text
|
| 48 |
-
with status_container.status("🔮 **Step 1/3**: Decoding picture magic...", expanded=True)
|
| 49 |
progress_bar.progress(33)
|
| 50 |
scenario = img2text(uploaded_file.name)
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
#Stage 2: Text to Story
|
| 55 |
-
with status_container.status("📚 **Step 2/3**: Writing your fairy tale..."
|
| 56 |
progress_bar.progress(66)
|
| 57 |
story = text2story(scenario)
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
#Stage 3:
|
| 62 |
-
with status_container.status("🎵 **Step 3/3**: Adding magic music..."
|
| 63 |
progress_bar.progress(100)
|
| 64 |
audio_data = text2audio(story)
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
sample_rate=audio_data['sampling_rate'],
|
| 73 |
-
autoplay=True) # 新增自动播放参数
|
| 74 |
-
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
| 77 |
main()
|
|
|
|
| 31 |
<h4 style="color:#4ECDC4;">Upload any photo to Get a fairy tale!</h4>
|
| 32 |
</div>
|
| 33 |
""", unsafe_allow_html=True)
|
| 34 |
+
|
| 35 |
+
# 新增持久化容器(关键修改)
|
| 36 |
+
result_container = st.container() # 用于存放所有生成内容
|
| 37 |
+
|
| 38 |
uploaded_file = st.file_uploader("🌈 Choose your magic picture...", type=["jpg", "png"])
|
| 39 |
|
| 40 |
if uploaded_file is not None:
|
| 41 |
+
# 清空旧内容(可选)
|
| 42 |
+
result_container.empty()
|
| 43 |
+
|
| 44 |
+
# 保存上传文件
|
| 45 |
bytes_data = uploaded_file.getvalue()
|
| 46 |
with open(uploaded_file.name, "wb") as file:
|
| 47 |
file.write(bytes_data)
|
| 48 |
+
|
| 49 |
+
# 显示图片在持久容器内
|
| 50 |
+
with result_container:
|
| 51 |
+
st.image(uploaded_file, caption="Your Magic Picture ✨", use_container_width=True)
|
| 52 |
|
| 53 |
+
# 初始化进度组件
|
| 54 |
+
status_container = st.empty()
|
| 55 |
progress_bar = st.progress(0)
|
| 56 |
|
| 57 |
# Stage 1: Image to Text
|
| 58 |
+
with status_container.status("🔮 **Step 1/3**: Decoding picture magic...", expanded=True):
|
| 59 |
progress_bar.progress(33)
|
| 60 |
scenario = img2text(uploaded_file.name)
|
| 61 |
+
with result_container: # 将结果写入持久容器
|
| 62 |
+
st.success("🔍 **What I see:**")
|
| 63 |
+
st.write(scenario)
|
| 64 |
+
progress_bar.progress(33)
|
| 65 |
|
| 66 |
+
# Stage 2: Text to Story
|
| 67 |
+
with status_container.status("📚 **Step 2/3**: Writing your fairy tale..."):
|
| 68 |
progress_bar.progress(66)
|
| 69 |
story = text2story(scenario)
|
| 70 |
+
with result_container: # 持久化故事内容
|
| 71 |
+
st.success("📖 **Your Story:**")
|
| 72 |
+
st.markdown(f"""
|
| 73 |
+
<div style="
|
| 74 |
+
background: #f0f9ff;
|
| 75 |
+
padding: 1rem;
|
| 76 |
+
border-radius: 10px;
|
| 77 |
+
font-size: 18px;
|
| 78 |
+
">{story}</div>
|
| 79 |
+
""", unsafe_allow_html=True)
|
| 80 |
+
progress_bar.progress(66)
|
| 81 |
|
| 82 |
+
# Stage 3: Audio
|
| 83 |
+
with status_container.status("🎵 **Step 3/3**: Adding magic music..."):
|
| 84 |
progress_bar.progress(100)
|
| 85 |
audio_data = text2audio(story)
|
| 86 |
+
with result_container: # 持久化音频控件
|
| 87 |
+
st.success("🎧 **Listen to your story!**")
|
| 88 |
+
st.audio(audio_data['audio'],
|
| 89 |
+
format="audio/wav",
|
| 90 |
+
start_time=0,
|
| 91 |
+
sample_rate=audio_data['sampling_rate'],
|
| 92 |
+
autoplay=True)
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
if __name__ == "__main__":
|
| 95 |
main()
|