AP123 commited on
Commit
7ff6d45
1 Parent(s): ae677e4

Updating functions to intake voice as a paremeter

Browse files
Files changed (1) hide show
  1. app.py +9 -8
app.py CHANGED
@@ -8,13 +8,13 @@ os.environ['OPENAI_API_KEY'] = os.environ.get('OPENAI_API_KEY')
8
 
9
  client = OpenAI() # add api_key
10
 
11
- def tts(text):
 
12
  response = client.audio.speech.create(
13
- model="tts-1",
14
- voice="alloy",
15
  input=text,
16
  )
17
-
18
  # Create a temp file to save the audio
19
  with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
20
  temp_file.write(response.content)
@@ -28,13 +28,14 @@ def tts(text):
28
  with gr.Blocks() as demo:
29
  gr.Markdown("# <center> OpenAI Text-To-Speech API with Gradio </center>")
30
  with gr.Row():
31
- dd1 = gr.Dropdown(choices=['tts-1','tts-1-hd'], label='Model')
32
- dd2 = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Voice Options')
33
 
34
  text = gr.Textbox(label="Input text")
35
  btn = gr.Button("Text-To-Speech")
36
  output_audio = gr.Audio(label="Speech Output")
37
 
38
- btn.click(fn=tts, inputs=text, outputs=output_audio, api_name="tts")
 
39
 
40
- demo.launch()
 
8
 
9
  client = OpenAI() # add api_key
10
 
11
+ # Updated tts function now takes model and voice as parameters
12
+ def tts(text, model, voice): # <-- Change here: added model and voice as parameters
13
  response = client.audio.speech.create(
14
+ model=model, # <-- Change here: using the model parameter
15
+ voice=voice, # <-- Change here: using the voice parameter
16
  input=text,
17
  )
 
18
  # Create a temp file to save the audio
19
  with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
20
  temp_file.write(response.content)
 
28
  with gr.Blocks() as demo:
29
  gr.Markdown("# <center> OpenAI Text-To-Speech API with Gradio </center>")
30
  with gr.Row():
31
+ dd1 = gr.Dropdown(choices=['tts-1', 'tts-1-hd'], label='Model')
32
+ dd2 = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Voice Options')
33
 
34
  text = gr.Textbox(label="Input text")
35
  btn = gr.Button("Text-To-Speech")
36
  output_audio = gr.Audio(label="Speech Output")
37
 
38
+ # Updated btn.click to pass model and voice parameters to the tts function
39
+ btn.click(fn=tts, inputs=[text, dd1, dd2], outputs=output_audio, api_name="tts") # <-- Change here: added dd1 and dd2 to inputs
40
 
41
+ demo.launch()