frankai98 commited on
Commit
89a5c7c
·
verified ·
1 Parent(s): c337535

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -13
app.py CHANGED
@@ -24,6 +24,9 @@ if 'processed_data' not in st.session_state:
24
  if 'image_data' not in st.session_state:
25
  st.session_state.image_data = None
26
 
 
 
 
27
  # Page setup
28
  st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
29
  st.header("Turn Your Image to a Short Audio Story for Children")
@@ -70,11 +73,15 @@ status_container = st.empty()
70
  progress_container = st.empty()
71
  results_container = st.container()
72
 
73
- # JavaScript timer that continues to run until frozen
 
 
 
 
 
 
74
  def start_timer():
75
  timer_html = """
76
- <div id="timer-display" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>
77
-
78
  <script>
79
  // Only create a timer if one doesn't exist already
80
  if (!window.timerInterval) {
@@ -110,7 +117,7 @@ def start_timer():
110
  }
111
  </script>
112
  """
113
- return html(timer_html, height=50)
114
 
115
  def freeze_timer():
116
  freeze_html = """
@@ -138,7 +145,7 @@ def freeze_timer():
138
  localStorage.setItem('timerRunning', 'false');
139
  </script>
140
  """
141
- return html(freeze_html, height=0)
142
 
143
  def reset_timer():
144
  reset_html = """
@@ -165,10 +172,7 @@ def reset_timer():
165
  }
166
  </script>
167
  """
168
- return html(reset_html, height=0)
169
-
170
- # Always show timer container
171
- timer_container.write(start_timer())
172
 
173
  # Always display the image if we have image data
174
  if st.session_state.image_data is not None:
@@ -191,7 +195,8 @@ if uploaded_file is not None:
191
 
192
  # Reset and start timer
193
  reset_timer()
194
- timer_container.write(start_timer())
 
195
 
196
  # Progress indicators
197
  status_text = status_container.empty()
@@ -233,6 +238,10 @@ if uploaded_file is not None:
233
  status_text.error(f"**❌ Error:** {str(e)}")
234
  progress_bar.empty()
235
  raise e
 
 
 
 
236
 
237
  # Display results if available
238
  if st.session_state.processed_data.get('scenario'):
@@ -248,10 +257,11 @@ if st.button("Play Audio of the Story Generated"):
248
  if st.session_state.processed_data.get('audio'):
249
  # Make sure the image is still displayed
250
  if st.session_state.image_data is not None:
251
- image_container.image(st.session_state.image_data, caption="Uploaded Image", use_column_width=True)
252
 
253
- # Freeze the timer
254
- freeze_timer()
 
255
 
256
  # Play the audio
257
  audio_data = st.session_state.processed_data['audio']
 
24
  if 'image_data' not in st.session_state:
25
  st.session_state.image_data = None
26
 
27
+ if 'timer_started' not in st.session_state:
28
+ st.session_state.timer_started = False
29
+
30
  # Page setup
31
  st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
32
  st.header("Turn Your Image to a Short Audio Story for Children")
 
73
  progress_container = st.empty()
74
  results_container = st.container()
75
 
76
+ # Display initial timer placeholder (empty timer)
77
+ timer_container.markdown(
78
+ '<div id="timer-display" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>',
79
+ unsafe_allow_html=True
80
+ )
81
+
82
+ # JavaScript timer functions
83
  def start_timer():
84
  timer_html = """
 
 
85
  <script>
86
  // Only create a timer if one doesn't exist already
87
  if (!window.timerInterval) {
 
117
  }
118
  </script>
119
  """
120
+ html(timer_html, height=0)
121
 
122
  def freeze_timer():
123
  freeze_html = """
 
145
  localStorage.setItem('timerRunning', 'false');
146
  </script>
147
  """
148
+ html(freeze_html, height=0)
149
 
150
  def reset_timer():
151
  reset_html = """
 
172
  }
173
  </script>
174
  """
175
+ html(reset_html, height=0)
 
 
 
176
 
177
  # Always display the image if we have image data
178
  if st.session_state.image_data is not None:
 
195
 
196
  # Reset and start timer
197
  reset_timer()
198
+ start_timer() # Start the timer ONLY when a new file is uploaded
199
+ st.session_state.timer_started = True
200
 
201
  # Progress indicators
202
  status_text = status_container.empty()
 
238
  status_text.error(f"**❌ Error:** {str(e)}")
239
  progress_bar.empty()
240
  raise e
241
+ # If we have a previously started timer but a new page load, restart it
242
+ elif st.session_state.timer_started:
243
+ # Don't reset, just restart the timer to continue from where it was
244
+ start_timer()
245
 
246
  # Display results if available
247
  if st.session_state.processed_data.get('scenario'):
 
257
  if st.session_state.processed_data.get('audio'):
258
  # Make sure the image is still displayed
259
  if st.session_state.image_data is not None:
260
+ image_container.image(st.session_state.image_data, caption="Uploaded Image", use_container_width=True)
261
 
262
+ # Freeze the timer if it was started
263
+ if st.session_state.timer_started:
264
+ freeze_timer()
265
 
266
  # Play the audio
267
  audio_data = st.session_state.processed_data['audio']