Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,7 @@ from gtts import gTTS
|
|
8 |
import io
|
9 |
import time
|
10 |
import asyncio
|
|
|
11 |
|
12 |
if not asyncio.get_event_loop().is_running():
|
13 |
asyncio.set_event_loop(asyncio.new_event_loop())
|
@@ -23,9 +24,15 @@ if 'processed_data' not in st.session_state:
|
|
23 |
if 'image_data' not in st.session_state:
|
24 |
st.session_state.image_data = None
|
25 |
|
26 |
-
if '
|
27 |
-
st.session_state.
|
|
|
|
|
|
|
28 |
|
|
|
|
|
|
|
29 |
# Page setup
|
30 |
st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
|
31 |
st.header("Turn Your Image to a Short Audio Story for Children")
|
@@ -72,63 +79,23 @@ status_container = st.empty()
|
|
72 |
progress_container = st.empty()
|
73 |
results_container = st.container()
|
74 |
|
75 |
-
#
|
76 |
-
def
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
if (window.timerInterval) {
|
83 |
-
clearInterval(window.timerInterval);
|
84 |
-
window.timerInterval = null;
|
85 |
-
}
|
86 |
-
|
87 |
-
// Set start time
|
88 |
-
const startTime = new Date().getTime();
|
89 |
-
|
90 |
-
// Update function
|
91 |
-
function updateTimer() {
|
92 |
-
const timerElement = document.getElementById('timer-display');
|
93 |
-
if (!timerElement) return; // Safety check
|
94 |
-
|
95 |
-
const currentTime = new Date().getTime();
|
96 |
-
const elapsedMs = currentTime - startTime;
|
97 |
-
|
98 |
-
const minutes = Math.floor(elapsedMs / 60000);
|
99 |
-
const seconds = Math.floor((elapsedMs % 60000) / 1000);
|
100 |
-
|
101 |
-
const minStr = (minutes < 10) ? '0' + minutes : minutes;
|
102 |
-
const secStr = (seconds < 10) ? '0' + seconds : seconds;
|
103 |
-
|
104 |
-
timerElement.innerHTML = `⏱️ Elapsed: ${minStr}:${secStr}`;
|
105 |
-
}
|
106 |
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
window.timerInterval = setInterval(updateTimer, 1000);
|
112 |
-
})();
|
113 |
-
</script>
|
114 |
-
"""
|
115 |
-
|
116 |
-
def stop_timer():
|
117 |
-
return """
|
118 |
-
<script>
|
119 |
-
(function() {
|
120 |
-
if (window.timerInterval) {
|
121 |
-
clearInterval(window.timerInterval);
|
122 |
-
window.timerInterval = null;
|
123 |
|
124 |
-
|
125 |
-
if
|
126 |
-
|
127 |
-
|
128 |
-
}
|
129 |
-
})();
|
130 |
-
</script>
|
131 |
-
"""
|
132 |
|
133 |
# UI components
|
134 |
uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
|
@@ -146,6 +113,10 @@ if st.session_state.processed_data.get('story'):
|
|
146 |
with results_container:
|
147 |
st.write("**Story:**", st.session_state.processed_data['story'])
|
148 |
|
|
|
|
|
|
|
|
|
149 |
# Process new uploaded file
|
150 |
if uploaded_file is not None:
|
151 |
# Save the image data to session state
|
@@ -157,10 +128,11 @@ if uploaded_file is not None:
|
|
157 |
|
158 |
if st.session_state.get('current_file') != uploaded_file.name:
|
159 |
st.session_state.current_file = uploaded_file.name
|
160 |
-
st.session_state.timer_active = True
|
161 |
|
162 |
-
#
|
163 |
-
|
|
|
|
|
164 |
|
165 |
# Progress indicators
|
166 |
status_text = status_container.empty()
|
@@ -197,7 +169,7 @@ if uploaded_file is not None:
|
|
197 |
status_text.success("**✅ Generation complete!**")
|
198 |
|
199 |
# Stop timer
|
200 |
-
|
201 |
|
202 |
# Show results
|
203 |
with results_container:
|
@@ -205,7 +177,7 @@ if uploaded_file is not None:
|
|
205 |
st.write("**Story:**", st.session_state.processed_data['story'])
|
206 |
|
207 |
except Exception as e:
|
208 |
-
|
209 |
status_text.error(f"**❌ Error:** {str(e)}")
|
210 |
progress_bar.empty()
|
211 |
raise e
|
|
|
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 |
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")
|
|
|
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 |
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
|
|
|
128 |
|
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 |
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 |
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
|