markllego commited on
Commit
3ff256e
1 Parent(s): ac4c46c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as grd
2
+ import os
3
+ import tempfile
4
+ from openai import OpenAI
5
+
6
+ # Initialize OpenAI client with API key
7
+ api_key = os.getenv('OPENAI_API_KEY')
8
+ os.environ['OPENAI_API_KEY'] = api_key
9
+
10
+ openai_client = OpenAI()
11
+
12
+ def synthesize_speech(input_text, selected_model, selected_voice):
13
+ audio_response = openai_client.audio.speech.create(
14
+ model=selected_model,
15
+ voice=selected_voice,
16
+ input=input_text,
17
+ )
18
+
19
+ # Save the synthesized speech to a temporary audio file
20
+ with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as audio_temp:
21
+ audio_temp.write(audio_response.content)
22
+ audio_file_path = audio_temp.name
23
+
24
+ return audio_file_path
25
+
26
+ # Define the Gradio interface
27
+ with grd.Blocks() as speech_synthesizer_interface:
28
+ grd.Markdown("# <center> Text-to-Speech Synthesizer </center>")
29
+ with grd.Row():
30
+ model_selector = grd.Dropdown(choices=['tts-1', 'tts-1-hd'], label='Choose Model', value='tts-1')
31
+ voice_selector = grd.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Select Voice', value='alloy')
32
+
33
+ input_field = grd.Textbox(label="Enter your text here", placeholder="Type here and convert to speech.")
34
+ synthesis_button = grd.Button("Convert to Speech")
35
+ audio_result = grd.Audio(label="Generated Speech")
36
+
37
+ input_field.submit(fn=synthesize_speech, inputs=[input_field, model_selector, voice_selector], outputs=audio_result)
38
+ synthesis_button.click(fn=synthesize_speech, inputs=[input_field, model_selector, voice_selector], outputs=audio_result)
39
+
40
+ # Launch the interface
41
+ speech_synthesizer_interface.launch()