frankai98 commited on
Commit
3a8f95c
·
verified ·
1 Parent(s): 7b4b461

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -141
app.py CHANGED
@@ -7,12 +7,12 @@ import torch
7
  from gtts import gTTS
8
  import io
9
  import time
10
- import asyncio
11
  from streamlit.components.v1 import html
 
12
 
13
  if not asyncio.get_event_loop().is_running():
14
  asyncio.set_event_loop(asyncio.new_event_loop())
15
-
16
  # Initialize session state
17
  if 'processed_data' not in st.session_state:
18
  st.session_state.processed_data = {
@@ -21,11 +21,34 @@ if 'processed_data' not in st.session_state:
21
  'audio': None
22
  }
23
 
24
- if 'image_data' not in st.session_state:
25
- st.session_state.image_data = None
26
 
27
- if 'timer_active' not in st.session_state:
28
- st.session_state.timer_active = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  # Page setup
31
  st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
@@ -66,168 +89,88 @@ def text2audio(story_text):
66
  audio_io.seek(0)
67
  return {'audio': audio_io, 'sampling_rate': 16000}
68
 
69
- # Create fixed containers for UI elements
70
- image_container = st.empty()
71
- timer_container = st.empty()
72
- status_container = st.empty()
73
- progress_container = st.empty()
74
- results_container = st.container()
75
-
76
- # JavaScript timer implementation
77
- def active_timer():
78
- timer_html = """
79
- <div style="font-size:16px;color:#666;margin-bottom:10px;">
80
- <span id="timer-display">⏱️ Elapsed: 00:00</span>
81
- </div>
82
-
83
- <script>
84
- // Reset any existing timer
85
- if (window.timerInterval) {
86
- clearInterval(window.timerInterval);
87
- window.timerInterval = null;
88
- }
89
 
90
- // Start a new timer
91
- var startTime = new Date().getTime();
92
- window.timerInterval = setInterval(function() {
93
- var now = new Date().getTime();
94
- var elapsed = now - startTime;
95
-
96
- var minutes = Math.floor(elapsed / (1000 * 60));
97
- var seconds = Math.floor((elapsed % (1000 * 60)) / 1000);
98
-
99
- var formattedTime =
100
- (minutes < 10 ? "0" : "") + minutes + ":" +
101
- (seconds < 10 ? "0" : "") + seconds;
102
-
103
- var timerElement = document.getElementById("timer-display");
104
- if (timerElement) {
105
- timerElement.innerHTML = "⏱️ Elapsed: " + formattedTime;
106
- }
107
- }, 100);
108
- </script>
109
- """
110
- return timer_html
111
-
112
- def frozen_timer(time_display):
113
- timer_html = """
114
- <div style="font-size:16px;color:#00cc00;font-weight:bold;margin-bottom:10px;">
115
- <span id="timer-display">⏱️ Elapsed: """ + time_display + """ ✓</span>
116
- </div>
117
 
118
- <script>
119
- // Clear any existing timer
120
- if (window.timerInterval) {
121
- clearInterval(window.timerInterval);
122
- window.timerInterval = null;
123
- }
124
- </script>
125
- """
126
- return timer_html
127
 
128
- def empty_timer():
129
- timer_html = """
130
- <div style="font-size:16px;color:#666;margin-bottom:10px;">
131
- <span id="timer-display">⏱️ Elapsed: 00:00</span>
132
- </div>
133
- """
134
- return timer_html
135
 
136
- # Display empty timer initially
137
- if not st.session_state.timer_active:
138
- timer_container.markdown(empty_timer(), unsafe_allow_html=True)
139
 
140
- # Always display the image if we have image data
141
- if st.session_state.image_data is not None:
142
- image_container.image(st.session_state.image_data, caption="Uploaded Image", use_container_width=True)
143
 
