hivecorp commited on
Commit
fc4c46a
·
verified ·
1 Parent(s): 5efe19e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -27
app.py CHANGED
@@ -2,28 +2,23 @@ import gradio as gr
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
- file if playing_states[file] else None,
22
- "⏹️" if playing_states[file] else "▶️", # Change button icon
23
- "play-btn playing" if playing_states[file] else "play-btn",
24
- )
25
 
26
- # Custom CSS for styling
27
  custom_css = """
28
  h2 {
29
  text-align: center;
@@ -111,6 +106,15 @@ body {
111
  }
112
  """
113
 
 
 
 
 
 
 
 
 
 
114
  # Create Gradio UI
115
  with gr.Blocks(css=custom_css) as demo:
116
  gr.Markdown("<h2>🎤 Human-Like Voices (Pro Plan)</h2>")
@@ -118,15 +122,14 @@ with gr.Blocks(css=custom_css) as demo:
118
  with gr.Column(elem_classes="voice-container"):
119
  for voice in voices:
120
  with gr.Column(elem_classes="voice-box"):
121
- # Audio Component (Hidden, Dynamic Source)
122
- audio = gr.Audio(visible=False)
123
- # Play Button
124
- btn = gr.Button("▶️", elem_classes="play-btn")
125
- # Voice Details
126
  info = gr.Markdown(f"<div class='voice-info'><b>{voice['name']}</b><br><span>{voice['gender']} | {voice['country']}</span></div>")
127
 
128
- # Play/Pause Click Event
129
- btn.click(fn=toggle_audio, inputs=[gr.State(voice["file"])], outputs=[audio, btn, btn])
130
 
131
  # Launch App
132
- demo.launch()
 
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
  }
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
  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()