Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tempfile
|
2 |
+
import edge_tts
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
language_dict = {
|
6 |
+
"English": {
|
7 |
+
"Jenny": "en-US-JennyNeural",
|
8 |
+
"Guy": "en-US-GuyNeural",
|
9 |
+
"Ana": "en-US-AnaNeural",
|
10 |
+
"Aria": "en-US-AriaNeural"
|
11 |
+
},
|
12 |
+
"Arabic": {
|
13 |
+
"Hamed": "ar-SA-HamedNeural",
|
14 |
+
"Zariyah": "ar-SA-ZariyahNeural",
|
15 |
+
"Fatima": "ar-AE-FatimaNeural",
|
16 |
+
"Hamdan": "ar-AE-HamdanNeural"
|
17 |
+
}
|
18 |
+
}
|
19 |
+
|
20 |
+
async def text_to_speech_edge(text, language_code, speaker):
|
21 |
+
voice = language_dict[language_code][speaker]
|
22 |
+
communicate = edge_tts.Communicate(text, voice)
|
23 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
24 |
+
tmp_path = tmp_file.name
|
25 |
+
await communicate.save(tmp_path)
|
26 |
+
|
27 |
+
return tmp_path
|
28 |
+
|
29 |
+
def get_speakers(language):
|
30 |
+
speakers = list(language_dict[language].keys())[:4] # Limit to 4 speakers
|
31 |
+
return gr.Dropdown(choices=speakers, value=speakers[0], interactive=True)
|
32 |
+
|
33 |
+
default_language = None
|
34 |
+
default_speaker = None
|
35 |
+
css_style = """
|
36 |
+
body {
|
37 |
+
background-image: url('https://cdna.artstation.com/p/assets/images/images/074/776/904/large/pietro-chiovaro-r1-castle-chp.jpg?1712916847');
|
38 |
+
background-size: cover;
|
39 |
+
background-position: center;
|
40 |
+
color: #fff; /* General text color */
|
41 |
+
font-family: 'Arial', sans-serif;
|
42 |
+
}
|
43 |
+
.title {
|
44 |
+
text-align: center;
|
45 |
+
font-size: 3.5em;
|
46 |
+
margin: 20px 0;
|
47 |
+
text-shadow: 2px 2px 5px rgba(0, 0, 0, 0.7);
|
48 |
+
color: #FFD700; /* Gold color for title */
|
49 |
+
}
|
50 |
+
.subtitle {
|
51 |
+
text-align: center;
|
52 |
+
font-size: 2.5em;
|
53 |
+
color: #FFD700;
|
54 |
+
}
|
55 |
+
.output {
|
56 |
+
margin-top: 20px;
|
57 |
+
padding: 15px;
|
58 |
+
border-radius: 8px;
|
59 |
+
background: rgba(0, 0, 0, 0.7); /* Dark background for output area */
|
60 |
+
}
|
61 |
+
/* Styling for the examples */
|
62 |
+
.gr-examples {
|
63 |
+
background-color: transparent !important; /* Transparent background */
|
64 |
+
color: white; /* White text for examples */
|
65 |
+
padding: 10px; /* Optional padding */
|
66 |
+
}
|
67 |
+
.gr-examples button {
|
68 |
+
color: white !important; /* White text for buttons */
|
69 |
+
background-color: transparent !important; /* Transparent background for buttons */
|
70 |
+
border: 1px solid white; /* Add a white border to buttons */
|
71 |
+
}
|
72 |
+
"""
|
73 |
+
|
74 |
+
examples = [ ["Once upon a time in a small village, there lived a kind-hearted girl named Ella. Every day, she would help her neighbors and spread joy wherever she went.", "English", "Jenny"],
|
75 |
+
["ูู ูุฏูู
ุงูุฒู
ุงูุ ูุงู ููุงู ูุชุงุฉ ุทูุจุฉ ุงูููุจ ุชุนูุด ูู ูุฑูุฉ ุตุบูุฑุฉ ุชูุฏุนู ูููู. ูุงูุช ูููู ุชุณุงุนุฏ ุฌูุฑุงููุง ูู ููู
ูุชูุดุฑ ุงููุฑุญ ูู ูู ู
ูุงู ุชุฐูุจ ุฅููู.", "Arabic", "Hamed"] ]
|
76 |
+
|
77 |
+
with gr.Blocks(css = css_style) as demo:
|
78 |
+
|
79 |
+
gr.HTML("<div class='title'> Storytelling</div>")
|
80 |
+
gr.HTML("<div class='subtitle'>Bring your stories to life with captivating voices!</div>")
|
81 |
+
|
82 |
+
with gr.Row():
|
83 |
+
with gr.Column():
|
84 |
+
input_text = gr.Textbox(lines=5, label="Input Text", placeholder="Enter the story you want to tell...")
|
85 |
+
language = gr.Dropdown(
|
86 |
+
choices=list(language_dict.keys()), value=default_language, label="Select Language", interactive=True
|
87 |
+
)
|
88 |
+
speaker = gr.Dropdown(choices=[], value=default_speaker, label="Choose a Speaker", interactive=False)
|
89 |
+
run_btn = gr.Button(value="Generate Magical Audio", variant="primary")
|
90 |
+
|
91 |
+
with gr.Column():
|
92 |
+
output_audio = gr.Audio(type="filepath", label="Audio Output", elem_id="output_audio", elem_classes="output")
|
93 |
+
|
94 |
+
with gr.Row():
|
95 |
+
with gr.Column():
|
96 |
+
gr.Markdown("### Example Prompts")
|
97 |
+
gr.Examples(examples,
|
98 |
+
inputs=[input_text,language, speaker],
|
99 |
+
cache_examples=False)
|
100 |
+
language.change(get_speakers, inputs=[language], outputs=[speaker])
|
101 |
+
run_btn.click(text_to_speech_edge, inputs=[input_text, language, speaker], outputs=[output_audio])
|
102 |
+
|
103 |
+
if __name__ == "__main__":
|
104 |
+
demo.queue().launch(share=False)
|