Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -23,7 +23,7 @@ if 'timer_started' not in st.session_state:
|
|
23 |
if 'timer_frozen' not in st.session_state:
|
24 |
st.session_state.timer_frozen = False
|
25 |
|
26 |
-
# Timer component
|
27 |
def timer():
|
28 |
return """
|
29 |
<div id="timer" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>
|
@@ -31,8 +31,15 @@ def timer():
|
|
31 |
(function() {
|
32 |
var start = Date.now();
|
33 |
var timerElement = document.getElementById('timer');
|
34 |
-
//
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
var elapsed = Date.now() - start;
|
37 |
var minutes = Math.floor(elapsed / 60000);
|
38 |
var seconds = Math.floor((elapsed % 60000) / 1000);
|
@@ -40,12 +47,6 @@ def timer():
|
|
40 |
(minutes < 10 ? '0' : '') + minutes + ':' +
|
41 |
(seconds < 10 ? '0' : '') + seconds;
|
42 |
}, 1000);
|
43 |
-
// Expose a function to freeze the timer.
|
44 |
-
window.freezeTimer = function() {
|
45 |
-
clearInterval(window.myTimerInterval);
|
46 |
-
// Change the timer color to indicate it’s frozen.
|
47 |
-
timerElement.style.color = '#00cc00';
|
48 |
-
};
|
49 |
})();
|
50 |
</script>
|
51 |
"""
|
@@ -93,10 +94,10 @@ def text2audio(story_text):
|
|
93 |
uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
|
94 |
|
95 |
if uploaded_file is not None:
|
96 |
-
#
|
97 |
if not st.session_state.timer_started and not st.session_state.timer_frozen:
|
98 |
st.session_state.timer_started = True
|
99 |
-
html(timer(), height=50)
|
100 |
|
101 |
# Initialize progress containers
|
102 |
status_text = st.empty()
|
@@ -137,9 +138,9 @@ if uploaded_file is not None:
|
|
137 |
progress_bar.progress(100)
|
138 |
|
139 |
status_text.success("**✅ Generation complete!**")
|
140 |
-
#
|
141 |
-
html("<script>
|
142 |
-
st.session_state.timer_frozen = True
|
143 |
except Exception as e:
|
144 |
html("<script>document.getElementById('timer').remove();</script>")
|
145 |
status_text.error(f"**❌ Error:** {str(e)}")
|
@@ -149,9 +150,10 @@ if uploaded_file is not None:
|
|
149 |
st.write("**Caption:**", st.session_state.processed_data['scenario'])
|
150 |
st.write("**Story:**", st.session_state.processed_data['story'])
|
151 |
|
|
|
152 |
if st.button("Play Audio of the Story Generated"):
|
153 |
if st.session_state.processed_data.get('audio'):
|
154 |
-
# Since the timer is already frozen,
|
155 |
audio_data = st.session_state.processed_data['audio']
|
156 |
st.audio(audio_data['audio'].getvalue(), format="audio/mp3")
|
157 |
else:
|
|
|
23 |
if 'timer_frozen' not in st.session_state:
|
24 |
st.session_state.timer_frozen = False
|
25 |
|
26 |
+
# Timer component that uses localStorage to check for a freeze flag
|
27 |
def timer():
|
28 |
return """
|
29 |
<div id="timer" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>
|
|
|
31 |
(function() {
|
32 |
var start = Date.now();
|
33 |
var timerElement = document.getElementById('timer');
|
34 |
+
// Clear any previous freeze flag
|
35 |
+
localStorage.removeItem("freezeTimer");
|
36 |
+
var interval = setInterval(function() {
|
37 |
+
// Check if freeze flag is set in localStorage.
|
38 |
+
if(localStorage.getItem("freezeTimer") === "true"){
|
39 |
+
clearInterval(interval);
|
40 |
+
timerElement.style.color = '#00cc00'; // Change color to indicate frozen.
|
41 |
+
return;
|
42 |
+
}
|
43 |
var elapsed = Date.now() - start;
|
44 |
var minutes = Math.floor(elapsed / 60000);
|
45 |
var seconds = Math.floor((elapsed % 60000) / 1000);
|
|
|
47 |
(minutes < 10 ? '0' : '') + minutes + ':' +
|
48 |
(seconds < 10 ? '0' : '') + seconds;
|
49 |
}, 1000);
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
})();
|
51 |
</script>
|
52 |
"""
|
|
|
94 |
uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
|
95 |
|
96 |
if uploaded_file is not None:
|
97 |
+
# Inject the timer only once, right after image upload.
|
98 |
if not st.session_state.timer_started and not st.session_state.timer_frozen:
|
99 |
st.session_state.timer_started = True
|
100 |
+
html(timer(), height=50, key="timer_component")
|
101 |
|
102 |
# Initialize progress containers
|
103 |
status_text = st.empty()
|
|
|
138 |
progress_bar.progress(100)
|
139 |
|
140 |
status_text.success("**✅ Generation complete!**")
|
141 |
+
# Immediately freeze the timer by setting the freeze flag.
|
142 |
+
html("<script>localStorage.setItem('freezeTimer', 'true');</script>", height=0)
|
143 |
+
st.session_state.timer_frozen = True
|
144 |
except Exception as e:
|
145 |
html("<script>document.getElementById('timer').remove();</script>")
|
146 |
status_text.error(f"**❌ Error:** {str(e)}")
|
|
|
150 |
st.write("**Caption:**", st.session_state.processed_data['scenario'])
|
151 |
st.write("**Story:**", st.session_state.processed_data['story'])
|
152 |
|
153 |
+
# Optionally, you can still have the Play Audio button.
|
154 |
if st.button("Play Audio of the Story Generated"):
|
155 |
if st.session_state.processed_data.get('audio'):
|
156 |
+
# Since the timer is already frozen by now, just play the audio.
|
157 |
audio_data = st.session_state.processed_data['audio']
|
158 |
st.audio(audio_data['audio'].getvalue(), format="audio/mp3")
|
159 |
else:
|