hackaton2 / app.py
akazmi's picture
Update app.py
0ed986b verified
import streamlit as st
from gtts import gTTS
import pyttsx3
import os
# Function to use Google Text-to-Speech (gTTS)
def gtts_text_to_speech(text):
tts = gTTS(text=text, lang='en', slow=False)
tts.save("output.mp3")
return "output.mp3"
# Function to use pyttsx3 (Offline TTS)
def pyttsx3_text_to_speech(text):
engine = pyttsx3.init()
engine.save_to_file(text, "output.mp3")
engine.runAndWait()
return "output.mp3"
# Streamlit UI
st.title("Text-to-Speech Converter")
# Input text
user_input = st.text_area("Enter text:", "Hello, how can I assist you today?")
# Select TTS option
tts_option = st.radio("Choose Text-to-Speech Method", ("Google TTS", "Offline TTS"))
# Button to convert to speech
if st.button("Convert to Speech"):
if tts_option == "Google TTS":
output_file = gtts_text_to_speech(user_input)
else:
output_file = pyttsx3_text_to_speech(user_input)
st.audio(output_file, format="audio/mp3")
st.success("Speech generated successfully!")