Update app.py
Browse files
app.py
CHANGED
|
@@ -1,136 +1,135 @@
|
|
| 1 |
-
import
|
| 2 |
-
import os
|
| 3 |
|
| 4 |
# Define available voices
|
| 5 |
voices = [
|
| 6 |
-
{"name": "Jessica", "gender": "Female", "country": "US", "file": "
|
| 7 |
-
{"name": "Michael", "gender": "Male", "country": "UK", "file": "
|
| 8 |
-
{"name": "Sophia", "gender": "Female", "country": "Canada", "file": "
|
| 9 |
-
{"name": "David", "gender": "Male", "country": "Australia", "file": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
]
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
# Custom CSS for better
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
.
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 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 |
-
#
|
| 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 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 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 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
-
|
|
|
|
|
|
| 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()
|