from gtts import gTTS
import gradio as gr
import os

# Function to convert text to speech and save as an audio file
def text_to_speech(text, language="hr"):
    # Create a gTTS object
    tts = gTTS(text=text, lang=language, slow=False)
    
    # Save the audio file
    audio_file = "output.mp3"
    tts.save(audio_file)
    
    return audio_file

# Gradio interface
def tts_demo(text, language):
    # Convert text to speech
    audio_file = text_to_speech(text, language)
    
    # Return the audio file to be played in the Gradio interface
    return audio_file

# Define Gradio inputs and outputs
inputs = [
    gr.Textbox(label="Enter Text", placeholder="Type something here..."),
    gr.Dropdown(label="Language", choices=["en", "es", "fr", "de", "hr"], value="hr")
]
outputs = gr.Audio(label="Generated Audio", type="filepath")

# Create the Gradio interface
iface = gr.Interface(
    fn=tts_demo,
    inputs=inputs,
    outputs=outputs,
    title="Text-to-Speech Demo",
    description="Enter text and select a language to generate and play audio."
)

# Launch the app
iface.launch()