text_to_audio_c / app.py
sikandarciv101's picture
Update app.py
22f8d6c verified
raw
history blame contribute delete
977 Bytes
import gradio as gr
from gtts import gTTS
import os
def text_to_speech(text):
try:
# Convert text to speech
tts = gTTS(text=text, lang='en')
output_path = "output.mp3"
# Save the audio to the output path
tts.save(output_path)
# Check if the file is saved correctly
if os.path.exists(output_path):
print(f"Audio saved to {output_path}")
return output_path # Return the file path directly
else:
print("Failed to save audio.")
return None
except Exception as e:
print(f"Error: {e}")
return None
# Create Gradio interface
iface = gr.Interface(fn=text_to_speech,
inputs="text",
outputs=gr.Audio(type="filepath"), # Make sure 'filepath' is used
title="Text to Speech",
description="Enter text, and the system will read it aloud.")
iface.launch()