frankai98 commited on
Commit
2521826
·
verified ·
1 Parent(s): 492133f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -41
app.py CHANGED
@@ -24,6 +24,12 @@ 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")
@@ -65,15 +71,20 @@ def text2audio(story_text):
65
 
66
  # Create fixed containers for UI elements
67
  image_container = st.empty()
 
68
  status_container = st.empty()
69
  progress_container = st.empty()
70
  results_container = st.container()
71
 
72
- # JavaScript timer component - using streamlit.components.v1.html
73
- def start_timer():
74
  timer_html = """
75
  <div id="timer" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>
76
  <script>
 
 
 
 
77
  var startTime = new Date().getTime();
78
  var timerInterval = setInterval(function() {
79
  var now = new Date().getTime();
@@ -81,42 +92,41 @@ def start_timer():
81
  var minutes = Math.floor(elapsed / (1000 * 60));
82
  var seconds = Math.floor((elapsed % (1000 * 60)) / 1000);
83
 
84
- document.getElementById("timer").innerHTML = "⏱️ Elapsed: " +
85
- (minutes < 10 ? "0" : "") + minutes + ":" +
86
- (seconds < 10 ? "0" : "") + seconds;
 
 
 
87
  }, 1000);
88
 
89
- // Store the interval ID in window object
90
- window.myTimerInterval = timerInterval;
 
91
  </script>
92
  """
93
  return html(timer_html, height=50)
94
 
95
- def stop_timer():
96
- stop_html = """
97
  <script>
98
- // Stop the timer interval
99
- if (window.myTimerInterval) {
100
- clearInterval(window.myTimerInterval);
101
- window.myTimerInterval = null;
102
- }
103
-
104
- // Get the current timer text and freeze it
105
- var timerEl = document.getElementById("timer");
106
- if (timerEl) {
107
- // Save the current time display
108
- var currentTimeDisplay = timerEl.innerText;
109
-
110
- // Apply completion styling
111
- timerEl.style.color = "#00cc00";
112
- timerEl.style.fontWeight = "bold";
113
 
114
- // Make sure the text doesn't change anymore
115
- timerEl.innerText = currentTimeDisplay + "";
 
 
 
 
 
116
  }
117
  </script>
118
  """
119
- return html(stop_html, height=0)
120
 
121
  # UI components
122
  uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
@@ -125,15 +135,6 @@ uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...
125
  if st.session_state.image_data is not None:
126
  image_container.image(st.session_state.image_data, caption="Uploaded Image", use_container_width=True)
127
 
128
- # Display results if available
129
- if st.session_state.processed_data.get('scenario'):
130
- with results_container:
131
- st.write("**Caption:**", st.session_state.processed_data['scenario'])
132
-
133
- if st.session_state.processed_data.get('story'):
134
- with results_container:
135
- st.write("**Story:**", st.session_state.processed_data['story'])
136
-
137
  # Process new uploaded file
138
  if uploaded_file is not None:
139
  # Save the image data to session state
@@ -145,9 +146,12 @@ if uploaded_file is not None:
145
 
146
  if st.session_state.get('current_file') != uploaded_file.name:
147
  st.session_state.current_file = uploaded_file.name
 
 
148
 
149
- # Start timer using v1.html
150
- timer_placeholder = start_timer()
 
151
 
152
  # Progress indicators
153
  status_text = status_container.empty()
@@ -183,8 +187,8 @@ if uploaded_file is not None:
183
  # Final status
184
  status_text.success("**✅ Generation complete!**")
185
 
186
- # Stop timer
187
- stop_timer()
188
 
189
  # Show results
190
  with results_container:
@@ -192,18 +196,32 @@ if uploaded_file is not None:
192
  st.write("**Story:**", st.session_state.processed_data['story'])
193
 
194
  except Exception as e:
195
- stop_timer()
196
  status_text.error(f"**❌ Error:** {str(e)}")
197
  progress_bar.empty()
198
  raise e
199
 
 
 
 
 
 
 
 
 
 
200
  # Audio playback
201
  if st.button("Play Audio of the Story Generated"):
202
  if st.session_state.processed_data.get('audio'):
203
  # Make sure the image is still displayed
204
  if st.session_state.image_data is not None:
205
  image_container.image(st.session_state.image_data, caption="Uploaded Image", use_container_width=True)
 
 
 
 
 
206
 
 
207
  audio_data = st.session_state.processed_data['audio']
