Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,9 +3,19 @@ import streamlit as st
|
|
| 3 |
from transformers import pipeline
|
| 4 |
from PIL import Image
|
| 5 |
import torch
|
| 6 |
-
from gtts import gTTS
|
| 7 |
import os
|
| 8 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Simple image-to-text function
|
| 11 |
def img2text(image):
|
|
@@ -56,7 +66,7 @@ def text2story(text):
|
|
| 56 |
# If no good ending is found, return as is
|
| 57 |
return story_text
|
| 58 |
|
| 59 |
-
# Updated text-to-audio function using gTTS
|
| 60 |
def text2audio(story_text):
|
| 61 |
# Create a temporary file
|
| 62 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
|
|
@@ -106,4 +116,30 @@ if uploaded_file is not None:
|
|
| 106 |
st.audio(audio_bytes, format='audio/mp3')
|
| 107 |
except Exception as e:
|
| 108 |
st.error(f"Error generating or playing audio: {e}")
|
| 109 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from transformers import pipeline
|
| 4 |
from PIL import Image
|
| 5 |
import torch
|
|
|
|
| 6 |
import os
|
| 7 |
import tempfile
|
| 8 |
+
import sys
|
| 9 |
+
import subprocess
|
| 10 |
+
|
| 11 |
+
# Try to import gTTS, install if missing
|
| 12 |
+
try:
|
| 13 |
+
from gtts import gTTS
|
| 14 |
+
except ImportError:
|
| 15 |
+
st.warning("Installing required package: gTTS...")
|
| 16 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "gTTS"])
|
| 17 |
+
from gtts import gTTS
|
| 18 |
+
st.success("gTTS installed successfully!")
|
| 19 |
|
| 20 |
# Simple image-to-text function
|
| 21 |
def img2text(image):
|
|
|
|
| 66 |
# If no good ending is found, return as is
|
| 67 |
return story_text
|
| 68 |
|
| 69 |
+
# Updated text-to-audio function using gTTS
|
| 70 |
def text2audio(story_text):
|
| 71 |
# Create a temporary file
|
| 72 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
|
|
|
|
| 116 |
st.audio(audio_bytes, format='audio/mp3')
|
| 117 |
except Exception as e:
|
| 118 |
st.error(f"Error generating or playing audio: {e}")
|
| 119 |
+
st.info("If you're having issues with gTTS, you might need to manually install it with: pip install gTTS")
|
| 120 |
+
|
| 121 |
+
# Fallback to a simple TTS if gTTS fails
|
| 122 |
+
try:
|
| 123 |
+
st.write("Attempting fallback to pyttsx3...")
|
| 124 |
+
import pyttsx3
|
| 125 |
+
engine = pyttsx3.init()
|
| 126 |
+
|
| 127 |
+
# Create a temporary file for the fallback audio
|
| 128 |
+
temp_wav = tempfile.NamedTemporaryFile(delete=False, suffix='.wav')
|
| 129 |
+
temp_wav_filename = temp_wav.name
|
| 130 |
+
temp_wav.close()
|
| 131 |
+
|
| 132 |
+
# Generate and save speech
|
| 133 |
+
engine.save_to_file(story, temp_wav_filename)
|
| 134 |
+
engine.runAndWait()
|
| 135 |
+
|
| 136 |
+
# Read the audio file
|
| 137 |
+
with open(temp_wav_filename, 'rb') as audio_file:
|
| 138 |
+
fallback_audio = audio_file.read()
|
| 139 |
+
|
| 140 |
+
# Clean up
|
| 141 |
+
os.unlink(temp_wav_filename)
|
| 142 |
+
|
| 143 |
+
st.audio(fallback_audio, format='audio/wav')
|
| 144 |
+
except:
|
| 145 |
+
st.error("Both TTS methods failed. Please install gTTS manually.")
|