Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,15 +3,13 @@ import streamlit as st
|
|
3 |
import time
|
4 |
import random
|
5 |
import textwrap
|
6 |
-
import os
|
7 |
-
import base64
|
8 |
from io import BytesIO
|
9 |
from PIL import Image, ImageDraw, ImageFont
|
10 |
import numpy as np
|
11 |
from gtts import gTTS
|
12 |
-
import tempfile
|
13 |
-
import subprocess
|
14 |
from transformers import pipeline
|
|
|
|
|
15 |
|
16 |
# Set up the page
|
17 |
st.set_page_config(
|
@@ -351,12 +349,23 @@ def generate_code_explanation(story, explanation_generator):
|
|
351 |
we used code like: character.move(). That's how we turn words into actions!"""
|
352 |
|
353 |
def text_to_speech(text, lang='en'):
|
354 |
-
"""Convert text to speech
|
355 |
try:
|
|
|
356 |
tts = gTTS(text=text, lang=lang, slow=False)
|
357 |
-
|
358 |
-
|
359 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
360 |
except Exception as e:
|
361 |
st.error(f"Text-to-speech error: {e}")
|
362 |
return None
|
@@ -546,9 +555,8 @@ with col1:
|
|
546 |
|
547 |
# Generate speech if enabled
|
548 |
if st.session_state.voice_enabled:
|
549 |
-
|
550 |
-
if
|
551 |
-
st.session_state.speech_file = speech_file
|
552 |
unlock_achievement("voice_artist")
|
553 |
|
554 |
# Simulate sound effect
|
@@ -599,17 +607,14 @@ with col2:
|
|
599 |
st.success(f"✨ Your {characters[st.session_state.selected_character]} in the {themes[st.session_state.selected_theme]} world!")
|
600 |
|
601 |
# Play voice explanation
|
602 |
-
if st.session_state.voice_enabled and '
|
603 |
-
|
604 |
-
|
605 |
-
|
606 |
-
|
607 |
-
|
608 |
-
|
609 |
-
|
610 |
-
|
611 |
-
except Exception as e:
|
612 |
-
st.warning(f"Couldn't load audio: {e}")
|
613 |
|
614 |
elif st.session_state.animation_generated:
|
615 |
st.warning("Couldn't generate animation. Try a different story!")
|
|
|
3 |
import time
|
4 |
import random
|
5 |
import textwrap
|
|
|
|
|
6 |
from io import BytesIO
|
7 |
from PIL import Image, ImageDraw, ImageFont
|
8 |
import numpy as np
|
9 |
from gtts import gTTS
|
|
|
|
|
10 |
from transformers import pipeline
|
11 |
+
import base64
|
12 |
+
import os
|
13 |
|
14 |
# Set up the page
|
15 |
st.set_page_config(
|
|
|
349 |
we used code like: character.move(). That's how we turn words into actions!"""
|
350 |
|
351 |
def text_to_speech(text, lang='en'):
|
352 |
+
"""Convert text to speech and return as base64 encoded audio"""
|
353 |
try:
|
354 |
+
# Create gTTS object
|
355 |
tts = gTTS(text=text, lang=lang, slow=False)
|
356 |
+
|
357 |
+
# Save to in-memory bytes buffer
|
358 |
+
audio_bytes = BytesIO()
|
359 |
+
tts.write_to_fp(audio_bytes)
|
360 |
+
audio_bytes.seek(0)
|
361 |
+
|
362 |
+
# Encode as base64
|
363 |
+
b64 = base64.b64encode(audio_bytes.read()).decode()
|
364 |
+
|
365 |
+
# Create data URI for HTML5 audio
|
366 |
+
data_url = f"data:audio/mp3;base64,{b64}"
|
367 |
+
|
368 |
+
return data_url
|
369 |
except Exception as e:
|
370 |
st.error(f"Text-to-speech error: {e}")
|
371 |
return None
|
|
|
555 |
|
556 |
# Generate speech if enabled
|
557 |
if st.session_state.voice_enabled:
|
558 |
+
st.session_state.speech_data = text_to_speech(st.session_state.code_explanation)
|
559 |
+
if st.session_state.speech_data:
|
|
|
560 |
unlock_achievement("voice_artist")
|
561 |
|
562 |
# Simulate sound effect
|
|
|
607 |
st.success(f"✨ Your {characters[st.session_state.selected_character]} in the {themes[st.session_state.selected_theme]} world!")
|
608 |
|
609 |
# Play voice explanation
|
610 |
+
if st.session_state.voice_enabled and 'speech_data' in st.session_state and st.session_state.speech_data:
|
611 |
+
st.markdown("### 🔊 Tavus Explanation")
|
612 |
+
st.markdown(f"""
|
613 |
+
<audio controls autoplay style="width:100%">
|
614 |
+
<source src="{st.session_state.speech_data}" type="audio/mp3">
|
615 |
+
Your browser does not support the audio element.
|
616 |
+
</audio>
|
617 |
+
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
618 |
|
619 |
elif st.session_state.animation_generated:
|
620 |
st.warning("Couldn't generate animation. Try a different story!")
|