208
  st.audio(
209
  audio_data['audio'].getvalue(),
 
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
+ if 'timer_to_freeze' not in st.session_state:
31
+ st.session_state.timer_to_freeze = False
32
+
33
  # Page setup
34
  st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
35
  st.header("Turn Your Image to a Short Audio Story for Children")
 
71
 
72
  # Create fixed containers for UI elements
73
  image_container = st.empty()
74
+ timer_container = st.empty()
75
  status_container = st.empty()
76
  progress_container = st.empty()
77
  results_container = st.container()
78
 
79
+ # JavaScript timer component
80
+ def active_timer():
81
  timer_html = """
82
  <div id="timer" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>
83
  <script>
84
+ // Generate a unique ID for this timer instance
85
+ var timerId = 'timer_' + Date.now();
86
+
87
+ // Initialize the timer
88
  var startTime = new Date().getTime();
89
  var timerInterval = setInterval(function() {
90
  var now = new Date().getTime();
 
92
  var minutes = Math.floor(elapsed / (1000 * 60));
93
  var seconds = Math.floor((elapsed % (1000 * 60)) / 1000);
94
 
95
+ var timerElement = document.getElementById("timer");
96
+ if (timerElement) {
97
+ timerElement.innerHTML = "⏱️ Elapsed: " +
98
+ (minutes < 10 ? "0" : "") + minutes + ":" +
99
+ (seconds < 10 ? "0" : "") + seconds;
100
+ }
101
  }, 1000);
102
 
103
+ // Store the interval ID in window object with unique ID
104
+ window[timerId] = timerInterval;
105
+ window.currentTimerId = timerId;
106
  </script>
107
  """
108
  return html(timer_html, height=50)
109
 
110
+ def freeze_timer():
111
+ freeze_html = """
112
  <script>
113
+ // Find the current timer ID
114
+ if (window.currentTimerId && window[window.currentTimerId]) {
115
+ // Stop the interval
116
+ clearInterval(window[window.currentTimerId]);
117
+ window[window.currentTimerId] = null;
 
 
 
 
 
 
 
 
 
 
118
 
119
+ // Get and style the timer element
120
+ var timerElement = document.getElementById("timer");
121
+ if (timerElement) {
122
+ timerElement.style.color = "#00cc00";
123
+ timerElement.style.fontWeight = "bold";
124
+ timerElement.innerHTML = timerElement.innerHTML + " ✓";
125
+ }
126
  }
127
  </script>
128
  """
129
+ return html(freeze_html, height=0)
130
 
131
  # UI components
132
  uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
 
135
  if st.session_state.image_data is not None:
136
  image_container.image(st.session_state.image_data, caption="Uploaded Image", use_container_width=True)
137
 
 
 
 
 
 
 
 
 
 
138
  # Process new uploaded file
139
  if uploaded_file is not None:
140
  # Save the image data to session state
 
146
 
147
  if st.session_state.get('current_file') != uploaded_file.name:
148
  st.session_state.current_file = uploaded_file.name
149
+ st.session_state.timer_active = True
150
+ st.session_state.timer_to_freeze = False
151
 
152
+ # Display timer
153
+ timer_container.empty()
154
+ timer_container.write(active_timer())
155
 
156
  # Progress indicators
157
  status_text = status_container.empty()
 
187
  # Final status
188
  status_text.success("**✅ Generation complete!**")
189
 
190
+ # Mark timer to be frozen when audio is played
191
+ st.session_state.timer_to_freeze = True
192
 
193
  # Show results
194
  with results_container:
 
196
  st.write("**Story:**", st.session_state.processed_data['story'])
197
 
198
  except Exception as e:
 
199
  status_text.error(f"**❌ Error:** {str(e)}")
200
  progress_bar.empty()
201
  raise e
202
 
203
+ # Display results if available
204
+ if st.session_state.processed_data.get('scenario'):
205
+ with results_container:
206
+ st.write("**Caption:**", st.session_state.processed_data['scenario'])
207
+
208
+ if st.session_state.processed_data.get('story'):
209
+ with results_container:
210
+ st.write("**Story:**", st.session_state.processed_data['story'])
211
+
212
  # Audio playback
213
  if st.button("Play Audio of the Story Generated"):
214
  if st.session_state.processed_data.get('audio'):
215
  # Make sure the image is still displayed
216
  if st.session_state.image_data is not None:
217
  image_container.image(st.session_state.image_data, caption="Uploaded Image", use_container_width=True)
218
+
219
+ # Freeze the timer if it's active and marked to be frozen
220
+ if st.session_state.timer_active and st.session_state.timer_to_freeze:
221
+ freeze_timer()
222
+ st.session_state.timer_active = False
223
 
224
+ # Play the audio
225
  audio_data = st.session_state.processed_data['audio']
226
  st.audio(
227
  audio_data['audio'].getvalue(),