Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,11 +8,11 @@ from gtts import gTTS
|
|
8 |
import io
|
9 |
import time
|
10 |
import asyncio
|
11 |
-
import
|
12 |
|
13 |
if not asyncio.get_event_loop().is_running():
|
14 |
asyncio.set_event_loop(asyncio.new_event_loop())
|
15 |
-
|
16 |
# Initialize session state
|
17 |
if 'processed_data' not in st.session_state:
|
18 |
st.session_state.processed_data = {
|
@@ -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_time' not in st.session_state:
|
28 |
-
st.session_state.timer_start_time = None
|
29 |
-
|
30 |
-
if 'timer_frozen' not in st.session_state:
|
31 |
-
st.session_state.timer_frozen = False
|
32 |
-
|
33 |
-
if 'last_update_time' not in st.session_state:
|
34 |
-
st.session_state.last_update_time = None
|
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,38 +70,112 @@ status_container = st.empty()
|
|
79 |
progress_container = st.empty()
|
80 |
results_container = st.container()
|
81 |
|
82 |
-
#
|
83 |
-
def
|
84 |
-
|
85 |
-
|
86 |
|
87 |
-
|
88 |
-
if
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
# Always display the image if we have image data
|
104 |
if st.session_state.image_data is not None:
|
105 |
image_container.image(st.session_state.image_data, caption="Uploaded Image", use_container_width=True)
|
106 |
|
107 |
-
#
|
108 |
-
|
109 |
-
|
110 |
-
if st.session_state.timer_frozen:
|
111 |
-
timer_container.markdown(f"<div style='font-size:16px;color:#00cc00;font-weight:bold;margin-bottom:10px;'>⏱️ Elapsed: {current_time_str} ✓</div>", unsafe_allow_html=True)
|
112 |
-
else:
|
113 |
-
timer_container.markdown(f"<div style='font-size:16px;color:#666;margin-bottom:10px;'>⏱️ Elapsed: {current_time_str}</div>", unsafe_allow_html=True)
|
114 |
|
115 |
# Process new uploaded file
|
116 |
if uploaded_file is not None:
|
@@ -125,9 +190,8 @@ if uploaded_file is not None:
|
|
125 |
st.session_state.current_file = uploaded_file.name
|
126 |
|
127 |
# Reset and start timer
|
128 |
-
|
129 |
-
|
130 |
-
st.session_state.timer_frozen = False
|
131 |
|
132 |
# Progress indicators
|
133 |
status_text = status_container.empty()
|
@@ -140,13 +204,11 @@ if uploaded_file is not None:
|
|
140 |
|
141 |
# Stage 1: Image to Text
|
142 |
status_text.markdown("**🖼️ Generating caption...**")
|
143 |
-
progress_bar.progress(0)
|
144 |
st.session_state.processed_data['scenario'] = img2text(uploaded_file.name)
|
145 |
progress_bar.progress(33)
|
146 |
|
147 |
# Stage 2: Text to Story
|
148 |
status_text.markdown("**📖 Generating story...**")
|
149 |
-
progress_bar.progress(33)
|
150 |
st.session_state.processed_data['story'] = text2story(
|
151 |
st.session_state.processed_data['scenario']
|
152 |
)
|
@@ -154,7 +216,6 @@ if uploaded_file is not None:
|
|
154 |
|
155 |
# Stage 3: Story to Audio
|
156 |
status_text.markdown("**🔊 Synthesizing audio...**")
|
157 |
-
progress_bar.progress(66)
|
158 |
st.session_state.processed_data['audio'] = text2audio(
|
159 |
st.session_state.processed_data['story']
|
160 |
)
|
@@ -182,19 +243,15 @@ if st.session_state.processed_data.get('story'):
|
|
182 |
with results_container:
|
183 |
st.write("**Story:**", st.session_state.processed_data['story'])
|
184 |
|
185 |
-
# Audio playback
|
186 |
if st.button("Play Audio of the Story Generated"):
|
187 |
if st.session_state.processed_data.get('audio'):
|
188 |
# Make sure the image is still displayed
|
189 |
if st.session_state.image_data is not None:
|
190 |
-
image_container.image(st.session_state.image_data, caption="Uploaded Image",
|
191 |
|
192 |
# Freeze the timer
|
193 |
-
|
194 |
-
|
195 |
-
# Update the timer display with frozen styling
|
196 |
-
final_time = get_formatted_timer()
|
197 |
-
timer_container.markdown(f"<div style='font-size:16px;color:#00cc00;font-weight:bold;margin-bottom:10px;'>⏱️ Elapsed: {final_time} ✓</div>", unsafe_allow_html=True)
|
198 |
|
199 |
# Play the audio
|
200 |
audio_data = st.session_state.processed_data['audio']
|
@@ -203,23 +260,4 @@ if st.button("Play Audio of the Story Generated"):
|
|
203 |
format="audio/mp3"
|
204 |
)
|
205 |
else:
|
206 |
-
st.warning("Please generate a story first!")
|
207 |
-
|
208 |
-
# Force a rerun every second while the timer is active (not frozen)
|
209 |
-
if st.session_state.timer_start_time is not None and not st.session_state.timer_frozen:
|
210 |
-
# Create a placeholder for our hidden component that triggers the rerun
|
211 |
-
rerun_trigger = st.empty()
|
212 |
-
|
213 |
-
# Add a hidden element that will automatically trigger a rerun after 0.5 seconds
|
214 |
-
rerun_trigger.markdown(
|
215 |
-
f"""
|
216 |
-
<div style="display:none;">
|
217 |
-
<script>
|
218 |
-
setTimeout(function() {{
|
219 |
-
window.parent.postMessage({{type: "streamlit:rerun"}}, "*");
|
220 |
-
}}, 500);
|
221 |
-
</script>
|
222 |
-
</div>
|
223 |
-
""",
|
224 |
-
unsafe_allow_html=True
|
225 |
-
)
|
|
|
8 |
import io
|
9 |
import time
|
10 |
import asyncio
|
11 |
+
from streamlit.components.v1 import html
|
12 |
|
13 |
if not asyncio.get_event_loop().is_running():
|
14 |
asyncio.set_event_loop(asyncio.new_event_loop())
|
15 |
+
|
16 |
# Initialize session state
|
17 |
if 'processed_data' not in st.session_state:
|
18 |
st.session_state.processed_data = {
|
|
|
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")
|
|
|
70 |
progress_container = st.empty()
|
71 |
results_container = st.container()
|
72 |
|
73 |
+
# JavaScript timer that continues to run until frozen
|
74 |
+
def start_timer():
|
75 |
+
timer_html = """
|
76 |
+
<div id="timer-display" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>
|
77 |
|
78 |
+
<script>
|
79 |
+
// Only create a timer if one doesn't exist already
|
80 |
+
if (!window.timerInterval) {
|
81 |
+
console.log("Starting timer");
|
82 |
+
var startTime = new Date().getTime();
|
83 |
+
|
84 |
+
// Store the start time in localStorage to persist across reruns
|
85 |
+
localStorage.setItem('timerStartTime', startTime);
|
86 |
+
|
87 |
+
window.timerInterval = setInterval(function() {
|
88 |
+
var now = new Date().getTime();
|
89 |
+
var startTimeFromStorage = parseInt(localStorage.getItem('timerStartTime') || startTime);
|
90 |
+
var elapsed = now - startTimeFromStorage;
|
91 |
+
|
92 |
+
var minutes = Math.floor(elapsed / (1000 * 60));
|
93 |
+
var seconds = Math.floor((elapsed % (1000 * 60)) / 1000);
|
94 |
+
|
95 |
+
var formattedTime =
|
96 |
+
(minutes < 10 ? "0" : "") + minutes + ":" +
|
97 |
+
(seconds < 10 ? "0" : "") + seconds;
|
98 |
+
|
99 |
+
var timerElement = document.getElementById("timer-display");
|
100 |
+
if (timerElement) {
|
101 |
+
timerElement.innerHTML = "⏱️ Elapsed: " + formattedTime;
|
102 |
+
}
|
103 |
+
|
104 |
+
// Store current display for potential freezing
|
105 |
+
localStorage.setItem('timerCurrentDisplay', formattedTime);
|
106 |
+
}, 100);
|
107 |
+
|
108 |
+
// Flag that timer is running
|
109 |
+
localStorage.setItem('timerRunning', 'true');
|
110 |
+
}
|
111 |
+
</script>
|
112 |
+
"""
|
113 |
+
return html(timer_html, height=50)
|
114 |
+
|
115 |
+
def freeze_timer():
|
116 |
+
freeze_html = """
|
117 |
+
<script>
|
118 |
+
console.log("Freezing timer");
|
119 |
+
|
120 |
+
// Clear the interval if it exists
|
121 |
+
if (window.timerInterval) {
|
122 |
+
clearInterval(window.timerInterval);
|
123 |
+
window.timerInterval = null;
|
124 |
+
}
|
125 |
+
|
126 |
+
// Get the last timer display
|
127 |
+
var lastTimerDisplay = localStorage.getItem('timerCurrentDisplay') || "00:00";
|
128 |
|
129 |
+
// Update the timer display with frozen styling
|
130 |
+
var timerElement = document.getElementById("timer-display");
|
131 |
+
if (timerElement) {
|
132 |
+
timerElement.style.color = "#00cc00";
|
133 |
+
timerElement.style.fontWeight = "bold";
|
134 |
+
timerElement.innerHTML = "⏱️ Elapsed: " + lastTimerDisplay + " ✓";
|
135 |
+
}
|
136 |
+
|
137 |
+
// Set flag that timer is frozen
|
138 |
+
localStorage.setItem('timerRunning', 'false');
|
139 |
+
</script>
|
140 |
+
"""
|
141 |
+
return html(freeze_html, height=0)
|
142 |
|
143 |
+
def reset_timer():
|
144 |
+
reset_html = """
|
145 |
+
<script>
|
146 |
+
console.log("Resetting timer");
|
147 |
+
|
148 |
+
// Clear any existing interval
|
149 |
+
if (window.timerInterval) {
|
150 |
+
clearInterval(window.timerInterval);
|
151 |
+
window.timerInterval = null;
|
152 |
+
}
|
153 |
+
|
154 |
+
// Clear localStorage timer data
|
155 |
+
localStorage.removeItem('timerStartTime');
|
156 |
+
localStorage.removeItem('timerCurrentDisplay');
|
157 |
+
localStorage.removeItem('timerRunning');
|
158 |
+
|
159 |
+
// Reset the display
|
160 |
+
var timerElement = document.getElementById("timer-display");
|
161 |
+
if (timerElement) {
|
162 |
+
timerElement.style.color = "#666";
|
163 |
+
timerElement.style.fontWeight = "normal";
|
164 |
+
timerElement.innerHTML = "���️ Elapsed: 00:00";
|
165 |
+
}
|
166 |
+
</script>
|
167 |
+
"""
|
168 |
+
return html(reset_html, height=0)
|
169 |
+
|
170 |
+
# Always show timer container
|
171 |
+
timer_container.write(start_timer())
|
172 |
|
173 |
# Always display the image if we have image data
|
174 |
if st.session_state.image_data is not None:
|
175 |
image_container.image(st.session_state.image_data, caption="Uploaded Image", use_container_width=True)
|
176 |
|
177 |
+
# UI components
|
178 |
+
uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
|
|
|
|
|
|
|
|
|
|
|
179 |
|
180 |
# Process new uploaded file
|
181 |
if uploaded_file is not None:
|
|
|
190 |
st.session_state.current_file = uploaded_file.name
|
191 |
|
192 |
# Reset and start timer
|
193 |
+
reset_timer()
|
194 |
+
timer_container.write(start_timer())
|
|
|
195 |
|
196 |
# Progress indicators
|
197 |
status_text = status_container.empty()
|
|
|
204 |
|
205 |
# Stage 1: Image to Text
|
206 |
status_text.markdown("**🖼️ Generating caption...**")
|
|
|
207 |
st.session_state.processed_data['scenario'] = img2text(uploaded_file.name)
|
208 |
progress_bar.progress(33)
|
209 |
|
210 |
# Stage 2: Text to Story
|
211 |
status_text.markdown("**📖 Generating story...**")
|
|
|
212 |
st.session_state.processed_data['story'] = text2story(
|
213 |
st.session_state.processed_data['scenario']
|
214 |
)
|
|
|
216 |
|
217 |
# Stage 3: Story to Audio
|
218 |
status_text.markdown("**🔊 Synthesizing audio...**")
|
|
|
219 |
st.session_state.processed_data['audio'] = text2audio(
|
220 |
st.session_state.processed_data['story']
|
221 |
)
|
|
|
243 |
with results_container:
|
244 |
st.write("**Story:**", st.session_state.processed_data['story'])
|
245 |
|
246 |
+
# Audio playback
|
247 |
if st.button("Play Audio of the Story Generated"):
|
248 |
if st.session_state.processed_data.get('audio'):
|
249 |
# Make sure the image is still displayed
|
250 |
if st.session_state.image_data is not None:
|
251 |
+
image_container.image(st.session_state.image_data, caption="Uploaded Image", use_column_width=True)
|
252 |
|
253 |
# Freeze the timer
|
254 |
+
freeze_timer()
|
|
|
|
|
|
|
|
|
255 |
|
256 |
# Play the audio
|
257 |
audio_data = st.session_state.processed_data['audio']
|
|
|
260 |
format="audio/mp3"
|
261 |
)
|
262 |
else:
|
263 |
+
st.warning("Please generate a story first!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|