hivecorp commited on
Commit
24ec678
·
verified ·
1 Parent(s): 3a10ce9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -125
app.py CHANGED
@@ -1,136 +1,135 @@
1
- import streamlit as st
2
- import os
3
 
4
  # Define available voices
5
  voices = [
6
- {"name": "Jessica", "gender": "Female", "country": "US", "file": "voices/jessica.mp3"},
7
- {"name": "Michael", "gender": "Male", "country": "UK", "file": "voices/michael.mp3"},
8
- {"name": "Sophia", "gender": "Female", "country": "Canada", "file": "voices/sophia.mp3"},
9
- {"name": "David", "gender": "Male", "country": "Australia", "file": "voices/david.mp3"},
 
 
 
 
 
 
10
  ]
11
 
12
- # Streamlit UI Setup
13
- st.set_page_config(page_title="Human-Like Voice Player", layout="wide")
 
14
 
15
- # Custom CSS for better styling
16
- st.markdown("""
17
- <style>
18
- body {
19
- background-color: #121212;
20
- font-family: Arial, sans-serif;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  }
22
- h2 {
23
- text-align: center;
24
- color: #ffffff;
25
- font-size: 28px;
26
- margin-bottom: 20px;
27
- }
28
- .container {
29
- display: flex;
30
- flex-wrap: wrap;
31
- justify-content: center;
32
- gap: 20px;
33
- padding: 20px;
34
- }
35
- .voice-box {
36
- background: #1e1e1e;
37
- border-radius: 12px;
38
- padding: 20px;
39
- text-align: center;
40
- box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.3);
41
- width: 220px;
42
- height: 220px;
43
- transition: transform 0.2s ease-in-out;
44
- position: relative;
45
- }
46
- .voice-box:hover {
47
- transform: scale(1.05);
48
- }
49
- .play-btn {
50
- background-color: #FF5722;
51
- color: white;
52
- border-radius: 50%;
53
- width: 60px;
54
- height: 60px;
55
- font-size: 26px;
56
- border: none;
57
- cursor: pointer;
58
- margin-bottom: 10px;
59
- box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.2);
60
- transition: background 0.3s ease-in-out;
61
- }
62
- .playing {
63
- background-color: #4CAF50 !important;
64
- }
65
- .playing::after {
66
- content: "";
67
- position: absolute;
68
- top: 50%;
69
- left: 50%;
70
- width: 70px;
71
- height: 70px;
72
- border-radius: 50%;
73
- border: 4px solid #2196F3;
74
- animation: pulse 1s infinite alternate;
75
- transform: translate(-50%, -50%);
76
- }
77
- @keyframes pulse {
78
- 0% { transform: scale(1); opacity: 0.7; }
79
- 100% { transform: scale(1.2); opacity: 1; }
80
- }
81
- .voice-info {
82
- color: #ffffff;
83
- text-align: center;
84
- }
85
- .voice-info b {
86
- font-weight: bold;
87
- font-size: 16px;
88
- }
89
- .voice-info span {
90
- font-size: 14px;
91
- color: #bbbbbb;
92
- }
93
- </style>
94
- """, unsafe_allow_html=True)
95
-
96
- # Display heading
97
- st.markdown("<h2>🎤 Human-Like Voices (Pro Plan)</h2>", unsafe_allow_html=True)
98
-
99
- # Layout container
100
- st.markdown('<div class="container">', unsafe_allow_html=True)
101
-
102
- # State for currently playing audio
103
- if "playing" not in st.session_state:
104
- st.session_state.playing = None # No file is playing initially
105
 
106
- # Display voice sections
107
- for voice in voices:
108
- with st.container():
109
- st.markdown(f'<div class="voice-box">', unsafe_allow_html=True)
110
-
111
- # Button for Play/Pause
112
- is_playing = st.session_state.playing == voice["file"]
113
- btn_label = "⏹️" if is_playing else "▶️"
114
- btn_class = "play-btn playing" if is_playing else "play-btn"
115
 
116
- if st.button(btn_label, key=voice["file"]):
117
- # Toggle Play/Pause
118
- if st.session_state.playing == voice["file"]:
119
- st.session_state.playing = None # Stop audio
120
- else:
121
- st.session_state.playing = voice["file"] # Play new audio
122
-
123
- # Show audio player if playing
124
- if is_playing:
125
- audio_path = os.path.abspath(voice["file"])
126
- st.audio(audio_path, format="audio/mp3", start_time=0)
127
 
128
- # Voice info
129
- st.markdown(
130
- f"<div class='voice-info'><b>{voice['name']}</b><br><span>{voice['gender']} | {voice['country']}</span></div>",
131
- unsafe_allow_html=True
132
- )
133
-
134
- st.markdown("</div>", unsafe_allow_html=True)
 
 
 
 
 
 
 
 
135
 
136
- st.markdown("</div>", unsafe_allow_html=True)
 
 
1
+ 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;
25
+ color: #ffffff;
26
+ font-size: 26px;
27
+ margin-bottom: 20px;
28
+ }
29
+ body {
30
+ background-color: #121212;
31
+ font-family: Arial, sans-serif;
32
+ }
33
+ .voice-container {
34
+ display: grid;
35
+ grid-template-columns: repeat(2, 1fr); /* Default: 2 per row */
36
+ gap: 15px;
37
+ padding: 20px;
38
+ }
39
+ @media (min-width: 1024px) {
40
+ .voice-container {
41
+ grid-template-columns: repeat(3, 1fr); /* 3 per row on PC */
42
  }
43
+ }
44
+ .voice-box {
45
+ background: #1e1e1e;
46
+ border-radius: 12px;
47
+ padding: 15px;
48
+ text-align: center;
49
+ box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.3);
50
+ display: flex;
51
+ flex-direction: column;
52
+ justify-content: center;
53
+ align-items: center;
54
+ width: 100%;
55
+ max-width: 220px;
56
+ height: 220px;
57
+ margin: auto;
58
+ transition: transform 0.2s ease-in-out;
59
+ }
60
+ .voice-box:hover {
61
+ transform: scale(1.05);
62
+ }
63
+ .play-btn {
64
+ background-color: #FF5722 !important;
65
+ color: white;
66
+ border-radius: 50%;
67
+ width: 60px;
68
+ height: 60px;
69
+ font-size: 26px;
70
+ border: none;
71
+ cursor: pointer;
72
+ margin-bottom: 10px;
73
+ box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.2);
74
+ transition: background 0.3s ease-in-out;
75
+ position: relative;
76
+ }
77
+ .play-btn.playing {
78
+ background-color: #4CAF50 !important; /* Green when playing */
79
+ }
80
+ .play-btn.playing::after {
81
+ content: "";
82
+ position: absolute;
83
+ top: -5px;
84
+ left: -5px;
85
+ width: 70px;
86
+ height: 70px;
87
+ border-radius: 50%;
88
+ border: 4px solid #2196F3; /* Blue ring */
89
+ animation: pulse 1s infinite alternate;
90
+ }
91
+ @keyframes pulse {
92
+ 0% { transform: scale(1); opacity: 0.7; }
93
+ 100% { transform: scale(1.2); opacity: 1; }
94
+ }
95
+ .voice-info {
96
+ color: #ffffff;
97
+ text-align: center;
98
+ }
99
+ .voice-info b {
100
+ font-weight: bold;
101
+ font-size: 16px;
102
+ }
103
+ .voice-info span {
104
+ font-size: 14px;
105
+ color: #bbbbbb;
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>")
121
+
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()