antoniomae1234 commited on
Commit
d3ddd31
·
verified ·
1 Parent(s): 542af15

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -0
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import edge_tts
3
+ import asyncio
4
+ import os
5
+ # https://speech.platform.bing.com/consumer/speech/synthesize/readaloud/voices/list?trustedclienttoken=6A5AA1D4EAFF4E9FB37E23D68491D6F4
6
+ SUPPORTED_VOICES = {
7
+ 'Vivienne': 'fr-FR-VivienneMultilingualNeural',
8
+ 'Denise': 'fr-FR-DeniseNeural',
9
+ 'Eloise': 'fr-FR-EloiseNeural',
10
+ 'Remy': 'fr-FR-RemyMultilingualNeural',
11
+ 'Henri': 'fr-FR-HenriNeural'
12
+ 'Henri': 'fr-FR-JosephineNeural'
13
+ }
14
+
15
+ # Modification de voix
16
+ def changeVoice(voices):
17
+ example = SUPPORTED_VOICES[voices]
18
+ example_file = os.path.join(os.path.dirname(__file__), "example/"+example+".mp3")
19
+ return example_file
20
+
21
+ # Text to Speech
22
+ async def textToSpeech(text, voices, rate, volume):
23
+ output_file = "output.mp3"
24
+ voices = SUPPORTED_VOICES[voices]
25
+ if (rate >= 0):
26
+ rates = rate = "+" + str(rate) + "%"
27
+ else:
28
+ rates = str(rate) + "%"
29
+ if (volume >= 0):
30
+ volumes = "+" + str(volume) + "%"
31
+ else:
32
+ volumes = str(volume) + "%"
33
+ communicate = edge_tts.Communicate(text,
34
+ voices,
35
+ rate=rates,
36
+ volume=volumes,
37
+ proxy=None)
38
+ await communicate.save(output_file)
39
+ audio_file = os.path.join(os.path.dirname(__file__), "output.mp3")
40
+ if (os.path.exists(audio_file)):
41
+ return audio_file
42
+ else:
43
+ raise gr.Error("La création a échoué!")
44
+ return FileNotFoundError
45
+
46
+
47
+ # Effacer le texte et rendu
48
+ def clearSpeech():
49
+ output_file = os.path.join(os.path.dirname(__file__), "output.mp3")
50
+ if (os.path.exists(output_file)):
51
+ os.remove(output_file)
52
+ return None, None
53
+
54
+
55
+ with gr.Blocks(css="style.css", title="Text To Speech") as demo:
56
+ gr.Markdown("""
57
+ # Text-to-Speech FR
58
+ Conversion texte en audio vocal avec edge-tts
59
+ """)
60
+ with gr.Row():
61
+ with gr.Column():
62
+ text = gr.TextArea(label="Entrez votre texte", elem_classes="text-area")
63
+ btn = gr.Button("Convertir en audio vocal", elem_id="submit-btn")
64
+ with gr.Column():
65
+ voices = gr.Dropdown(choices=[
66
+ "Vivienne", "Denise", "Eloise", "Remy",
67
+ "Henri"
68
+ ],
69
+ value="Vivienne",
70
+ label="Modèle de voix",
71
+ info="Choix du modèle vocal FR (France)",
72
+ interactive=True)
73
+
74
+ example = gr.Audio(label="Exemple",
75
+ value="example/fr-FR-VivienneMultilingualNeural.mp3",
76
+ interactive=False,
77
+ autoplay=False,
78
+ elem_classes="example")
79
+
80
+ voices.change(fn=changeVoice,inputs=voices,outputs=example)
81
+ rate = gr.Slider(-100,
82
+ 100,
83
+ step=1,
84
+ value=0,
85
+ label="Vitesse de parole",
86
+ info="Accélérer ou ralentir la voix",
87
+ interactive=True)
88
+
89
+ volume = gr.Slider(-100,
90
+ 100,
91
+ step=1,
92
+ value=0,
93
+ label="Volume",
94
+ info="Augmenter ou baisser le volume audio",
95
+ interactive=True)
96
+ audio = gr.Audio(label="Rendu audio",
97
+ interactive=False,
98
+ elem_classes="audio")
99
+ clear = gr.Button("Effacer", elem_id="clear-btn")
100
+ btn.click(fn=textToSpeech,
101
+ inputs=[text, voices, rate, volume],
102
+ outputs=[audio])
103
+ clear.click(fn=clearSpeech, outputs=[text, audio])
104
+
105
+ if __name__ == "__main__":
106
+ demo.launch()
107
+