144
- # UI components
145
- uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
146
-
147
- # Process new uploaded file
148
- if uploaded_file is not None:
149
- # Save the image data to session state
150
- bytes_data = uploaded_file.getvalue()
151
- st.session_state.image_data = bytes_data
152
-
153
- # Display the image
154
- image_container.image(bytes_data, caption="Uploaded Image", use_container_width=True)
155
-
156
- if st.session_state.get('current_file') != uploaded_file.name:
157
- st.session_state.current_file = uploaded_file.name
158
-
159
- # Start timer
160
- st.session_state.timer_active = True
161
- timer_container.markdown(active_timer(), unsafe_allow_html=True)
162
-
163
- # Progress indicators
164
- status_text = status_container.empty()
165
- progress_bar = progress_container.progress(0)
166
-
167
- try:
168
- # Save uploaded file
169
- with open(uploaded_file.name, "wb") as file:
170
- file.write(bytes_data)
171
-
172
  # Stage 1: Image to Text
173
  status_text.markdown("**🖼️ Generating caption...**")
 
174
  st.session_state.processed_data['scenario'] = img2text(uploaded_file.name)
175
  progress_bar.progress(33)
176
-
177
  # Stage 2: Text to Story
178
  status_text.markdown("**📖 Generating story...**")
 
179
  st.session_state.processed_data['story'] = text2story(
180
  st.session_state.processed_data['scenario']
181
  )
182
  progress_bar.progress(66)
183
-
184
  # Stage 3: Story to Audio
185
  status_text.markdown("**🔊 Synthesizing audio...**")
 
186
  st.session_state.processed_data['audio'] = text2audio(
187
  st.session_state.processed_data['story']
188
  )
189
  progress_bar.progress(100)
190
-
191
- # Final status
192
  status_text.success("**✅ Generation complete!**")
193
-
194
- # Show results
195
- with results_container:
196
- st.write("**Caption:**", st.session_state.processed_data['scenario'])
197
- st.write("**Story:**", st.session_state.processed_data['story'])
198
-
199
- except Exception as e:
200
- status_text.error(f"**❌ Error:** {str(e)}")
201
- progress_bar.empty()
202
- raise e
203
-
204
- # Display results if available
205
- if st.session_state.processed_data.get('scenario'):
206
- with results_container:
207
- st.write("**Caption:**", st.session_state.processed_data['scenario'])
208
 
209
- if st.session_state.processed_data.get('story'):
210
- with results_container:
 
211
  st.write("**Story:**", st.session_state.processed_data['story'])
212
 
213
- # Audio playback with timer freezing
 
 
 
 
 
 
 
 
 
 
 
214
  if st.button("Play Audio of the Story Generated"):
 
 
 
 
 
 
 
 
 
 
 
 
215
  if st.session_state.processed_data.get('audio'):
216
- # Make sure the image is still displayed
217
- if st.session_state.image_data is not None:
218
- image_container.image(st.session_state.image_data, caption="Uploaded Image", use_container_width=True)
219
-
220
- # Freeze the timer (with a default value since we can't easily get the real time)
221
- timer_container.markdown(frozen_timer("00:30"), unsafe_allow_html=True)
222
-
223
- # Set timer as inactive since we've frozen it
224
- st.session_state.timer_active = False
225
-
226
- # Play the audio
227
  audio_data = st.session_state.processed_data['audio']
228
- st.audio(
229
- audio_data['audio'].getvalue(),
230
- format="audio/mp3"
231
- )
232
  else:
233
- st.warning("Please generate a story first!")
 
7
  from gtts import gTTS
8
  import io
9
  import time
 
10
  from streamlit.components.v1 import html
11
+ import asyncio
12
 
13
  if not asyncio.get_event_loop().is_running():
14
  asyncio.set_event_loop(asyncio.new_event_loop())
15
+
16
  # Initialize session state
17
  if 'processed_data' not in st.session_state:
18
  st.session_state.processed_data = {
 
21
  'audio': None
22
  }
23
 
 
 
24
 
