Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -21,53 +21,64 @@ if 'processed_data' not in st.session_state:
|
|
21 |
'audio': None
|
22 |
}
|
23 |
|
|
|
24 |
# Modified JavaScript timer component
|
25 |
def timer():
|
26 |
return """
|
27 |
-
<
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
existingTimer.remove();
|
32 |
-
clearInterval(window.parent.timerInterval);
|
33 |
-
}
|
34 |
-
|
35 |
-
// Create new timer element in parent document
|
36 |
-
var timerDiv = window.parent.document.createElement('div');
|
37 |
-
timerDiv.id = 'parent-timer';
|
38 |
-
timerDiv.style.cssText = `
|
39 |
font-size: 16px;
|
40 |
color: #666;
|
41 |
-
|
42 |
-
padding: 8px;
|
43 |
background: #f0f2f6;
|
44 |
-
border-radius:
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
57 |
}
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
}, 1000);
|
68 |
|
69 |
-
//
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
</script>
|
72 |
"""
|
73 |
|
@@ -112,14 +123,14 @@ def text2audio(story_text):
|
|
112 |
|
113 |
# UI components
|
114 |
uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
|
115 |
-
|
116 |
if uploaded_file is not None:
|
117 |
# Initialize progress containers
|
118 |
status_text = st.empty()
|
119 |
progress_bar = st.progress(0)
|
120 |
|
121 |
# Start JavaScript timer
|
122 |
-
html(timer(), height=
|
123 |
|
124 |
try:
|
125 |
# Save uploaded file
|
@@ -134,7 +145,7 @@ if uploaded_file is not None:
|
|
134 |
st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
|
135 |
|
136 |
# Stage 1: Image to Text
|
137 |
-
status_text.markdown("**🖼️
|
138 |
progress_bar.progress(0)
|
139 |
st.session_state.processed_data['scenario'] = img2text(uploaded_file.name)
|
140 |
progress_bar.progress(33)
|
@@ -157,15 +168,7 @@ if uploaded_file is not None:
|
|
157 |
|
158 |
# Final status and stop timer
|
159 |
status_text.success("**✅ Generation complete!**")
|
160 |
-
html(
|
161 |
-
<script>
|
162 |
-
clearInterval(window.parent.timerInterval);
|
163 |
-
var timerDiv = window.parent.document.getElementById('parent-timer');
|
164 |
-
if (timerDiv) {
|
165 |
-
timerDiv.style.color = '#00cc00';
|
166 |
-
}
|
167 |
-
</script>
|
168 |
-
""")
|
169 |
|
170 |
# Show results
|
171 |
# st.subheader("Results")
|
@@ -173,15 +176,7 @@ if uploaded_file is not None:
|
|
173 |
st.write("**Story:**", st.session_state.processed_data['story'])
|
174 |
|
175 |
except Exception as e:
|
176 |
-
html(
|
177 |
-
<script>
|
178 |
-
clearInterval(window.parent.timerInterval);
|
179 |
-
var timerDiv = window.parent.document.getElementById('parent-timer');
|
180 |
-
if (timerDiv) {
|
181 |
-
timerDiv.remove();
|
182 |
-
}
|
183 |
-
</script>
|
184 |
-
""")
|
185 |
status_text.error(f"**❌ Error:** {str(e)}")
|
186 |
progress_bar.empty()
|
187 |
raise e
|
|
|
21 |
'audio': None
|
22 |
}
|
23 |
|
24 |
+
|
25 |
# Modified JavaScript timer component
|
26 |
def timer():
|
27 |
return """
|
28 |
+
<div id="main-timer" style="
|
29 |
+
position: fixed;
|
30 |
+
top: 20px;
|
31 |
+
right: 20px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
font-size: 16px;
|
33 |
color: #666;
|
34 |
+
padding: 8px 15px;
|
|
|
35 |
background: #f0f2f6;
|
36 |
+
border-radius: 20px;
|
37 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
38 |
+
z-index: 1000000;
|
39 |
+
border: 1px solid #ddd;
|
40 |
+
">
|
41 |
+
⏱️ <span id="time-display">00:00</span>
|
42 |
+
</div>
|
43 |
+
<script>
|
44 |
+
// Timer system
|
45 |
+
let shouldRun = true;
|
46 |
+
const startTime = Date.now();
|
47 |
+
const timerElement = document.getElementById('time-display');
|
48 |
|
49 |
+
function updateTimer() {
|
50 |
+
if (!shouldRun) return;
|
51 |
+
|
52 |
+
const elapsed = Math.floor((Date.now() - startTime) / 1000);
|
53 |
+
const minutes = Math.floor(elapsed / 60).toString().padStart(2, '0');
|
54 |
+
const seconds = (elapsed % 60).toString().padStart(2, '0');
|
55 |
+
|
56 |
+
timerElement.textContent = `${minutes}:${seconds}`;
|
57 |
+
requestAnimationFrame(updateTimer);
|
58 |
}
|
59 |
|
60 |
+
// Store control in parent window
|
61 |
+
window.parent.timerControls = {
|
62 |
+
stop: () => {
|
63 |
+
shouldRun = false;
|
64 |
+
document.getElementById('main-timer').style.backgroundColor = '#e6ffe6';
|
65 |
+
document.getElementById('main-timer').style.borderColor = '#99ff99';
|
66 |
+
}
|
67 |
+
};
|
|
|
68 |
|
69 |
+
// Start updates
|
70 |
+
updateTimer();
|
71 |
+
</script>
|
72 |
+
"""
|
73 |
+
|
74 |
+
# Modified stop command
|
75 |
+
def stop_timer():
|
76 |
+
return """
|
77 |
+
<script>
|
78 |
+
// Safely stop timer if exists
|
79 |
+
if (window.parent.timerControls) {
|
80 |
+
window.parent.timerControls.stop();
|
81 |
+
}
|
82 |
</script>
|
83 |
"""
|
84 |
|
|
|
123 |
|
124 |
# UI components
|
125 |
uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
|
126 |
+
|
127 |
if uploaded_file is not None:
|
128 |
# Initialize progress containers
|
129 |
status_text = st.empty()
|
130 |
progress_bar = st.progress(0)
|
131 |
|
132 |
# Start JavaScript timer
|
133 |
+
html(timer(), height=0)
|
134 |
|
135 |
try:
|
136 |
# Save uploaded file
|
|
|
145 |
st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
|
146 |
|
147 |
# Stage 1: Image to Text
|
148 |
+
status_text.markdown("**🖼️ Generating caption...**")
|
149 |
progress_bar.progress(0)
|
150 |
st.session_state.processed_data['scenario'] = img2text(uploaded_file.name)
|
151 |
progress_bar.progress(33)
|
|
|
168 |
|
169 |
# Final status and stop timer
|
170 |
status_text.success("**✅ Generation complete!**")
|
171 |
+
html(stop_timer())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
|
173 |
# Show results
|
174 |
# st.subheader("Results")
|
|
|
176 |
st.write("**Story:**", st.session_state.processed_data['story'])
|
177 |
|
178 |
except Exception as e:
|
179 |
+
html(stop_timer())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
status_text.error(f"**❌ Error:** {str(e)}")
|
181 |
progress_bar.empty()
|
182 |
raise e
|