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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -37
app.py CHANGED
@@ -7,8 +7,8 @@ import torch
7
  from gtts import gTTS
8
  import io
9
  import time
 
10
  import asyncio
11
- import datetime
12
 
13
  if not asyncio.get_event_loop().is_running():
14
  asyncio.set_event_loop(asyncio.new_event_loop())
@@ -24,15 +24,6 @@ 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_start' not in st.session_state:
28
- st.session_state.timer_start = None
29
-
30
- if 'timer_running' not in st.session_state:
31
- st.session_state.timer_running = False
32
-
33
- if 'timer_complete' not in st.session_state:
34
- st.session_state.timer_complete = False
35
-
36
  # Page setup
37
  st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
38
  st.header("Turn Your Image to a Short Audio Story for Children")
@@ -74,28 +65,58 @@ def text2audio(story_text):
74
 
75
  # Create fixed containers for UI elements
76
  image_container = st.empty()
77
- timer_container = st.empty()
78
  status_container = st.empty()
79
  progress_container = st.empty()
80
  results_container = st.container()
81
 
82
- # Native Streamlit timer display
83
- def display_timer():
84
- if st.session_state.timer_start is not None and st.session_state.timer_running:
85
- elapsed = datetime.datetime.now() - st.session_state.timer_start
86
- elapsed_seconds = int(elapsed.total_seconds())
87
- minutes = elapsed_seconds // 60
88
- seconds = elapsed_seconds % 60
 
 
 
 
89
 
90
- if st.session_state.timer_complete:
91
- timer_container.markdown(f"⏱️ **Elapsed: {minutes:02d}:{seconds:02d}**", unsafe_allow_html=True)
92
- else:
93
- timer_container.markdown(f"⏱️ Elapsed: {minutes:02d}:{seconds:02d}")
94
-
95
- # Keep updating timer while running
96
- if not st.session_state.timer_complete:
97
- time.sleep(0.1) # Small delay to reduce CPU usage
98
- st.experimental_rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
  # UI components
101
  uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
@@ -113,10 +134,6 @@ if st.session_state.processed_data.get('story'):
113
  with results_container:
114
  st.write("**Story:**", st.session_state.processed_data['story'])
115
 
116
- # Display timer if running
117
- if st.session_state.timer_running:
118
- display_timer()
119
-
120
  # Process new uploaded file
121
  if uploaded_file is not None:
122
  # Save the image data to session state
@@ -129,10 +146,8 @@ if uploaded_file is not None:
129
  if st.session_state.get('current_file') != uploaded_file.name:
130
  st.session_state.current_file = uploaded_file.name
131
 
132
- # Start timer
133
- st.session_state.timer_start = datetime.datetime.now()
134
- st.session_state.timer_running = True
135
- st.session_state.timer_complete = False
136
 
137
  # Progress indicators
138
  status_text = status_container.empty()
@@ -169,7 +184,7 @@ if uploaded_file is not None:
169
  status_text.success("**✅ Generation complete!**")
170
 
171
  # Stop timer
172
- st.session_state.timer_complete = True
173
 
174
  # Show results
175
  with results_container:
@@ -177,7 +192,7 @@ if uploaded_file is not None:
177
  st.write("**Story:**", st.session_state.processed_data['story'])
178
 
179
  except Exception as e:
180
- st.session_state.timer_complete = True
181
  status_text.error(f"**❌ Error:** {str(e)}")
182
  progress_bar.empty()
183
  raise e
 
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())
 
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
 
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();
80
+ var elapsed = now - startTime;
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...")
 
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
 
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()
 
184
  status_text.success("**✅ Generation complete!**")
185
 
186
  # Stop timer
187
+ stop_timer()
188
 
189
  # Show results
190
  with results_container:
 
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