app.py
Browse files
app.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from scipy.io import wavfile
|
3 |
+
import torch
|
4 |
+
from parler_tts import ParlerTTSForConditionalGeneration
|
5 |
+
from transformers import AutoTokenizer
|
6 |
+
import gradio as gr
|
7 |
+
import re
|
8 |
+
|
9 |
+
# Text preprocessing tools
|
10 |
+
class EnglishNumberNormalizer:
|
11 |
+
def __call__(self, text):
|
12 |
+
# Placeholder for number normalization logic
|
13 |
+
return text
|
14 |
+
|
15 |
+
number_normalizer = EnglishNumberNormalizer()
|
16 |
+
|
17 |
+
def preprocess(text):
|
18 |
+
text = number_normalizer(text).strip()
|
19 |
+
text = text.replace("-", " ")
|
20 |
+
if text[-1] not in ".!?":
|
21 |
+
text = f"{text}."
|
22 |
+
|
23 |
+
abbreviations_pattern = r'\b[A-Z][A-Z\.]+\b'
|
24 |
+
|
25 |
+
def separate_abb(chunk):
|
26 |
+
chunk = chunk.replace(".", "")
|
27 |
+
return " ".join(chunk)
|
28 |
+
|
29 |
+
abbreviations = re.findall(abbreviations_pattern, text)
|
30 |
+
for abv in abbreviations:
|
31 |
+
if abv in text:
|
32 |
+
text = text.replace(abv, separate_abb(abv))
|
33 |
+
return text
|
34 |
+
|
35 |
+
# Vérification de la disponibilité de CUDA
|
36 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
37 |
+
|
38 |
+
# Chargement du modèle et du tokenizer
|
39 |
+
try:
|
40 |
+
model = ParlerTTSForConditionalGeneration.from_pretrained("CONCREE/Adia_TTS", torch_dtype=torch.float16).to(device)
|
41 |
+
tokenizer = AutoTokenizer.from_pretrained("CONCREE/Adia_TTS")
|
42 |
+
except Exception as e:
|
43 |
+
raise RuntimeError(f"Erreur lors du chargement du modèle : {e}")
|
44 |
+
|
45 |
+
# Texte et description par défaut
|
46 |
+
default_prompt = "Politigu Senegaal, doxalinu demokraasi bu dëgër la, am wote yuy faral di am, te askan wi di ci bokk bu baax"
|
47 |
+
default_description = """A crystal clear and distinct voice, with a moderate reading rate that facilitates understanding. The tone is monotonous, without variations or inflections, which provides a uniform listening experience. The voice is free of background noise and allows for continuous reading, without inappropriate pauses, thus ensuring a constant and pleasant flow."""
|
48 |
+
|
49 |
+
# Fonction pour générer l'audio sans segmentation
|
50 |
+
def generate_audio(prompt, description):
|
51 |
+
try:
|
52 |
+
# Preprocess the input text
|
53 |
+
prompt = preprocess(prompt)
|
54 |
+
|
55 |
+
# Génération des IDs d'entrée
|
56 |
+
input_ids = tokenizer(description, return_tensors="pt").input_ids.to(device)
|
57 |
+
prompt_input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)
|
58 |
+
|
59 |
+
# Générer l'audio
|
60 |
+
generation = model.generate(input_ids=input_ids, prompt_input_ids=prompt_input_ids)
|
61 |
+
audio_arr = generation.cpu().numpy().squeeze() # Transformer en tableau numpy
|
62 |
+
|
63 |
+
# Taux d'échantillonnage
|
64 |
+
sampling_rate = model.config.sampling_rate
|
65 |
+
|
66 |
+
# Normaliser l'audio
|
67 |
+
audio_arr = audio_arr / np.max(np.abs(audio_arr))
|
68 |
+
|
69 |
+
return sampling_rate, audio_arr
|
70 |
+
except Exception as e:
|
71 |
+
raise RuntimeError(f"Erreur lors de la génération de l'audio : {e}")
|
72 |
+
|
73 |
+
# Fonction pour mettre à jour le compteur de caractères
|
74 |
+
# def update_char_counter(text):
|
75 |
+
# remaining_chars = 200 - len(text)
|
76 |
+
# return f"Caractères restants : {remaining_chars}"
|
77 |
+
|
78 |
+
# Interface Gradio
|
79 |
+
def create_interface():
|
80 |
+
with gr.Blocks() as demo:
|
81 |
+
# Ajouter une image ou un logo
|
82 |
+
gr.Markdown("")
|
83 |
+
|
84 |
+
# Titre et description
|
85 |
+
gr.Markdown("# 🌟 Bienvenue sur Adia TTS 🌟")
|
86 |
+
gr.Markdown(f"[Adia TTS](https://huggingface.co/CONCREE/Adia_TTS) est un modèle de génération audio en wolof. Cette interface vous permet de générer des fichiers audio à partir de textes en wolof. Vous pouvez choisir une description pour personnaliser la voix générée.")
|
87 |
+
|
88 |
+
with gr.Row():
|
89 |
+
with gr.Column():
|
90 |
+
prompt_input = gr.Textbox(label="Entrez votre texte en wolof", placeholder="Ex: Abdoul nena souba dinagnou am reunion pour waxtaan li des", value=default_prompt, max_length=180)
|
91 |
+
# char_counter = gr.Label(value=update_char_counter(default_prompt))
|
92 |
+
description_input = gr.Textbox(label="Entrez une description pour la voix", value=default_description)
|
93 |
+
generate_button = gr.Button("Générer l'audio", variant="primary")
|
94 |
+
|
95 |
+
with gr.Column():
|
96 |
+
audio_output = gr.Audio(label="Audio généré", type="numpy")
|
97 |
+
|
98 |
+
# Section des exemples
|
99 |
+
gr.Markdown("## Exemples de textes et descriptions")
|
100 |
+
gr.Examples(
|
101 |
+
examples=[
|
102 |
+
[
|
103 |
+
"""Liggéeyukaay ci wàllu mbay mi ci Senegal dafa am solo lool ci wàllu kaaraange dundu ak sos liggéey, ndax dafay boole xeeti liggéey yu bees yu melni agroecologie ak togg ci gox bi.""",
|
104 |
+
default_description,
|
105 |
+
],
|
106 |
+
[
|
107 |
+
"""Entreprenariat ci Senegal dafa am solo lool ci yokkuteg koom-koom, di gëna yokk liggéey ak indi gis-gis yu bees ci dëkk bi. Ndaw yi am këru liggéey dañuy am xéewal yu amul fenn ndax ecosystem bi dafay màgg, te inisiatiif yu réew mi ak yu prive yi ñoo leen di jàppale.""",
|
108 |
+
default_description,
|
109 |
+
],
|
110 |
+
],
|
111 |
+
inputs=[prompt_input, description_input],
|
112 |
+
outputs=audio_output,
|
113 |
+
fn=generate_audio, # Fonction à appeler lors du clic sur un exemple
|
114 |
+
label="Cliquez sur un exemple pour générer l'audio",
|
115 |
+
cache_examples=True, # Optionnel : pour accélérer les exemples déjà générés
|
116 |
+
)
|
117 |
+
|
118 |
+
# Mettre à jour le compteur de caractères à chaque saisie
|
119 |
+
# prompt_input.change(fn=update_char_counter, inputs=prompt_input, outputs=char_counter)
|
120 |
+
|
121 |
+
generate_button.click(fn=generate_audio, inputs=[prompt_input, description_input], outputs=audio_output)
|
122 |
+
|
123 |
+
return demo
|
124 |
+
|
125 |
+
# Lancement de l'interface
|
126 |
+
if __name__ == "__main__":
|
127 |
+
interface = create_interface()
|
128 |
+
interface.launch()
|