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,14 +24,14 @@ 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 |
if 'timer_frozen' not in st.session_state:
|
30 |
st.session_state.timer_frozen = False
|
31 |
|
32 |
-
if '
|
33 |
-
st.session_state.
|
34 |
|
35 |
# Page setup
|
36 |
st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
|
@@ -78,13 +79,23 @@ status_container = st.empty()
|
|
78 |
progress_container = st.empty()
|
79 |
results_container = st.container()
|
80 |
|
81 |
-
#
|
82 |
-
def
|
83 |
-
if st.session_state.timer_start_time
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
# UI components
|
90 |
uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
|
@@ -93,14 +104,13 @@ uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...
|
|
93 |
if st.session_state.image_data is not None:
|
94 |
image_container.image(st.session_state.image_data, caption="Uploaded Image", use_container_width=True)
|
95 |
|
96 |
-
#
|
97 |
-
|
98 |
|
99 |
-
# Style the timer based on whether it's frozen
|
100 |
if st.session_state.timer_frozen:
|
101 |
-
timer_container.markdown(f"<div style='font-size:16px;color:#00cc00;font-weight:bold;margin-bottom:10px;'>⏱️ Elapsed: {
|
102 |
else:
|
103 |
-
timer_container.markdown(f"<div style='font-size:16px;color:#666;margin-bottom:10px;'>⏱️ Elapsed: {
|
104 |
|
105 |
# Process new uploaded file
|
106 |
if uploaded_file is not None:
|
@@ -116,8 +126,8 @@ if uploaded_file is not None:
|
|
116 |
|
117 |
# Reset and start timer
|
118 |
st.session_state.timer_start_time = time.time()
|
|
|
119 |
st.session_state.timer_frozen = False
|
120 |
-
st.session_state.timer_value = "00:00"
|
121 |
|
122 |
# Progress indicators
|
123 |
status_text = status_container.empty()
|
@@ -172,7 +182,7 @@ if st.session_state.processed_data.get('story'):
|
|
172 |
with results_container:
|
173 |
st.write("**Story:**", st.session_state.processed_data['story'])
|
174 |
|
175 |
-
# Audio playback
|
176 |
if st.button("Play Audio of the Story Generated"):
|
177 |
if st.session_state.processed_data.get('audio'):
|
178 |
# Make sure the image is still displayed
|
@@ -182,8 +192,9 @@ if st.button("Play Audio of the Story Generated"):
|
|
182 |
# Freeze the timer
|
183 |
st.session_state.timer_frozen = True
|
184 |
|
185 |
-
#
|
186 |
-
|
|
|
187 |
|
188 |
# Play the audio
|
189 |
audio_data = st.session_state.processed_data['audio']
|
@@ -194,7 +205,21 @@ if st.button("Play Audio of the Story Generated"):
|
|
194 |
else:
|
195 |
st.warning("Please generate a story first!")
|
196 |
|
197 |
-
#
|
198 |
-
if st.session_state.timer_start_time and not st.session_state.timer_frozen:
|
199 |
-
|
200 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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_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="🦜")
|
|
|
79 |
progress_container = st.empty()
|
80 |
results_container = st.container()
|
81 |
|
82 |
+
# Get current timer value
|
83 |
+
def get_formatted_timer():
|
84 |
+
if st.session_state.timer_start_time is None:
|
85 |
+
return "00:00"
|
86 |
+
|
87 |
+
current_time = time.time()
|
88 |
+
if st.session_state.timer_frozen:
|
89 |
+
# Use the last update time if timer is frozen
|
90 |
+
elapsed_seconds = int(st.session_state.last_update_time - st.session_state.timer_start_time)
|
91 |
+
else:
|
92 |
+
elapsed_seconds = int(current_time - st.session_state.timer_start_time)
|
93 |
+
# Update the last update time
|
94 |
+
st.session_state.last_update_time = current_time
|
95 |
+
|
96 |
+
minutes = elapsed_seconds // 60
|
97 |
+
seconds = elapsed_seconds % 60
|
98 |
+
return f"{minutes:02d}:{seconds:02d}"
|
99 |
|
100 |
# UI components
|
101 |
uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
|
|
|
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 |
+
# Display timer - update the display based on current state
|
108 |
+
current_time_str = get_formatted_timer()
|
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:
|
|
|
126 |
|
127 |
# Reset and start timer
|
128 |
st.session_state.timer_start_time = time.time()
|
129 |
+
st.session_state.last_update_time = time.time()
|
130 |
st.session_state.timer_frozen = False
|
|
|
131 |
|
132 |
# Progress indicators
|
133 |
status_text = status_container.empty()
|
|
|
182 |
with results_container:
|
183 |
st.write("**Story:**", st.session_state.processed_data['story'])
|
184 |
|
185 |
+
# Audio playback - this will freeze the timer
|
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
|
|
|
192 |
# Freeze the timer
|
193 |
st.session_state.timer_frozen = True
|
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']
|
|
|
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 |
+
)
|