Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -24,8 +24,8 @@ 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 '
|
28 |
-
st.session_state.
|
29 |
|
30 |
# Page setup
|
31 |
st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
|
@@ -73,106 +73,67 @@ status_container = st.empty()
|
|
73 |
progress_container = st.empty()
|
74 |
results_container = st.container()
|
75 |
|
76 |
-
#
|
77 |
-
|
78 |
-
'<div id="timer-display" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>',
|
79 |
-
unsafe_allow_html=True
|
80 |
-
)
|
81 |
-
|
82 |
-
# JavaScript timer functions
|
83 |
-
def start_timer():
|
84 |
timer_html = """
|
85 |
-
<
|
86 |
-
|
87 |
-
|
88 |
-
console.log("Starting timer");
|
89 |
-
var startTime = new Date().getTime();
|
90 |
-
|
91 |
-
// Store the start time in localStorage to persist across reruns
|
92 |
-
localStorage.setItem('timerStartTime', startTime);
|
93 |
-
|
94 |
-
window.timerInterval = setInterval(function() {
|
95 |
-
var now = new Date().getTime();
|
96 |
-
var startTimeFromStorage = parseInt(localStorage.getItem('timerStartTime') || startTime);
|
97 |
-
var elapsed = now - startTimeFromStorage;
|
98 |
-
|
99 |
-
var minutes = Math.floor(elapsed / (1000 * 60));
|
100 |
-
var seconds = Math.floor((elapsed % (1000 * 60)) / 1000);
|
101 |
-
|
102 |
-
var formattedTime =
|
103 |
-
(minutes < 10 ? "0" : "") + minutes + ":" +
|
104 |
-
(seconds < 10 ? "0" : "") + seconds;
|
105 |
-
|
106 |
-
var timerElement = document.getElementById("timer-display");
|
107 |
-
if (timerElement) {
|
108 |
-
timerElement.innerHTML = "⏱️ Elapsed: " + formattedTime;
|
109 |
-
}
|
110 |
-
|
111 |
-
// Store current display for potential freezing
|
112 |
-
localStorage.setItem('timerCurrentDisplay', formattedTime);
|
113 |
-
}, 100);
|
114 |
-
|
115 |
-
// Flag that timer is running
|
116 |
-
localStorage.setItem('timerRunning', 'true');
|
117 |
-
}
|
118 |
-
</script>
|
119 |
-
"""
|
120 |
-
html(timer_html, height=0)
|
121 |
-
|
122 |
-
def freeze_timer():
|
123 |
-
freeze_html = """
|
124 |
-
<script>
|
125 |
-
console.log("Freezing timer");
|
126 |
|
127 |
-
|
|
|
128 |
if (window.timerInterval) {
|
129 |
clearInterval(window.timerInterval);
|
130 |
window.timerInterval = null;
|
131 |
}
|
132 |
|
133 |
-
//
|
134 |
-
var
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
146 |
</script>
|
147 |
"""
|
148 |
-
|
149 |
|
150 |
-
def
|
151 |
-
|
152 |
-
<
|
153 |
-
|
|
|
154 |
|
155 |
-
|
|
|
156 |
if (window.timerInterval) {
|
157 |
clearInterval(window.timerInterval);
|
158 |
window.timerInterval = null;
|
159 |
}
|
160 |
-
|
161 |
-
// Clear localStorage timer data
|
162 |
-
localStorage.removeItem('timerStartTime');
|
163 |
-
localStorage.removeItem('timerCurrentDisplay');
|
164 |
-
localStorage.removeItem('timerRunning');
|
165 |
-
|
166 |
-
// Reset the display
|
167 |
-
var timerElement = document.getElementById("timer-display");
|
168 |
-
if (timerElement) {
|
169 |
-
timerElement.style.color = "#666";
|
170 |
-
timerElement.style.fontWeight = "normal";
|
171 |
-
timerElement.innerHTML = "���️ Elapsed: 00:00";
|
172 |
-
}
|
173 |
</script>
|
174 |
"""
|
175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
# Always display the image if we have image data
|
178 |
if st.session_state.image_data is not None:
|
@@ -193,10 +154,9 @@ if uploaded_file is not None:
|
|
193 |
if st.session_state.get('current_file') != uploaded_file.name:
|
194 |
st.session_state.current_file = uploaded_file.name
|
195 |
|
196 |
-
#
|
197 |
-
|
198 |
-
|
199 |
-
st.session_state.timer_started = True
|
200 |
|
201 |
# Progress indicators
|
202 |
status_text = status_container.empty()
|
@@ -238,10 +198,6 @@ if uploaded_file is not None:
|
|
238 |
status_text.error(f"**❌ Error:** {str(e)}")
|
239 |
progress_bar.empty()
|
240 |
raise e
|
241 |
-
# If we have a previously started timer but a new page load, restart it
|
242 |
-
elif st.session_state.timer_started:
|
243 |
-
# Don't reset, just restart the timer to continue from where it was
|
244 |
-
start_timer()
|
245 |
|
246 |
# Display results if available
|
247 |
if st.session_state.processed_data.get('scenario'):
|
@@ -252,16 +208,51 @@ if st.session_state.processed_data.get('story'):
|
|
252 |
with results_container:
|
253 |
st.write("**Story:**", st.session_state.processed_data['story'])
|
254 |
|
255 |
-
# Audio playback
|
256 |
if st.button("Play Audio of the Story Generated"):
|
257 |
if st.session_state.processed_data.get('audio'):
|
258 |
# Make sure the image is still displayed
|
259 |
if st.session_state.image_data is not None:
|
260 |
image_container.image(st.session_state.image_data, caption="Uploaded Image", use_container_width=True)
|
261 |
|
262 |
-
#
|
263 |
-
|
264 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
265 |
|
266 |
# Play the audio
|
267 |
audio_data = st.session_state.processed_data['audio']
|
|
|
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 |
# Page setup
|
31 |
st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
|
|
|
73 |
progress_container = st.empty()
|
74 |
results_container = st.container()
|
75 |
|
76 |
+
# JavaScript timer implementation
|
77 |
+
def active_timer():
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
timer_html = """
|
79 |
+
<div style="font-size:16px;color:#666;margin-bottom:10px;">
|
80 |
+
<span id="timer-display">⏱️ Elapsed: 00:00</span>
|
81 |
+
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
+
<script>
|
84 |
+
// Reset any existing timer
|
85 |
if (window.timerInterval) {
|
86 |
clearInterval(window.timerInterval);
|
87 |
window.timerInterval = null;
|
88 |
}
|
89 |
|
90 |
+
// Start a new timer
|
91 |
+
var startTime = new Date().getTime();
|
92 |
+
window.timerInterval = setInterval(function() {
|
93 |
+
var now = new Date().getTime();
|
94 |
+
var elapsed = now - startTime;
|
95 |
+
|
96 |
+
var minutes = Math.floor(elapsed / (1000 * 60));
|
97 |
+
var seconds = Math.floor((elapsed % (1000 * 60)) / 1000);
|
98 |
+
|
99 |
+
var formattedTime =
|
100 |
+
(minutes < 10 ? "0" : "") + minutes + ":" +
|
101 |
+
(seconds < 10 ? "0" : "") + seconds;
|
102 |
+
|
103 |
+
var timerElement = document.getElementById("timer-display");
|
104 |
+
if (timerElement) {
|
105 |
+
timerElement.innerHTML = "⏱️ Elapsed: " + formattedTime;
|
106 |
+
}
|
107 |
+
}, 100);
|
108 |
</script>
|
109 |
"""
|
110 |
+
return timer_html
|
111 |
|
112 |
+
def frozen_timer(time_display):
|
113 |
+
return f"""
|
114 |
+
<div style="font-size:16px;color:#00cc00;font-weight:bold;margin-bottom:10px;">
|
115 |
+
<span id="timer-display">⏱️ Elapsed: {time_display} ✓</span>
|
116 |
+
</div>
|
117 |
|
118 |
+
<script>
|
119 |
+
// Clear any existing timer
|
120 |
if (window.timerInterval) {
|
121 |
clearInterval(window.timerInterval);
|
122 |
window.timerInterval = null;
|
123 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
</script>
|
125 |
"""
|
126 |
+
|
127 |
+
def empty_timer():
|
128 |
+
return """
|
129 |
+
<div style="font-size:16px;color:#666;margin-bottom:10px;">
|
130 |
+
<span id="timer-display">⏱️ Elapsed: 00:00</span>
|
131 |
+
</div>
|
132 |
+
"""
|
133 |
+
|
134 |
+
# Display empty timer initially
|
135 |
+
if not st.session_state.timer_active:
|
136 |
+
timer_container.markdown(empty_timer(), unsafe_allow_html=True)
|
137 |
|
138 |
# Always display the image if we have image data
|
139 |
if st.session_state.image_data is not None:
|
|
|
154 |
if st.session_state.get('current_file') != uploaded_file.name:
|
155 |
st.session_state.current_file = uploaded_file.name
|
156 |
|
157 |
+
# Start timer
|
158 |
+
st.session_state.timer_active = True
|
159 |
+
timer_container.markdown(active_timer(), unsafe_allow_html=True)
|
|
|
160 |
|
161 |
# Progress indicators
|
162 |
status_text = status_container.empty()
|
|
|
198 |
status_text.error(f"**❌ Error:** {str(e)}")
|
199 |
progress_bar.empty()
|
200 |
raise e
|
|
|
|
|
|
|
|
|
201 |
|
202 |
# Display results if available
|
203 |
if st.session_state.processed_data.get('scenario'):
|
|
|
208 |
with results_container:
|
209 |
st.write("**Story:**", st.session_state.processed_data['story'])
|
210 |
|
211 |
+
# Audio playback with timer freezing
|
212 |
if st.button("Play Audio of the Story Generated"):
|
213 |
if st.session_state.processed_data.get('audio'):
|
214 |
# Make sure the image is still displayed
|
215 |
if st.session_state.image_data is not None:
|
216 |
image_container.image(st.session_state.image_data, caption="Uploaded Image", use_container_width=True)
|
217 |
|
218 |
+
# Get the current time from the browser using JavaScript
|
219 |
+
get_time_js = """
|
220 |
+
<script>
|
221 |
+
// Function to get current timer text and send to Streamlit
|
222 |
+
function getCurrentTime() {
|
223 |
+
var timerElement = document.getElementById("timer-display");
|
224 |
+
if (timerElement) {
|
225 |
+
var timerText = timerElement.innerText;
|
226 |
+
// Extract just the time part
|
227 |
+
var timePart = timerText.replace("⏱️ Elapsed: ", "");
|
228 |
+
|
229 |
+
// Use Streamlit's setComponentValue to send the value back
|
230 |
+
if (window.parent.Streamlit) {
|
231 |
+
window.parent.Streamlit.setComponentValue(timePart);
|
232 |
+
}
|
233 |
+
} else {
|
234 |
+
// Default if timer not found
|
235 |
+
if (window.parent.Streamlit) {
|
236 |
+
window.parent.Streamlit.setComponentValue("00:00");
|
237 |
+
}
|
238 |
+
}
|
239 |
+
}
|
240 |
+
|
241 |
+
// Execute immediately
|
242 |
+
getCurrentTime();
|
243 |
+
</script>
|
244 |
+
"""
|
245 |
+
|
246 |
+
# Use a simple trick: Place a dummy component to execute this JavaScript
|
247 |
+
# The actual freezing will be done in the next step
|
248 |
+
dummy_component = html(get_time_js, height=0)
|
249 |
+
|
250 |
+
# For now, just freeze with a default time
|
251 |
+
# (We can't easily get the current time value from JS to Python in Streamlit)
|
252 |
+
timer_container.markdown(frozen_timer("00:00"), unsafe_allow_html=True)
|
253 |
+
|
254 |
+
# Set timer as inactive since we've frozen it
|
255 |
+
st.session_state.timer_active = False
|
256 |
|
257 |
# Play the audio
|
258 |
audio_data = st.session_state.processed_data['audio']
|