jonathanagustin commited on
Commit
6385f1b
1 Parent(s): 1777fa6

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +69 -6
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import tempfile
3
  import openai
4
  import requests
 
5
 
6
 
7
  def tts(input_text: str, model: str, voice: str, api_key: str) -> str:
@@ -59,12 +60,74 @@ def main():
59
  VOICE_OPTIONS = ["alloy", "echo", "fable", "onyx", "nova", "shimmer"]
60
 
61
  # Predefine voice previews URLs
62
- VOICE_PREVIEWS = {
63
  voice: f"https://cdn.openai.com/API/docs/audio/{voice}.wav"
64
  for voice in VOICE_OPTIONS
65
  }
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  with gr.Row():
69
  with gr.Column(scale=1):
70
  api_key_input = gr.Textbox(
@@ -83,12 +146,12 @@ def main():
83
  # Add voice previews using HTML for minimal audio interface
84
  gr.Markdown("### Voice Previews")
85
  for voice in VOICE_OPTIONS:
86
- audio_url = VOICE_PREVIEWS[voice]
87
  # Create an HTML audio player with minimal controls
88
  html_snippet = f'''
89
  <div style="margin-bottom: 10px;">
90
  <p><strong>{voice.capitalize()}</strong></p>
91
- <audio controls style="width: 100%;">
92
  <source src="{audio_url}" type="audio/wav">
93
  Your browser does not support the audio element.
94
  </audio>
@@ -118,9 +181,9 @@ def main():
118
  outputs=output_audio,
119
  )
120
 
121
- # Launch the Gradio app with error display enabled
122
- demo.launch(show_error=True)
123
 
124
 
125
  if __name__ == "__main__":
126
- main()
 
2
  import tempfile
3
  import openai
4
  import requests
5
+ import os
6
 
7
 
8
  def tts(input_text: str, model: str, voice: str, api_key: str) -> str:
 
60
  VOICE_OPTIONS = ["alloy", "echo", "fable", "onyx", "nova", "shimmer"]
61
 
62
  # Predefine voice previews URLs
63
+ VOICE_PREVIEW_URLS = {
64
  voice: f"https://cdn.openai.com/API/docs/audio/{voice}.wav"
65
  for voice in VOICE_OPTIONS
66
  }
67
 
68
+ # Download audio previews to disk before initiating the interface
69
+ PREVIEW_DIR = "voice_previews"
70
+ os.makedirs(PREVIEW_DIR, exist_ok=True)
71
+
72
+ VOICE_PREVIEW_FILES = {}
73
+ for voice, url in VOICE_PREVIEW_URLS.items():
74
+ local_file_path = os.path.join(PREVIEW_DIR, f"{voice}.wav")
75
+ if not os.path.exists(local_file_path):
76
+ try:
77
+ response = requests.get(url)
78
+ response.raise_for_status()
79
+ with open(local_file_path, "wb") as f:
80
+ f.write(response.content)
81
+ except requests.exceptions.RequestException as e:
82
+ print(f"Failed to download {voice} preview: {e}")
83
+ VOICE_PREVIEW_FILES[voice] = local_file_path
84
+
85
+ # Map URLs to local file paths for Gradio to serve
86
+ static_file_mappings = {
87
+ f"/static/voice_previews/{voice}.wav": path
88
+ for voice, path in VOICE_PREVIEW_FILES.items()
89
+ }
90
+
91
  with gr.Blocks() as demo:
92
+ # Include global CSS styles for the audio elements
93
+ gr.HTML("""
94
+ <style>
95
+ /* Hide unnecessary controls in the audio player */
96
+ audio::-webkit-media-controls-enclosure {
97
+ overflow: hidden;
98
+ }
99
+ /* Hide specific controls in WebKit browsers (Chrome, Safari) */
100
+ audio::-webkit-media-controls-current-time-display,
101
+ audio::-webkit-media-controls-time-remaining-display,
102
+ audio::-webkit-media-controls-timeline,
103
+ audio::-webkit-media-controls-volume-slider-container,
104
+ audio::-webkit-media-controls-fullscreen-button,
105
+ audio::-webkit-media-controls-mute-button,
106
+ audio::-webkit-media-controls-volume-slider,
107
+ audio::-webkit-media-controls-timeline-container,
108
+ audio::-webkit-media-controls-seek-back-button,
109
+ audio::-webkit-media-controls-seek-forward-button,
110
+ audio::-webkit-media-controls-rewind-button,
111
+ audio::-webkit-media-controls-return-to-realtime-button,
112
+ audio::-webkit-media-controls-toggle-closed-captions-button {
113
+ display: none;
114
+ }
115
+ /* Hide specific controls in Mozilla Firefox */
116
+ audio:not([controls]) {
117
+ display: none;
118
+ }
119
+ audio::-moz-volume-slider,
120
+ audio::-moz-volume-box {
121
+ display: none;
122
+ }
123
+ /* Set the width of the play button */
124
+ audio {
125
+ width: 50px; /* Adjust as needed */
126
+ height: 30px; /* Adjust as needed */
127
+ }
128
+ </style>
129
+ """)
130
+
131
  with gr.Row():
132
  with gr.Column(scale=1):
133
  api_key_input = gr.Textbox(
 
146
  # Add voice previews using HTML for minimal audio interface
147
  gr.Markdown("### Voice Previews")
148
  for voice in VOICE_OPTIONS:
149
+ audio_url = f"/file/voice_previews/{voice}.wav"
150
  # Create an HTML audio player with minimal controls
151
  html_snippet = f'''
152
  <div style="margin-bottom: 10px;">
153
  <p><strong>{voice.capitalize()}</strong></p>
154
+ <audio controls preload="auto" controlsList="nodownload nofullscreen noremoteplayback" style="width: 50px; height: 30px;">
155
  <source src="{audio_url}" type="audio/wav">
156
  Your browser does not support the audio element.
157
  </audio>
 
181
  outputs=output_audio,
182
  )
183
 
184
+ # Launch the Gradio app with error display enabled and static file mappings
185
+ demo.launch(show_error=True, static_files=static_file_mappings)
186
 
187
 
188
  if __name__ == "__main__":
189
+ main()