Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,163 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import
|
5 |
-
from PIL import Image
|
6 |
-
import numpy as np
|
7 |
-
|
8 |
-
# Ensure output directory exists
|
9 |
-
output_dir = "output"
|
10 |
-
if not os.path.exists(output_dir):
|
11 |
-
os.makedirs(output_dir)
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
# Login function for authentication
|
14 |
def custom_auth(username, password):
|
15 |
return password == "aitutor"
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
from elevenlabs import voices, generate, set_api_key, UnauthenticatedRateLimitError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
def pad_buffer(audio):
|
7 |
+
# Pad buffer to multiple of 2 bytes
|
8 |
+
buffer_size = len(audio)
|
9 |
+
element_size = np.dtype(np.int16).itemsize
|
10 |
+
if buffer_size % element_size != 0:
|
11 |
+
audio = audio + b'\0' * (element_size - (buffer_size % element_size))
|
12 |
+
return audio
|
13 |
+
|
14 |
+
def generate_voice(text, voice_name):
|
15 |
+
try:
|
16 |
+
audio = generate(
|
17 |
+
text[:250], # Limit to 250 characters
|
18 |
+
voice=voice_name,
|
19 |
+
model="eleven_multilingual_v2"
|
20 |
+
)
|
21 |
+
return (44100, np.frombuffer(pad_buffer(audio), dtype=np.int16))
|
22 |
+
except UnauthenticatedRateLimitError as e:
|
23 |
+
raise gr.Error("Thanks for trying out ElevenLabs TTS! You've reached the free tier limit. Please provide an API key to continue.")
|
24 |
+
except Exception as e:
|
25 |
+
raise gr.Error(e)
|
26 |
+
|
27 |
# Login function for authentication
|
28 |
def custom_auth(username, password):
|
29 |
return password == "aitutor"
|
30 |
|
31 |
+
|
32 |
+
badges = """
|
33 |
+
"""
|
34 |
+
menu = """
|
35 |
+
<style>
|
36 |
+
/* Existing styles */
|
37 |
+
.gradio-container-3-40-1 .prose a {
|
38 |
+
color: #ffffff!important;
|
39 |
+
text-decoration: auto!important;
|
40 |
+
}
|
41 |
+
.menu-bar {
|
42 |
+
display: flex;
|
43 |
+
justify-content: space-between;
|
44 |
+
align-items: center;
|
45 |
+
background-color: #333;
|
46 |
+
padding: 10px;
|
47 |
+
color: white;
|
48 |
+
font-family: 'sans-serif';
|
49 |
+
}
|
50 |
+
.menu-bar a, .menu-bar a:visited {
|
51 |
+
color: white;
|
52 |
+
text-decoration: none;
|
53 |
+
}
|
54 |
+
.menu-icon {
|
55 |
+
font-size: 24px;
|
56 |
+
background-color: #ffffff;
|
57 |
+
border-radius: 50%;
|
58 |
+
padding: 5px;
|
59 |
+
}
|
60 |
+
.menu-items {
|
61 |
+
display: flex;
|
62 |
+
gap: 15px;
|
63 |
+
color: white;
|
64 |
+
}
|
65 |
+
.menu-item {
|
66 |
+
padding: 8px 16px;
|
67 |
+
background-color: #555;
|
68 |
+
border-radius: 4px;
|
69 |
+
transition: background-color 0.3s;
|
70 |
+
font-weight: bold;
|
71 |
+
font-size: 12px;
|
72 |
+
}
|
73 |
+
.menu-item:hover {
|
74 |
+
background-color: #777;
|
75 |
+
}
|
76 |
+
/* Responsive styles for mobile */
|
77 |
+
@media (max-width: 768px) {
|
78 |
+
.menu-item {
|
79 |
+
font-size: 12px; /* Shrink text size */
|
80 |
+
}
|
81 |
+
.menu-icon {
|
82 |
+
font-size: 18px; /* Shrink icon size */
|
83 |
+
}
|
84 |
+
}
|
85 |
+
</style>
|
86 |
+
<div class="menu-bar">
|
87 |
+
<a href="#" class="menu-icon">🎵</a>
|
88 |
+
<div class="menu-items">
|
89 |
+
<span class="menu-item"><a href="#">Dashboard</a></span>
|
90 |
+
<span class="menu-item"><a href="#">Premium</a></span>
|
91 |
+
<span class="menu-item"><a href="#">Account</a></span>
|
92 |
+
<span class="menu-item"><a href="#">Voices</a></span>
|
93 |
+
</div>
|
94 |
+
</div>
|
95 |
+
"""
|
96 |
+
|
97 |
+
description = """
|
98 |
+
<style>
|
99 |
+
.notification {
|
100 |
+
text-align: left; /* Left-align the text */
|
101 |
+
background-color: #ffffff; /* White Background */
|
102 |
+
color: #000; /* Black text */
|
103 |
+
padding: 20px; /* Padding */
|
104 |
+
margin: 20px; /* Margin */
|
105 |
+
margin-bottom: 40px; /* Additional space below the notification */
|
106 |
+
border: 2px solid #000000; /* Black border */
|
107 |
+
border-radius: 20px; /* Rounded corners */
|
108 |
+
font-size: 18px; /* Font size */
|
109 |
+
font-family: 'Arial', sans-serif; /* Font family */
|
110 |
+
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.1); /* Subtle box shadow for depth */
|
111 |
+
}
|
112 |
+
</style>
|
113 |
+
<div class="notification">
|
114 |
+
Welcome to Text to Voice, single foundational model supporting 28 languages including: English, Chinese, Spanish, Hindi, Portuguese, French, German, Japanese, Arabic, Korean, Indonesian, Italian, Dutch, Turkish, Polish, Swedish, Filipino, Malay, Romanian, Ukrainian, Greek, Czech, Danish, Finnish, Bulgarian, Croatian, Slovak, and Tamil.
|
115 |
+
</div>
|
116 |
+
"""
|
117 |
+
|
118 |
+
|
119 |
+
|
120 |
+
with gr.Blocks() as block:
|
121 |
+
gr.Markdown('')
|
122 |
+
gr.Markdown(menu)
|
123 |
+
gr.Markdown(badges)
|
124 |
+
gr.Markdown(description)
|
125 |
+
|
126 |
+
input_text = gr.Textbox(
|
127 |
+
label="Input Text (250 characters max)",
|
128 |
+
lines=2,
|
129 |
+
value="Hello! 你好! Hola! नमस्ते! Bonjour! こんにちは! مرحبا! 안녕하세요! Ciao! Cześć! Привіт! Γειά σας! Здравей! வணக்கம்!",
|
130 |
+
elem_id="input_text"
|
131 |
+
)
|
132 |
+
|
133 |
+
all_voices = voices()
|
134 |
+
input_voice = gr.Dropdown(
|
135 |
+
[voice.name for voice in all_voices],
|
136 |
+
value="Bella",
|
137 |
+
label="Voice",
|
138 |
+
elem_id="input_voice"
|
139 |
+
)
|
140 |
+
|
141 |
+
run_button = gr.Button(
|
142 |
+
text="Generate Voice",
|
143 |
+
type="button"
|
144 |
+
)
|
145 |
+
|
146 |
+
out_audio = gr.Audio(
|
147 |
+
label="Generated Voice",
|
148 |
+
type="numpy",
|
149 |
+
elem_id="out_audio",
|
150 |
+
format="mp3"
|
151 |
+
)
|
152 |
+
|
153 |
+
inputs = [input_text, input_voice]
|
154 |
+
outputs = [out_audio]
|
155 |
+
|
156 |
+
run_button.click(
|
157 |
+
fn=generate_voice,
|
158 |
+
inputs=inputs,
|
159 |
+
outputs=outputs,
|
160 |
+
queue=True
|
161 |
+
)
|
162 |
+
|
163 |
+
block.queue(concurrency_count=5).launch(debug=True, auth=custom_auth)
|