frankai98 commited on
Commit
52cdbd8
·
verified ·
1 Parent(s): 2521826

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -70
app.py CHANGED
@@ -7,7 +7,6 @@ import torch
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():
@@ -24,11 +23,14 @@ 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
- 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="🦜")
@@ -76,57 +78,13 @@ 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();
91
- var elapsed = now - startTime;
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,6 +93,15 @@ 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,12 +113,11 @@ if uploaded_file is not None:
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,9 +153,6 @@ if uploaded_file is not None:
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:
195
  st.write("**Caption:**", st.session_state.processed_data['scenario'])
@@ -216,11 +179,12 @@ if st.button("Play Audio of the Story Generated"):
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(
@@ -228,4 +192,9 @@ if st.button("Play Audio of the Story Generated"):
228
  format="audio/mp3"
229
  )
230
  else:
231
- st.warning("Please generate a story first!")
 
 
 
 
 
 
7
  from gtts import gTTS
8
  import io
9
  import time
 
10
  import asyncio
11
 
12
  if not asyncio.get_event_loop().is_running():
 
23
  if 'image_data' not in st.session_state:
24
  st.session_state.image_data = None
25
 
26
+ if 'timer_value' not in st.session_state:
27
+ st.session_state.timer_value = "00:00"
28
 
29
+ if 'timer_frozen' not in st.session_state:
30
+ st.session_state.timer_frozen = False
31
+
32
+ if 'timer_start_time' not in st.session_state:
33
+ st.session_state.timer_start_time = None
34
 
35
  # Page setup
36
  st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
 
78
  progress_container = st.empty()
79
  results_container = st.container()
80
 
81
+ # Update timer value (simple, native Streamlit approach)
82
+ def update_timer():
83
+ if st.session_state.timer_start_time and not st.session_state.timer_frozen:
84
+ elapsed = time.time() - st.session_state.timer_start_time
85
+ minutes = int(elapsed // 60)
86
+ seconds = int(elapsed % 60)
87
+ st.session_state.timer_value = f"{minutes:02d}:{seconds:02d}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
  # UI components
90
  uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
 
93
  if st.session_state.image_data is not None:
94
  image_container.image(st.session_state.image_data, caption="Uploaded Image", use_container_width=True)
95
 
96
+ # Update and display timer
97
+ update_timer()
98
+
99
+ # Style the timer based on whether it's frozen
100
+ if st.session_state.timer_frozen:
101
+ timer_container.markdown(f"<div style='font-size:16px;color:#00cc00;font-weight:bold;margin-bottom:10px;'>⏱️ Elapsed: {st.session_state.timer_value} ✓</div>", unsafe_allow_html=True)
102
+ else:
103
+ timer_container.markdown(f"<div style='font-size:16px;color:#666;margin-bottom:10px;'>⏱️ Elapsed: {st.session_state.timer_value}</div>", unsafe_allow_html=True)
104
+
105
  # Process new uploaded file
106
  if uploaded_file is not None:
107
  # Save the image data to session state
 
113
 
114
  if st.session_state.get('current_file') != uploaded_file.name:
115
  st.session_state.current_file = uploaded_file.name
 
 
116
 
117
+ # Reset and start timer
118
+ st.session_state.timer_start_time = time.time()
119
+ st.session_state.timer_frozen = False
120
+ st.session_state.timer_value = "00:00"
121
 
122
  # Progress indicators
123
  status_text = status_container.empty()
 
153
  # Final status
154
  status_text.success("**✅ Generation complete!**")
155
 
 
 
 
156
  # Show results
157
  with results_container:
158
  st.write("**Caption:**", st.session_state.processed_data['scenario'])
 
179
  if st.session_state.image_data is not None:
180
  image_container.image(st.session_state.image_data, caption="Uploaded Image", use_container_width=True)
181
 
182
+ # Freeze the timer
183
+ st.session_state.timer_frozen = True
184
+
185
+ # Display final timer value with completion styling
186
+ timer_container.markdown(f"<div style='font-size:16px;color:#00cc00;font-weight:bold;margin-bottom:10px;'>⏱️ Elapsed: {st.session_state.timer_value} ✓</div>", unsafe_allow_html=True)
187
+
188
  # Play the audio
189
  audio_data = st.session_state.processed_data['audio']
190
  st.audio(
 
192
  format="audio/mp3"
193
  )
194
  else:
195
+ st.warning("Please generate a story first!")
196
+
197
+ # Trigger rerun to update timer every second if timer is running
198
+ if st.session_state.timer_start_time and not st.session_state.timer_frozen:
199
+ time.sleep(1)
200
+ st.experimental_rerun()