Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,7 @@ import io
|
|
9 |
import time
|
10 |
from streamlit.components.v1 import html
|
11 |
import asyncio
|
|
|
12 |
|
13 |
if not asyncio.get_event_loop().is_running():
|
14 |
asyncio.set_event_loop(asyncio.new_event_loop())
|
@@ -21,17 +22,21 @@ if 'processed_data' not in st.session_state:
|
|
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 updateTimer() {
|
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,21 +44,33 @@ def timer():
|
|
39 |
(minutes < 10 ? '0' : '') + minutes + ':' +
|
40 |
(seconds < 10 ? '0' : '') + seconds;
|
41 |
}, 1000);
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
46 |
}
|
47 |
}
|
48 |
-
|
|
|
49 |
|
50 |
// Handle Streamlit's component cleanup
|
51 |
window.addEventListener('beforeunload', function() {
|
52 |
-
|
53 |
});
|
54 |
</script>
|
55 |
"""
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
# Page setup
|
59 |
st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
|
@@ -98,17 +115,19 @@ def text2audio(story_text):
|
|
98 |
uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
|
99 |
|
100 |
if uploaded_file is not None:
|
|
|
|
|
|
|
|
|
101 |
# Initialize progress containers
|
102 |
status_text = st.empty()
|
103 |
progress_bar = st.progress(0)
|
104 |
|
105 |
-
|
106 |
html(timer(), height=50)
|
107 |
|
108 |
-
|
109 |
try:
|
110 |
# Save uploaded file
|
111 |
-
bytes_data = uploaded_file.getvalue()
|
112 |
with open(uploaded_file.name, "wb") as file:
|
113 |
file.write(bytes_data)
|
114 |
|
@@ -142,22 +161,30 @@ if uploaded_file is not None:
|
|
142 |
|
143 |
# Final status and stop timer
|
144 |
status_text.success("**✅ Generation complete!**")
|
145 |
-
|
146 |
|
147 |
# Show results
|
148 |
-
# st.subheader("Results")
|
149 |
st.write("**Caption:**", st.session_state.processed_data['scenario'])
|
150 |
st.write("**Story:**", st.session_state.processed_data['story'])
|
151 |
|
152 |
except Exception as e:
|
153 |
-
|
154 |
status_text.error(f"**❌ Error:** {str(e)}")
|
155 |
progress_bar.empty()
|
156 |
raise e
|
157 |
-
|
158 |
|
159 |
finally:
|
160 |
-
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
|
163 |
# Audio playback
|
|
|
9 |
import time
|
10 |
from streamlit.components.v1 import html
|
11 |
import asyncio
|
12 |
+
import base64
|
13 |
|
14 |
if not asyncio.get_event_loop().is_running():
|
15 |
asyncio.set_event_loop(asyncio.new_event_loop())
|
|
|
22 |
'audio': None
|
23 |
}
|
24 |
|
25 |
+
if 'image_data' not in st.session_state:
|
26 |
+
st.session_state.image_data = None
|
27 |
|
28 |
+
# JavaScript timer component with stop function
|
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 |
+
var timerInterval;
|
34 |
+
|
35 |
function updateTimer() {
|
36 |
var start = Date.now();
|
37 |
var timerElement = document.getElementById('timer');
|
38 |
|
39 |
+
timerInterval = setInterval(function() {
|
40 |
var elapsed = Date.now() - start;
|
41 |
var minutes = Math.floor(elapsed / 60000);
|
42 |
var seconds = Math.floor((elapsed % 60000) / 1000);
|
|
|
44 |
(minutes < 10 ? '0' : '') + minutes + ':' +
|
45 |
(seconds < 10 ? '0' : '') + seconds;
|
46 |
}, 1000);
|
47 |
+
}
|
48 |
+
|
49 |
+
function stopTimer() {
|
50 |
+
if (timerInterval) {
|
51 |
+
clearInterval(timerInterval);
|
52 |
+
document.getElementById('timer').style.color = '#00cc00';
|
53 |
}
|
54 |
}
|
55 |
+
|
56 |
+
updateTimer();
|
57 |
|
58 |
// Handle Streamlit's component cleanup
|
59 |
window.addEventListener('beforeunload', function() {
|
60 |
+
if (timerInterval) clearInterval(timerInterval);
|
61 |
});
|
62 |
</script>
|
63 |
"""
|
64 |
|
65 |
+
# Stop timer function
|
66 |
+
def stop_timer():
|
67 |
+
html("""
|
68 |
+
<script>
|
69 |
+
if (typeof stopTimer === 'function') {
|
70 |
+
stopTimer();
|
71 |
+
}
|
72 |
+
</script>
|
73 |
+
""", height=0)
|
74 |
|
75 |
# Page setup
|
76 |
st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
|
|
|
115 |
uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
|
116 |
|
117 |
if uploaded_file is not None:
|
118 |
+
# Save the image data to session state
|
119 |
+
bytes_data = uploaded_file.getvalue()
|
120 |
+
st.session_state.image_data = bytes_data
|
121 |
+
|
122 |
# Initialize progress containers
|
123 |
status_text = st.empty()
|
124 |
progress_bar = st.progress(0)
|
125 |
|
126 |
+
# Start JavaScript timer
|
127 |
html(timer(), height=50)
|
128 |
|
|
|
129 |
try:
|
130 |
# Save uploaded file
|
|
|
131 |
with open(uploaded_file.name, "wb") as file:
|
132 |
file.write(bytes_data)
|
133 |
|
|
|
161 |
|
162 |
# Final status and stop timer
|
163 |
status_text.success("**✅ Generation complete!**")
|
164 |
+
stop_timer()
|
165 |
|
166 |
# Show results
|
|
|
167 |
st.write("**Caption:**", st.session_state.processed_data['scenario'])
|
168 |
st.write("**Story:**", st.session_state.processed_data['story'])
|
169 |
|
170 |
except Exception as e:
|
171 |
+
stop_timer()
|
172 |
status_text.error(f"**❌ Error:** {str(e)}")
|
173 |
progress_bar.empty()
|
174 |
raise e
|
|
|
175 |
|
176 |
finally:
|
177 |
+
pass
|
178 |
+
|
179 |
+
elif st.session_state.image_data is not None:
|
180 |
+
# Display the previously uploaded image from session state
|
181 |
+
st.image(st.session_state.image_data, caption="Uploaded Image", use_container_width=True)
|
182 |
+
|
183 |
+
# Show previous results if available
|
184 |
+
if st.session_state.processed_data.get('scenario'):
|
185 |
+
st.write("**Caption:**", st.session_state.processed_data['scenario'])
|
186 |
+
if st.session_state.processed_data.get('story'):
|
187 |
+
st.write("**Story:**", st.session_state.processed_data['story'])
|
188 |
|
189 |
|
190 |
# Audio playback
|