deekshitha9876's picture
Update app.py
7c45000 verified
raw
history blame contribute delete
997 Bytes
import os
import openai
import gradio as gr
# read key from env
openai.api_key = os.getenv("OPENAI_API_KEY")
def text_to_speech(text):
try:
# call the TTS endpoint
response = openai.audio.speech.create(
model="tts-1", # or "tts-1-hd"
voice="nova", # nova | shimmer | echo | fable | onyx | alloy
input=text
)
# response.content holds the raw mp3 bytes
out_path = "output.mp3"
with open(out_path, "wb") as f:
f.write(response.content)
return out_path
except Exception as e:
# return the error message so you see it in the UI
return f"Error: {e}"
iface = gr.Interface(
fn=text_to_speech,
inputs=gr.Textbox(lines=2, placeholder="Enter text to convert to speech…"),
outputs=gr.Audio(type="filepath"),
title="Text to Speech with OpenAI",
description="Enter text and get the spoken audio!"
)
if __name__ == "__main__":
iface.launch()