Spaces:
Sleeping
Sleeping
File size: 9,171 Bytes
7f6c08d 7352851 e8b9998 7352851 7f6c08d c337535 05fbc95 7f6c08d c337535 7352851 72ca55a ef13d6a 89a5c7c 7352851 e8b9998 7352851 e1a461b 7352851 e8b9998 7352851 66a966e 2521826 66a966e 89a5c7c c337535 89a5c7c c337535 29d3b0a c337535 89a5c7c 66a966e c337535 89a5c7c 66a966e c337535 52cdbd8 66a966e 7352851 72ca55a 66a966e dd1055c 66a966e 52cdbd8 c337535 89a5c7c 66a966e 7352851 ef13d6a 7352851 66a966e 7352851 dd1055c 7352851 66a966e 7352851 dd1055c 7352851 66a966e dd1055c 66a966e 89a5c7c 7bdb95c 2521826 c337535 7352851 66a966e 89a5c7c 2521826 89a5c7c 52cdbd8 2521826 7352851 c337535 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
import nest_asyncio
nest_asyncio.apply()
import streamlit as st
from transformers import pipeline
import torch
from gtts import gTTS
import io
import time
import asyncio
from streamlit.components.v1 import html
if not asyncio.get_event_loop().is_running():
asyncio.set_event_loop(asyncio.new_event_loop())
# Initialize session state
if 'processed_data' not in st.session_state:
st.session_state.processed_data = {
'scenario': None,
'story': None,
'audio': None
}
if 'image_data' not in st.session_state:
st.session_state.image_data = None
if 'timer_started' not in st.session_state:
st.session_state.timer_started = False
# Page setup
st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
st.header("Turn Your Image to a Short Audio Story for Children")
# Model loading
@st.cache_resource
def load_models():
return {
"img_model": pipeline("image-to-text", "cnmoro/tiny-image-captioning"),
"story_model": pipeline("text-generation", "Qwen/Qwen2.5-0.5B-Instruct")
}
models = load_models()
# Processing functions
def img2text(url):
return models["img_model"](url)[0]["generated_text"]
def text2story(text):
prompt = f"Generate a 100-word story about: {text}"
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
]
response = models["story_model"](
messages,
max_new_tokens=100,
do_sample=True,
temperature=0.7
)[0]["generated_text"]
return response[2]["content"]
def text2audio(story_text):
audio_io = io.BytesIO()
tts = gTTS(text=story_text, lang='en', slow=False)
tts.write_to_fp(audio_io)
audio_io.seek(0)
return {'audio': audio_io, 'sampling_rate': 16000}
# Create fixed containers for UI elements
image_container = st.empty()
timer_container = st.empty()
status_container = st.empty()
progress_container = st.empty()
results_container = st.container()
# Display initial timer placeholder (empty timer)
timer_container.markdown(
'<div id="timer-display" style="font-size:16px;color:#666;margin-bottom:10px;">⏱️ Elapsed: 00:00</div>',
unsafe_allow_html=True
)
# JavaScript timer functions
def start_timer():
timer_html = """
<script>
// Only create a timer if one doesn't exist already
if (!window.timerInterval) {
console.log("Starting timer");
var startTime = new Date().getTime();
// Store the start time in localStorage to persist across reruns
localStorage.setItem('timerStartTime', startTime);
window.timerInterval = setInterval(function() {
var now = new Date().getTime();
var startTimeFromStorage = parseInt(localStorage.getItem('timerStartTime') || startTime);
var elapsed = now - startTimeFromStorage;
var minutes = Math.floor(elapsed / (1000 * 60));
var seconds = Math.floor((elapsed % (1000 * 60)) / 1000);
var formattedTime =
(minutes < 10 ? "0" : "") + minutes + ":" +
(seconds < 10 ? "0" : "") + seconds;
var timerElement = document.getElementById("timer-display");
if (timerElement) {
timerElement.innerHTML = "⏱️ Elapsed: " + formattedTime;
}
// Store current display for potential freezing
localStorage.setItem('timerCurrentDisplay', formattedTime);
}, 100);
// Flag that timer is running
localStorage.setItem('timerRunning', 'true');
}
</script>
"""
html(timer_html, height=0)
def freeze_timer():
freeze_html = """
<script>
console.log("Freezing timer");
// Clear the interval if it exists
if (window.timerInterval) {
clearInterval(window.timerInterval);
window.timerInterval = null;
}
// Get the last timer display
var lastTimerDisplay = localStorage.getItem('timerCurrentDisplay') || "00:00";
// Update the timer display with frozen styling
var timerElement = document.getElementById("timer-display");
if (timerElement) {
timerElement.style.color = "#00cc00";
timerElement.style.fontWeight = "bold";
timerElement.innerHTML = "⏱️ Elapsed: " + lastTimerDisplay + " ✓";
}
// Set flag that timer is frozen
localStorage.setItem('timerRunning', 'false');
</script>
"""
html(freeze_html, height=0)
def reset_timer():
reset_html = """
<script>
console.log("Resetting timer");
// Clear any existing interval
if (window.timerInterval) {
clearInterval(window.timerInterval);
window.timerInterval = null;
}
// Clear localStorage timer data
localStorage.removeItem('timerStartTime');
localStorage.removeItem('timerCurrentDisplay');
localStorage.removeItem('timerRunning');
// Reset the display
var timerElement = document.getElementById("timer-display");
if (timerElement) {
timerElement.style.color = "#666";
timerElement.style.fontWeight = "normal";
timerElement.innerHTML = "⏱️ Elapsed: 00:00";
}
</script>
"""
html(reset_html, height=0)
# Always display the image if we have image data
if st.session_state.image_data is not None:
image_container.image(st.session_state.image_data, caption="Uploaded Image", use_container_width=True)
# UI components
uploaded_file = st.file_uploader("Select an Image After the Models are Loaded...")
# Process new uploaded file
if uploaded_file is not None:
# Save the image data to session state
bytes_data = uploaded_file.getvalue()
st.session_state.image_data = bytes_data
# Display the image
image_container.image(bytes_data, caption="Uploaded Image", use_container_width=True)
if st.session_state.get('current_file') != uploaded_file.name:
st.session_state.current_file = uploaded_file.name
# Reset and start timer
reset_timer()
start_timer() # Start the timer ONLY when a new file is uploaded
st.session_state.timer_started = True
# Progress indicators
status_text = status_container.empty()
progress_bar = progress_container.progress(0)
try:
# Save uploaded file
with open(uploaded_file.name, "wb") as file:
file.write(bytes_data)
# Stage 1: Image to Text
status_text.markdown("**🖼️ Generating caption...**")
st.session_state.processed_data['scenario'] = img2text(uploaded_file.name)
progress_bar.progress(33)
# Stage 2: Text to Story
status_text.markdown("**📖 Generating story...**")
st.session_state.processed_data['story'] = text2story(
st.session_state.processed_data['scenario']
)
progress_bar.progress(66)
# Stage 3: Story to Audio
status_text.markdown("**🔊 Synthesizing audio...**")
st.session_state.processed_data['audio'] = text2audio(
st.session_state.processed_data['story']
)
progress_bar.progress(100)
# Final status
status_text.success("**✅ Generation complete!**")
# Show results
with results_container:
st.write("**Caption:**", st.session_state.processed_data['scenario'])
st.write("**Story:**", st.session_state.processed_data['story'])
except Exception as e:
status_text.error(f"**❌ Error:** {str(e)}")
progress_bar.empty()
raise e
# If we have a previously started timer but a new page load, restart it
elif st.session_state.timer_started:
# Don't reset, just restart the timer to continue from where it was
start_timer()
# Display results if available
if st.session_state.processed_data.get('scenario'):
with results_container:
st.write("**Caption:**", st.session_state.processed_data['scenario'])
if st.session_state.processed_data.get('story'):
with results_container:
st.write("**Story:**", st.session_state.processed_data['story'])
# Audio playback
if st.button("Play Audio of the Story Generated"):
if st.session_state.processed_data.get('audio'):
# Make sure the image is still displayed
if st.session_state.image_data is not None:
image_container.image(st.session_state.image_data, caption="Uploaded Image", use_container_width=True)
# Freeze the timer if it was started
if st.session_state.timer_started:
freeze_timer()
# Play the audio
audio_data = st.session_state.processed_data['audio']
st.audio(
audio_data['audio'].getvalue(),
format="audio/mp3"
)
else:
st.warning("Please generate a story first!") |