import gradio as gr import gtts import io def text_to_speech(text): if text: try: # Use gTTS to convert text to speech and save it as an MP3 file speech = gtts.gTTS(text) mp3_data = io.BytesIO() speech.save(mp3_data) mp3_data.seek(0) return mp3_data.getvalue() except Exception as e: return str(e) else: return None iface = gr.Interface( fn=text_to_speech, inputs=gr.inputs.Text(default="Enter text here...", label="Input Text"), # Use gr.inputs.Text outputs=gr.outputs.Audio(label="Voice"), # Use the 'numpy' type by default title="Text to Speech", description="Enter text and click 'Generate' to convert it to speech.", theme="blue", live=False, ) iface.launch()