sunbal7 commited on
Commit
898c662
·
verified ·
1 Parent(s): db4be6b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -22
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 using gTTS"""
355
  try:
 
356
  tts = gTTS(text=text, lang=lang, slow=False)
357
- with tempfile.NamedTemporaryFile(delete=True, suffix='.mp3') as fp:
358
- tts.save(fp.name)
359
- return fp.name
 
 
 
 
 
 
 
 
 
 
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
- speech_file = text_to_speech(st.session_state.code_explanation)
550
- if speech_file:
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 'speech_file' in st.session_state:
603
- try:
604
- # Read audio file and convert to base64
605
- with open(st.session_state.speech_file, "rb") as f:
606
- audio_bytes = f.read()
607
-
608
- # Display audio player
609
- st.audio(audio_bytes, format='audio/mp3')
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!")