Leo Liu commited on
Commit
4ae104f
·
verified ·
1 Parent(s): cd3773c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -41
app.py CHANGED
@@ -2,6 +2,26 @@
2
  import streamlit as st
3
  from transformers import pipeline
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  # function part
6
  # img2text
7
  def img2text(url):
@@ -31,65 +51,45 @@ def 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()
 
2
  import streamlit as st
3
  from transformers import pipeline
4
 
5
+ st.markdown("""
6
+ <style>
7
+ @import url('https://fonts.googleapis.com/css2?family=Comic+Neue:wght@700&display=swap');
8
+ .header {
9
+ background-image: url('https://i.imgur.com/6GEdzVp.png');
10
+ background-size: cover;
11
+ border-radius: 15px;
12
+ padding: 2rem;
13
+ text-align: center;
14
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
15
+ }
16
+ .header h1 {
17
+ color: #FF6B6B;
18
+ font-family: 'Comic Neue', cursive;
19
+ font-size: 2.5rem;
20
+ text-shadow: 2px 2px #FFF;
21
+ }
22
+ </style>
23
+ """, unsafe_allow_html=True)
24
+
25
  # function part
26
  # img2text
27
  def img2text(url):
 
51
  <h4 style="color:#4ECDC4;">Upload any photo to Get a fairy tale!</h4>
52
  </div>
53
  """, unsafe_allow_html=True)
 
 
 
 
54
  uploaded_file = st.file_uploader("🌈 Choose your magic picture...", type=["jpg", "png"])
55
 
56
  if uploaded_file is not None:
57
+ # 保存上传文件(原有逻辑)
 
 
 
58
  bytes_data = uploaded_file.getvalue()
59
  with open(uploaded_file.name, "wb") as file:
60
  file.write(bytes_data)
61
+ st.image(uploaded_file, caption="Your Magic Picture ✨", use_container_width=True)
 
 
 
62
 
63
+ # 初始化状态容器(修复点:移出if块)
64
+ status_container = st.empty() # 移动到此处
65
  progress_bar = st.progress(0)
66
 
67
  # Stage 1: Image to Text
68
+ with status_container.status("🔮 **Step 1/3**: Decoding picture magic...", expanded=True) as status: # 保持缩进
69
  progress_bar.progress(33)
70
  scenario = img2text(uploaded_file.name)
71
+ status.update(label="✅ Picture decoded!", state="complete")
72
+ st.write(f"**What I see:** {scenario}")
 
 
73
 
74
+ #Stage 2: Text to Story
75
+ with status_container.status("📚 **Step 2/3**: Writing your fairy tale...", expanded=True) as status:
76
  progress_bar.progress(66)
77
  story = text2story(scenario)
78
+ status.update(label="✅ Story created!", state="complete")
79
+ st.write(f"**Your Story:**\n{story}")
 
 
 
 
 
 
 
 
 
80
 
81
+ #Stage 3: Story to Audio data
82
+ with status_container.status("🎵 **Step 3/3**: Adding magic music...", expanded=True) as status:
83
  progress_bar.progress(100)
84
  audio_data = text2audio(story)
85
+ status.update(label="✅ All ready!", state="complete")
86
+
87
+ # Auto-play the audio
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()