Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -20,18 +20,21 @@ if 'processed_data' not in st.session_state:
|
|
20 |
'story': None,
|
21 |
'audio': None
|
22 |
}
|
23 |
-
|
|
|
|
|
|
|
24 |
|
25 |
# JavaScript timer component
|
26 |
def timer():
|
27 |
return """
|
28 |
<div id="timer" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>
|
29 |
<script>
|
30 |
-
function
|
31 |
var start = Date.now();
|
32 |
var timerElement = document.getElementById('timer');
|
33 |
-
|
34 |
-
|
35 |
var elapsed = Date.now() - start;
|
36 |
var minutes = Math.floor(elapsed / 60000);
|
37 |
var seconds = Math.floor((elapsed % 60000) / 1000);
|
@@ -39,17 +42,10 @@ def timer():
|
|
39 |
(minutes < 10 ? '0' : '') + minutes + ':' +
|
40 |
(seconds < 10 ? '0' : '') + seconds;
|
41 |
}, 1000);
|
42 |
-
|
43 |
-
// Make the cleanup function available globally
|
44 |
-
window.stopTimer = function() {
|
45 |
-
clearInterval(interval);
|
46 |
-
};
|
47 |
-
}
|
48 |
-
updateTimer();
|
49 |
</script>
|
50 |
"""
|
51 |
|
52 |
-
|
53 |
# Page setup
|
54 |
st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
|
55 |
st.header("Turn Your Image to a Short Audio Story for Children")
|
@@ -80,6 +76,7 @@ def text2story(text):
|
|
80 |
do_sample=True,
|
81 |
temperature=0.7
|
82 |
)[0]["generated_text"]
|
|
|
83 |
return response[2]["content"]
|
84 |
|
85 |
def text2audio(story_text):
|
@@ -89,18 +86,19 @@ def text2audio(story_text):
|
|
89 |
audio_io.seek(0)
|
90 |
return {'audio': audio_io, 'sampling_rate': 16000}
|
91 |
|
|
|
|
|
|
|
|
|
|
|
92 |
# UI components
|
93 |
uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
|
94 |
-
|
95 |
if uploaded_file is not None:
|
96 |
# Initialize progress containers
|
97 |
status_text = st.empty()
|
98 |
progress_bar = st.progress(0)
|
99 |
|
100 |
-
# Start JavaScript timer
|
101 |
-
html(timer(), height=50)
|
102 |
-
|
103 |
-
|
104 |
try:
|
105 |
# Save uploaded file
|
106 |
bytes_data = uploaded_file.getvalue()
|
@@ -135,41 +133,28 @@ if uploaded_file is not None:
|
|
135 |
)
|
136 |
progress_bar.progress(100)
|
137 |
|
138 |
-
# Final status and
|
139 |
status_text.success("**✅ Generation complete!**")
|
140 |
html("<script>document.getElementById('timer').style.color = '#00cc00';</script>")
|
141 |
-
|
142 |
-
# Show results
|
143 |
-
# st.subheader("Results")
|
144 |
-
st.write("**Caption:**", st.session_state.processed_data['scenario'])
|
145 |
-
st.write("**Story:**", st.session_state.processed_data['story'])
|
146 |
-
|
147 |
except Exception as e:
|
148 |
html("<script>document.getElementById('timer').remove();</script>")
|
149 |
status_text.error(f"**❌ Error:** {str(e)}")
|
150 |
progress_bar.empty()
|
151 |
raise e
|
152 |
|
153 |
-
|
154 |
-
|
155 |
-
pass # Timer cleanup handled by JavaScript
|
156 |
-
|
157 |
|
158 |
-
# Audio playback
|
159 |
if st.button("Play Audio of the Story Generated"):
|
160 |
-
# Stop the timer
|
161 |
-
st.markdown(
|
162 |
-
"""
|
163 |
-
<script>
|
164 |
-
if (window.stopTimer) {
|
165 |
-
window.stopTimer();
|
166 |
-
}
|
167 |
-
</script>
|
168 |
-
""",
|
169 |
-
unsafe_allow_html=True
|
170 |
-
)
|
171 |
-
|
172 |
if st.session_state.processed_data.get('audio'):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
audio_data = st.session_state.processed_data['audio']
|
174 |
st.audio(audio_data['audio'].getvalue(), format="audio/mp3")
|
175 |
else:
|
|
|
20 |
'story': None,
|
21 |
'audio': None
|
22 |
}
|
23 |
+
if 'timer_frozen' not in st.session_state:
|
24 |
+
st.session_state.timer_frozen = False
|
25 |
+
if 'timer_started' not in st.session_state:
|
26 |
+
st.session_state.timer_started = False
|
27 |
|
28 |
# JavaScript timer component
|
29 |
def timer():
|
30 |
return """
|
31 |
<div id="timer" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>
|
32 |
<script>
|
33 |
+
(function() {
|
34 |
var start = Date.now();
|
35 |
var timerElement = document.getElementById('timer');
|
36 |
+
// Save the interval id globally
|
37 |
+
window.myTimerInterval = setInterval(function() {
|
38 |
var elapsed = Date.now() - start;
|
39 |
var minutes = Math.floor(elapsed / 60000);
|
40 |
var seconds = Math.floor((elapsed % 60000) / 1000);
|
|
|
42 |
(minutes < 10 ? '0' : '') + minutes + ':' +
|
43 |
(seconds < 10 ? '0' : '') + seconds;
|
44 |
}, 1000);
|
45 |
+
})();
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
</script>
|
47 |
"""
|
48 |
|
|
|
49 |
# Page setup
|
50 |
st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
|
51 |
st.header("Turn Your Image to a Short Audio Story for Children")
|
|
|
76 |
do_sample=True,
|
77 |
temperature=0.7
|
78 |
)[0]["generated_text"]
|
79 |
+
# Adjust response extraction as needed (this was in your original code)
|
80 |
return response[2]["content"]
|
81 |
|
82 |
def text2audio(story_text):
|
|
|
86 |
audio_io.seek(0)
|
87 |
return {'audio': audio_io, 'sampling_rate': 16000}
|
88 |
|
89 |
+
# Only start the timer once (when image is uploaded) if not frozen.
|
90 |
+
if not st.session_state.timer_started and not st.session_state.timer_frozen:
|
91 |
+
html(timer(), height=50)
|
92 |
+
st.session_state.timer_started = True
|
93 |
+
|
94 |
# UI components
|
95 |
uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
|
96 |
+
|
97 |
if uploaded_file is not None:
|
98 |
# Initialize progress containers
|
99 |
status_text = st.empty()
|
100 |
progress_bar = st.progress(0)
|
101 |
|
|
|
|
|
|
|
|
|
102 |
try:
|
103 |
# Save uploaded file
|
104 |
bytes_data = uploaded_file.getvalue()
|
|
|
133 |
)
|
134 |
progress_bar.progress(100)
|
135 |
|
136 |
+
# Final status and optionally change timer color
|
137 |
status_text.success("**✅ Generation complete!**")
|
138 |
html("<script>document.getElementById('timer').style.color = '#00cc00';</script>")
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
except Exception as e:
|
140 |
html("<script>document.getElementById('timer').remove();</script>")
|
141 |
status_text.error(f"**❌ Error:** {str(e)}")
|
142 |
progress_bar.empty()
|
143 |
raise e
|
144 |
|
145 |
+
st.write("**Caption:**", st.session_state.processed_data['scenario'])
|
146 |
+
st.write("**Story:**", st.session_state.processed_data['story'])
|
|
|
|
|
147 |
|
148 |
+
# Audio playback with timer freeze
|
149 |
if st.button("Play Audio of the Story Generated"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
if st.session_state.processed_data.get('audio'):
|
151 |
+
# Stop the timer by clearing the interval
|
152 |
+
st.markdown(
|
153 |
+
"<script>if(window.myTimerInterval){ clearInterval(window.myTimerInterval); }</script>",
|
154 |
+
unsafe_allow_html=True
|
155 |
+
)
|
156 |
+
# Set flag so that timer HTML is not re-rendered
|
157 |
+
st.session_state.timer_frozen = True
|
158 |
audio_data = st.session_state.processed_data['audio']
|
159 |
st.audio(audio_data['audio'].getvalue(), format="audio/mp3")
|
160 |
else:
|