25
+ # JavaScript timer component
26
+ def timer():
27
+ return """
28
+ <div id="timer" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>
29
+ <script>
30
+ function updateTimer() {
31
+ var start = Date.now();
32
+ var timerElement = document.getElementById('timer');
33
+
34
+ var interval = setInterval(function() {
35
+ var elapsed = Date.now() - start;
36
+ var minutes = Math.floor(elapsed / 60000);
37
+ var seconds = Math.floor((elapsed % 60000) / 1000);
38
+ timerElement.innerHTML = '⏱️ Elapsed: ' +
39
+ (minutes < 10 ? '0' : '') + minutes + ':' +
40
+ (seconds < 10 ? '0' : '') + seconds;
41
+ }, 1000);
42
+
43
+ // Make the cleanup function available globally
44
+ window.stopTimer = function() {
45
+ clearInterval(interval);
46
+ };
47
+ }
48
+ updateTimer();
49
+ </script>
50
+ """
51
+
52
 
53
  # Page setup
54
  st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
 
89
  audio_io.seek(0)
90
  return {'audio': audio_io, 'sampling_rate': 16000}
91
 
92
+ # UI components
93
+ uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
94
+
95
+ if uploaded_file is not None:
96
+ # Initialize progress containers
97
+ status_text = st.empty()
98
+ progress_bar = st.progress(0)
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
+ # Start JavaScript timer
101
+ html(timer(), height=50)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
 
 
 
 
 
 
 
 
 
103
 
104
+ try:
105
+ # Save uploaded file
106
+ bytes_data = uploaded_file.getvalue()
107
+ with open(uploaded_file.name, "wb") as file:
108
+ file.write(bytes_data)
 
 
109
 
110
+ if st.session_state.get('current_file') != uploaded_file.name:
111
+ st.session_state.current_file = uploaded_file.name
 
112
 
113
+ # Display image
114
+ st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
 
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  # Stage 1: Image to Text
117
  status_text.markdown("**🖼️ Generating caption...**")
118
+ progress_bar.progress(0)
119
  st.session_state.processed_data['scenario'] = img2text(uploaded_file.name)
120
  progress_bar.progress(33)
121
+
122
  # Stage 2: Text to Story
123
  status_text.markdown("**📖 Generating story...**")
124
+ progress_bar.progress(33)
125
  st.session_state.processed_data['story'] = text2story(
126
  st.session_state.processed_data['scenario']
127
  )
128
  progress_bar.progress(66)
129
+
130
  # Stage 3: Story to Audio
131
  status_text.markdown("**🔊 Synthesizing audio...**")
132
+ progress_bar.progress(66)
133
  st.session_state.processed_data['audio'] = text2audio(
134
  st.session_state.processed_data['story']
135
  )
136
  progress_bar.progress(100)
137
+
138
+ # Final status and stop timer
139
  status_text.success("**✅ Generation complete!**")
140
+ html("<script>document.getElementById('timer').style.color = '#00cc00';</script>")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
 
142
+ # Show results
143
+ # st.subheader("Results")
144
+ st.write("**Caption:**", st.session_state.processed_data['scenario'])
145
  st.write("**Story:**", st.session_state.processed_data['story'])
146
 
147
+ except Exception as e:
148
+ html("<script>document.getElementById('timer').remove();</script>")
149
+ status_text.error(f"**❌ Error:** {str(e)}")
150
+ progress_bar.empty()
151
+ raise e
152
+
153
+
154
+ finally:
155
+ pass # Timer cleanup handled by JavaScript
156
+
157
+
158
+ # Audio playback
159
  if st.button("Play Audio of the Story Generated"):
160
+ # Stop the timer
161
+ st.markdown(
162
+ """
163
+ <script>
164
+ if (window.stopTimer) {
165
+ window.stopTimer();
166
+ }
167
+ </script>
168
+ """,
169
+ unsafe_allow_html=True
170
+ )
171
+
172
  if st.session_state.processed_data.get('audio'):
 
 
 
 
 
 
 
 
 
 
 
173
  audio_data = st.session_state.processed_data['audio']
174
+ st.audio(audio_data['audio'].getvalue(), format="audio/mp3")
 
 
 
175
  else:
176
+ st.warning("Please generate a story first!")