Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import torch
|
|
4 |
from gtts import gTTS
|
5 |
import io
|
6 |
import time
|
|
|
7 |
|
8 |
# Initialize session state
|
9 |
if 'processed_data' not in st.session_state:
|
@@ -13,29 +14,42 @@ if 'processed_data' not in st.session_state:
|
|
13 |
'audio': None
|
14 |
}
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
# Page setup
|
24 |
st.set_page_config(page_title="Your Image to Audio Story", page_icon="π¦")
|
25 |
st.header("Turn Your Image to a Short Audio Story for Children")
|
26 |
|
27 |
-
# Helper functions
|
28 |
-
def update_progress(stage, percentage):
|
29 |
-
st.session_state.progress.update({
|
30 |
-
'current_stage': stage,
|
31 |
-
'percent': percentage,
|
32 |
-
'start_time': time.time() if percentage == 0 else st.session_state.progress['start_time']
|
33 |
-
})
|
34 |
-
|
35 |
-
def get_elapsed_time():
|
36 |
-
elapsed = time.time() - st.session_state.progress['start_time']
|
37 |
-
return f"{int(elapsed//60):02d}:{int(elapsed%60):02d}"
|
38 |
-
|
39 |
# Model loading
|
40 |
@st.cache_resource
|
41 |
def load_models():
|
@@ -58,7 +72,7 @@ def text2story(text):
|
|
58 |
]
|
59 |
response = models["story_model"](
|
60 |
messages,
|
61 |
-
max_new_tokens=
|
62 |
do_sample=True,
|
63 |
temperature=0.7
|
64 |
)[0]["generated_text"]
|
@@ -78,7 +92,9 @@ if uploaded_file is not None:
|
|
78 |
# Initialize progress containers
|
79 |
status_text = st.empty()
|
80 |
progress_bar = st.progress(0)
|
81 |
-
|
|
|
|
|
82 |
|
83 |
try:
|
84 |
# Save uploaded file
|
@@ -88,47 +104,35 @@ if uploaded_file is not None:
|
|
88 |
|
89 |
if st.session_state.get('current_file') != uploaded_file.name:
|
90 |
st.session_state.current_file = uploaded_file.name
|
91 |
-
update_progress("Starting processing...", 0)
|
92 |
|
93 |
# Display image
|
94 |
st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
|
95 |
|
96 |
# Stage 1: Image to Text
|
97 |
-
|
98 |
-
|
99 |
-
**{st.session_state.progress['current_stage']}**
|
100 |
-
β±οΈ Elapsed: {get_elapsed_time()}
|
101 |
-
""")
|
102 |
st.session_state.processed_data['scenario'] = img2text(uploaded_file.name)
|
103 |
progress_bar.progress(33)
|
104 |
|
105 |
# Stage 2: Text to Story
|
106 |
-
|
107 |
-
|
108 |
-
**{st.session_state.progress['current_stage']}**
|
109 |
-
β±οΈ Elapsed: {get_elapsed_time()}
|
110 |
-
""")
|
111 |
st.session_state.processed_data['story'] = text2story(
|
112 |
st.session_state.processed_data['scenario']
|
113 |
)
|
114 |
progress_bar.progress(66)
|
115 |
|
116 |
# Stage 3: Story to Audio
|
117 |
-
|
118 |
-
|
119 |
-
**{st.session_state.progress['current_stage']}**
|
120 |
-
β±οΈ Elapsed: {get_elapsed_time()}
|
121 |
-
""")
|
122 |
st.session_state.processed_data['audio'] = text2audio(
|
123 |
st.session_state.processed_data['story']
|
124 |
)
|
125 |
progress_bar.progress(100)
|
126 |
|
127 |
# Final status
|
128 |
-
status_text.success(
|
129 |
-
|
130 |
-
Total time: {get_elapsed_time()}
|
131 |
-
""")
|
132 |
|
133 |
# Show results
|
134 |
st.subheader("Results")
|
@@ -136,12 +140,13 @@ if uploaded_file is not None:
|
|
136 |
st.write("**Story:**", st.session_state.processed_data['story'])
|
137 |
|
138 |
except Exception as e:
|
139 |
-
|
|
|
140 |
progress_bar.empty()
|
141 |
raise e
|
142 |
|
143 |
finally:
|
144 |
-
|
145 |
|
146 |
# Audio playback
|
147 |
if st.button("Play Audio of the Story Generated"):
|
|
|
4 |
from gtts import gTTS
|
5 |
import io
|
6 |
import time
|
7 |
+
from streamlit.components.v1 import html
|
8 |
|
9 |
# Initialize session state
|
10 |
if 'processed_data' not in st.session_state:
|
|
|
14 |
'audio': None
|
15 |
}
|
16 |
|
17 |
+
# JavaScript timer component
|
18 |
+
def timer():
|
19 |
+
return """
|
20 |
+
<div id="timer" style="font-size:16px;color:#666;margin-bottom:10px;">β±οΈ Elapsed: 00:00</div>
|
21 |
+
<script>
|
22 |
+
function updateTimer() {
|
23 |
+
var start = Date.now();
|
24 |
+
var timerElement = document.getElementById('timer');
|
25 |
+
|
26 |
+
var interval = setInterval(function() {
|
27 |
+
var elapsed = Date.now() - start;
|
28 |
+
var minutes = Math.floor(elapsed / 60000);
|
29 |
+
var seconds = Math.floor((elapsed % 60000) / 1000);
|
30 |
+
timerElement.innerHTML = 'β±οΈ Elapsed: ' +
|
31 |
+
(minutes < 10 ? '0' : '') + minutes + ':' +
|
32 |
+
(seconds < 10 ? '0' : '') + seconds;
|
33 |
+
}, 1000);
|
34 |
+
|
35 |
+
// Cleanup when component is removed
|
36 |
+
return function() {
|
37 |
+
clearInterval(interval);
|
38 |
+
}
|
39 |
}
|
40 |
+
var cleanup = updateTimer();
|
41 |
+
|
42 |
+
// Handle Streamlit's component cleanup
|
43 |
+
window.addEventListener('beforeunload', function() {
|
44 |
+
cleanup();
|
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")
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
# Model loading
|
54 |
@st.cache_resource
|
55 |
def load_models():
|
|
|
72 |
]
|
73 |
response = models["story_model"](
|
74 |
messages,
|
75 |
+
max_new_tokens=128,
|
76 |
do_sample=True,
|
77 |
temperature=0.7
|
78 |
)[0]["generated_text"]
|
|
|
92 |
# Initialize progress containers
|
93 |
status_text = st.empty()
|
94 |
progress_bar = st.progress(0)
|
95 |
+
|
96 |
+
# Start JavaScript timer
|
97 |
+
html(timer(), height=50)
|
98 |
|
99 |
try:
|
100 |
# Save uploaded file
|
|
|
104 |
|
105 |
if st.session_state.get('current_file') != uploaded_file.name:
|
106 |
st.session_state.current_file = uploaded_file.name
|
|
|
107 |
|
108 |
# Display image
|
109 |
st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
|
110 |
|
111 |
# Stage 1: Image to Text
|
112 |
+
status_text.markdown("**πΌοΈ Analyzing image...**")
|
113 |
+
progress_bar.progress(0)
|
|
|
|
|
|
|
114 |
st.session_state.processed_data['scenario'] = img2text(uploaded_file.name)
|
115 |
progress_bar.progress(33)
|
116 |
|
117 |
# Stage 2: Text to Story
|
118 |
+
status_text.markdown("**π Generating story...**")
|
119 |
+
progress_bar.progress(33)
|
|
|
|
|
|
|
120 |
st.session_state.processed_data['story'] = text2story(
|
121 |
st.session_state.processed_data['scenario']
|
122 |
)
|
123 |
progress_bar.progress(66)
|
124 |
|
125 |
# Stage 3: Story to Audio
|
126 |
+
status_text.markdown("**π Synthesizing audio...**")
|
127 |
+
progress_bar.progress(66)
|
|
|
|
|
|
|
128 |
st.session_state.processed_data['audio'] = text2audio(
|
129 |
st.session_state.processed_data['story']
|
130 |
)
|
131 |
progress_bar.progress(100)
|
132 |
|
133 |
# Final status
|
134 |
+
status_text.success("**β
Generation complete!**")
|
135 |
+
html("<script>document.getElementById('timer').style.color = '#00cc00';</script>")
|
|
|
|
|
136 |
|
137 |
# Show results
|
138 |
st.subheader("Results")
|
|
|
140 |
st.write("**Story:**", st.session_state.processed_data['story'])
|
141 |
|
142 |
except Exception as e:
|
143 |
+
html("<script>document.getElementById('timer').remove();</script>")
|
144 |
+
status_text.error(f"**β Error:** {str(e)}")
|
145 |
progress_bar.empty()
|
146 |
raise e
|
147 |
|
148 |
finally:
|
149 |
+
pass # Timer cleanup handled by JavaScript
|
150 |
|
151 |
# Audio playback
|
152 |
if st.button("Play Audio of the Story Generated"):
|