jonathanagustin commited on
Commit
fd9f747
1 Parent(s): f660a3d

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +11 -33
app.py CHANGED
@@ -1,11 +1,9 @@
1
  import gradio as gr
2
  import tempfile
3
  import openai
4
- import requests
5
  import os
6
  from functools import partial
7
 
8
-
9
  def tts(
10
  input_text: str,
11
  model: str,
@@ -69,7 +67,6 @@ def tts(
69
 
70
  return temp_file_path
71
 
72
-
73
  def main():
74
  """
75
  Main function to create and launch the Gradio interface.
@@ -78,31 +75,9 @@ def main():
78
  VOICE_OPTIONS = ["alloy", "echo", "fable", "onyx", "nova", "shimmer"]
79
  RESPONSE_FORMAT_OPTIONS = ["mp3", "opus", "aac", "flac", "wav", "pcm"]
80
 
81
- # Predefine voice previews URLs
82
- VOICE_PREVIEW_URLS = {
83
- voice: f"https://cdn.openai.com/API/docs/audio/{voice}.wav"
84
- for voice in VOICE_OPTIONS
85
- }
86
-
87
- # Download audio previews before initiating the interface
88
- PREVIEW_DIR = "voice_previews"
89
- os.makedirs(PREVIEW_DIR, exist_ok=True)
90
-
91
- VOICE_PREVIEW_FILES = {}
92
- for voice, url in VOICE_PREVIEW_URLS.items():
93
- local_file_path = os.path.join(PREVIEW_DIR, f"{voice}.wav")
94
- if not os.path.exists(local_file_path):
95
- try:
96
- response = requests.get(url)
97
- response.raise_for_status()
98
- with open(local_file_path, "wb") as f:
99
- f.write(response.content)
100
- except requests.exceptions.RequestException as e:
101
- print(f"Failed to download {voice} preview: {e}")
102
- VOICE_PREVIEW_FILES[voice] = local_file_path
103
-
104
- # Set static paths for Gradio to serve
105
- gr.set_static_paths(paths=[PREVIEW_DIR])
106
 
107
  with gr.Blocks(title="OpenAI - Text to Speech") as demo:
108
  with gr.Row():
@@ -124,10 +99,12 @@ def main():
124
 
125
  with gr.Group():
126
 
 
 
127
  preview_audio = gr.Audio(
128
  interactive=False,
129
- label="Preview Voice: Echo",
130
- value=VOICE_PREVIEW_FILES["echo"],
131
  visible=True,
132
  show_download_button=True,
133
  show_share_button=False,
@@ -273,7 +250,9 @@ def main():
273
  :return: File path to the generated audio file.
274
  :rtype: str
275
  """
276
- audio_file = tts(input_text, model, voice, api_key, response_format, speed)
 
 
277
  return audio_file
278
 
279
  # Trigger the conversion when the submit button is clicked
@@ -293,6 +272,5 @@ def main():
293
  # Launch the Gradio app with error display enabled
294
  demo.launch(show_error=True)
295
 
296
-
297
  if __name__ == "__main__":
298
- main()
 
1
  import gradio as gr
2
  import tempfile
3
  import openai
 
4
  import os
5
  from functools import partial
6
 
 
7
  def tts(
8
  input_text: str,
9
  model: str,
 
67
 
68
  return temp_file_path
69
 
 
70
  def main():
71
  """
72
  Main function to create and launch the Gradio interface.
 
75
  VOICE_OPTIONS = ["alloy", "echo", "fable", "onyx", "nova", "shimmer"]
76
  RESPONSE_FORMAT_OPTIONS = ["mp3", "opus", "aac", "flac", "wav", "pcm"]
77
 
78
+ # Since you've already downloaded the voice previews to the current directory,
79
+ # set up the VOICE_PREVIEW_FILES dictionary to point to these files directly.
80
+ VOICE_PREVIEW_FILES = {voice: f"{voice}.wav" for voice in VOICE_OPTIONS}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
  with gr.Blocks(title="OpenAI - Text to Speech") as demo:
83
  with gr.Row():
 
99
 
100
  with gr.Group():
101
 
102
+ # Set the default preview audio to one of the voices
103
+ default_voice = "echo"
104
  preview_audio = gr.Audio(
105
  interactive=False,
106
+ label=f"Preview Voice: {default_voice.capitalize()}",
107
+ value=VOICE_PREVIEW_FILES[default_voice],
108
  visible=True,
109
  show_download_button=True,
110
  show_share_button=False,
 
250
  :return: File path to the generated audio file.
251
  :rtype: str
252
  """
253
+ audio_file = tts(
254
+ input_text, model, voice, api_key, response_format, speed
255
+ )
256
  return audio_file
257
 
258
  # Trigger the conversion when the submit button is clicked
 
272
  # Launch the Gradio app with error display enabled
273
  demo.launch(show_error=True)
274
 
 
275
  if __name__ == "__main__":
276
+ main()