frankai98 commited on
Commit
af0fd83
·
verified ·
1 Parent(s): a1f1fd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -23,7 +23,7 @@ if 'timer_started' not in st.session_state:
23
  if 'timer_frozen' not in st.session_state:
24
  st.session_state.timer_frozen = False
25
 
26
- # JavaScript timer component (defines window.myTimerInterval for later stopping)
27
  def timer():
28
  return """
29
  <div id="timer" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>
@@ -31,6 +31,7 @@ def timer():
31
  (function() {
32
  var start = Date.now();
33
  var timerElement = document.getElementById('timer');
 
34
  window.myTimerInterval = setInterval(function() {
35
  var elapsed = Date.now() - start;
36
  var minutes = Math.floor(elapsed / 60000);
@@ -39,6 +40,12 @@ def timer():
39
  (minutes < 10 ? '0' : '') + minutes + ':' +
40
  (seconds < 10 ? '0' : '') + seconds;
41
  }, 1000);
 
 
 
 
 
 
42
  })();
43
  </script>
44
  """
@@ -86,7 +93,7 @@ def text2audio(story_text):
86
  uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
87
 
88
  if uploaded_file is not None:
89
- # Only inject the timer once after an image is uploaded
90
  if not st.session_state.timer_started and not st.session_state.timer_frozen:
91
  st.session_state.timer_started = True
92
  html(timer(), height=50)
@@ -128,10 +135,11 @@ if uploaded_file is not None:
128
  st.session_state.processed_data['story']
129
  )
130
  progress_bar.progress(100)
131
- html("<script>if(window.myTimerInterval){ clearInterval(window.myTimerInterval); }</script>", height=0)
132
  status_text.success("**✅ Generation complete!**")
133
- html("<script>document.getElementById('timer').style.color = '#00cc00';</script>")
134
-
 
135
  except Exception as e:
136
  html("<script>document.getElementById('timer').remove();</script>")
137
  status_text.error(f"**❌ Error:** {str(e)}")
@@ -141,15 +149,9 @@ if uploaded_file is not None:
141
  st.write("**Caption:**", st.session_state.processed_data['scenario'])
142
  st.write("**Story:**", st.session_state.processed_data['story'])
143
 
144
- # Audio playback and timer freeze
145
  if st.button("Play Audio of the Story Generated"):
146
  if st.session_state.processed_data.get('audio'):
147
- # Inject JS to stop the timer and freeze it
148
- st.markdown(
149
- "<script>if(window.myTimerInterval){ clearInterval(window.myTimerInterval); }</script>",
150
- unsafe_allow_html=True
151
- )
152
- st.session_state.timer_frozen = True
153
  audio_data = st.session_state.processed_data['audio']
154
  st.audio(audio_data['audio'].getvalue(), format="audio/mp3")
155
  else:
 
23
  if 'timer_frozen' not in st.session_state:
24
  st.session_state.timer_frozen = False
25
 
26
+ # Timer component with explicit freeze function
27
  def timer():
28
  return """
29
  <div id="timer" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>
 
31
  (function() {
32
  var start = Date.now();
33
  var timerElement = document.getElementById('timer');
34
+ // Start the timer and store the interval ID globally.
35
  window.myTimerInterval = setInterval(function() {
36
  var elapsed = Date.now() - start;
37
  var minutes = Math.floor(elapsed / 60000);
 
40
  (minutes < 10 ? '0' : '') + minutes + ':' +
41
  (seconds < 10 ? '0' : '') + seconds;
42
  }, 1000);
43
+ // Expose a function to freeze the timer.
44
+ window.freezeTimer = function() {
45
+ clearInterval(window.myTimerInterval);
46
+ // Change the timer color to indicate it’s frozen.
47
+ timerElement.style.color = '#00cc00';
48
+ };
49
  })();
50
  </script>
51
  """
 
93
  uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
94
 
95
  if uploaded_file is not None:
96
+ # Only inject the timer once after an image is uploaded (if not already frozen).
97
  if not st.session_state.timer_started and not st.session_state.timer_frozen:
98
  st.session_state.timer_started = True
99
  html(timer(), height=50)
 
135
  st.session_state.processed_data['story']
136
  )
137
  progress_bar.progress(100)
138
+
139
  status_text.success("**✅ Generation complete!**")
140
+ # Freeze the timer immediately now that generation is complete.
141
+ html("<script>if(window.freezeTimer){ window.freezeTimer(); }</script>", height=0)
142
+ st.session_state.timer_frozen = True # Prevent re-injecting the timer.
143
  except Exception as e:
144
  html("<script>document.getElementById('timer').remove();</script>")
145
  status_text.error(f"**❌ Error:** {str(e)}")
 
149
  st.write("**Caption:**", st.session_state.processed_data['scenario'])
150
  st.write("**Story:**", st.session_state.processed_data['story'])
151
 
 
152
  if st.button("Play Audio of the Story Generated"):
153
  if st.session_state.processed_data.get('audio'):
154
+ # Since the timer is already frozen, simply play the audio.
 
 
 
 
 
155
  audio_data = st.session_state.processed_data['audio']
156
  st.audio(audio_data['audio'].getvalue(), format="audio/mp3")
157
  else: