hivecorp commited on
Commit
7eef922
·
verified ·
1 Parent(s): 03cc29a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -28
app.py CHANGED
@@ -2,23 +2,27 @@ import gradio as gr
2
 
3
  # Define available voices
4
  voices = [
5
- {"name": "Jessica", "gender": "Female", "country": "US", "file": "jessica.mp3"},
6
- {"name": "Michael", "gender": "Male", "country": "UK", "file": "michael.mp3"},
7
- {"name": "Sophia", "gender": "Female", "country": "Canada", "file": "sophia.mp3"},
8
- {"name": "David", "gender": "Male", "country": "Australia", "file": "david.mp3"},
9
- {"name": "Emma", "gender": "Female", "country": "Germany", "file": "emma.mp3"},
10
- {"name": "Lucas", "gender": "Male", "country": "France", "file": "lucas.mp3"},
11
- {"name": "Liam", "gender": "Male", "country": "India", "file": "liam.mp3"},
12
- {"name": "Olivia", "gender": "Female", "country": "Brazil", "file": "olivia.mp3"},
13
- {"name": "Noah", "gender": "Male", "country": "Japan", "file": "noah.mp3"},
14
- {"name": "Ava", "gender": "Female", "country": "South Korea", "file": "ava.mp3"},
15
  ]
16
 
17
- # Function to return audio file path
18
- def play_audio(file):
19
- return f"voices/{file}"
 
 
 
 
 
 
 
 
 
 
20
 
21
- # Custom CSS for better design and responsiveness
22
  custom_css = """
23
  h2 {
24
  text-align: center;
@@ -106,15 +110,6 @@ body {
106
  }
107
  """
108
 
109
- # State to track playing voices
110
- playing_states = {voice["file"]: False for voice in voices}
111
-
112
- # Function to toggle play/pause
113
- def toggle_audio(file):
114
- global playing_states
115
- playing_states[file] = not playing_states[file]
116
- return (f"voices/{file}" if playing_states[file] else None), gr.update(elem_classes="play-btn playing" if playing_states[file] else "play-btn")
117
-
118
  # Create Gradio UI
119
  with gr.Blocks(css=custom_css) as demo:
120
  gr.Markdown("<h2>🎤 Human-Like Voices (Pro Plan)</h2>")
@@ -122,14 +117,15 @@ with gr.Blocks(css=custom_css) as demo:
122
  with gr.Column(elem_classes="voice-container"):
123
  for voice in voices:
124
  with gr.Column(elem_classes="voice-box"):
125
- # Hidden Audio Component
126
- audio = gr.Audio(None, autoplay=False, elem_id=voice["name"], visible=False)
127
- # Round Play Button
128
  btn = gr.Button("▶", elem_classes="play-btn")
129
- # Voice Details (Name in Bold, Other Info Below)
130
  info = gr.Markdown(f"<div class='voice-info'><b>{voice['name']}</b><br><span>{voice['gender']} | {voice['country']}</span></div>")
131
 
132
- btn.click(fn=toggle_audio, inputs=[], outputs=[audio, btn])
 
133
 
134
  # Launch App
135
  demo.launch()
 
2
 
3
  # Define available voices
4
  voices = [
5
+ {"name": "Jessica", "gender": "Female", "country": "US", "file": "voices/jessica.mp3"},
6
+ {"name": "Michael", "gender": "Male", "country": "UK", "file": "voices/michael.mp3"},
7
+ {"name": "Sophia", "gender": "Female", "country": "Canada", "file": "voices/sophia.mp3"},
8
+ {"name": "David", "gender": "Male", "country": "Australia", "file": "voices/david.mp3"},
 
 
 
 
 
 
9
  ]
10
 
11
+ # Store playing state
12
+ playing_states = {voice["file"]: False for voice in voices}
13
+
14
+ # Function to toggle audio
15
+ def toggle_audio(file):
16
+ global playing_states
17
+ playing_states[file] = not playing_states[file] # Toggle state
18
+
19
+ # If playing, return file path; otherwise, return None to stop audio
20
+ return (
21
+ gr.update(value=file if playing_states[file] else None),
22
+ gr.update(elem_classes="play-btn playing" if playing_states[file] else "play-btn"),
23
+ )
24
 
25
+ # Custom CSS for styling
26
  custom_css = """
27
  h2 {
28
  text-align: center;
 
110
  }
111
  """
112
 
 
 
 
 
 
 
 
 
 
113
  # Create Gradio UI
114
  with gr.Blocks(css=custom_css) as demo:
115
  gr.Markdown("<h2>🎤 Human-Like Voices (Pro Plan)</h2>")
 
117
  with gr.Column(elem_classes="voice-container"):
118
  for voice in voices:
119
  with gr.Column(elem_classes="voice-box"):
120
+ # Audio Component (Hidden, Dynamic Source)
121
+ audio = gr.Audio(None, autoplay=True, elem_id=voice["name"], visible=False)
122
+ # Play Button
123
  btn = gr.Button("▶", elem_classes="play-btn")
124
+ # Voice Details
125
  info = gr.Markdown(f"<div class='voice-info'><b>{voice['name']}</b><br><span>{voice['gender']} | {voice['country']}</span></div>")
126
 
127
+ # Play/Pause Click Event
128
+ btn.click(fn=toggle_audio, inputs=[], outputs=[audio, btn], _js="() => {}")
129
 
130
  # Launch App
131
  demo.launch()