Spaces:
Running
on
Zero
Running
on
Zero
Update goai_helpers/goai_traduction.py
Browse files- goai_helpers/goai_traduction.py +13 -36
goai_helpers/goai_traduction.py
CHANGED
@@ -9,27 +9,14 @@ max_length = 512
|
|
9 |
auth_token = os.getenv('HF_SPACE_TOKEN')
|
10 |
login(token=auth_token)
|
11 |
|
12 |
-
def
|
13 |
-
"""Initialise et retourne le tokenizer approprié"""
|
14 |
-
if src_lang == "mos_Latn" and tgt_lang == "fra_Latn":
|
15 |
-
model_id = "ArissBandoss/3b-new-400"
|
16 |
-
else:
|
17 |
-
model_id = "ArissBandoss/nllb-200-distilled-600M-finetuned-fr-to-mos-V4"
|
18 |
-
|
19 |
-
return AutoTokenizer.from_pretrained(model_id, token=auth_token)
|
20 |
-
|
21 |
-
def split_text_by_tokens(text, src_lang, tgt_lang, max_tokens_per_chunk=200):
|
22 |
"""
|
23 |
-
Divise le texte en chunks en respectant les phrases
|
24 |
"""
|
25 |
-
|
26 |
-
tokenizer.src_lang = src_lang
|
27 |
-
|
28 |
-
# Séparation basée sur les phrases
|
29 |
sentences = re.split(r'([.!?])', text)
|
30 |
chunks = []
|
31 |
current_chunk = ""
|
32 |
-
current_tokens = 0
|
33 |
|
34 |
for i in range(0, len(sentences), 2):
|
35 |
# Reconstruire la phrase avec sa ponctuation
|
@@ -38,17 +25,12 @@ def split_text_by_tokens(text, src_lang, tgt_lang, max_tokens_per_chunk=200):
|
|
38 |
else:
|
39 |
sentence = sentences[i]
|
40 |
|
41 |
-
#
|
42 |
-
|
43 |
-
|
44 |
-
# Si l'ajout de cette phrase dépasse la limite de tokens, on crée un nouveau chunk
|
45 |
-
if current_tokens + sentence_tokens > max_tokens_per_chunk and current_chunk:
|
46 |
chunks.append(current_chunk.strip())
|
47 |
current_chunk = sentence
|
48 |
-
current_tokens = sentence_tokens
|
49 |
else:
|
50 |
current_chunk += sentence
|
51 |
-
current_tokens += sentence_tokens
|
52 |
|
53 |
# Ajouter le dernier chunk s'il reste du texte
|
54 |
if current_chunk:
|
@@ -57,15 +39,10 @@ def split_text_by_tokens(text, src_lang, tgt_lang, max_tokens_per_chunk=200):
|
|
57 |
return chunks
|
58 |
|
59 |
@spaces.GPU
|
60 |
-
def goai_traduction(text, src_lang, tgt_lang,
|
61 |
-
#
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
text_tokens = len(tokenizer.encode(text))
|
66 |
-
|
67 |
-
if text_tokens > max_tokens_per_chunk:
|
68 |
-
chunks = split_text_by_tokens(text, src_lang, tgt_lang, max_tokens_per_chunk)
|
69 |
translations = []
|
70 |
|
71 |
for chunk in chunks:
|
@@ -96,14 +73,14 @@ def translate_chunk(text, src_lang, tgt_lang):
|
|
96 |
# ID du token de langue cible
|
97 |
tgt_lang_id = tokenizer.convert_tokens_to_ids(tgt_lang)
|
98 |
|
99 |
-
# Paramètres de génération optimisés
|
100 |
outputs = model.generate(
|
101 |
**inputs,
|
102 |
forced_bos_token_id=tgt_lang_id,
|
103 |
max_new_tokens=512,
|
104 |
num_beams=5,
|
105 |
-
no_repeat_ngram_size=
|
106 |
-
repetition_penalty=
|
107 |
length_penalty=1.0,
|
108 |
early_stopping=True
|
109 |
)
|
@@ -114,4 +91,4 @@ def translate_chunk(text, src_lang, tgt_lang):
|
|
114 |
return translation
|
115 |
|
116 |
def real_time_traduction(input_text, src_lang, tgt_lang):
|
117 |
-
return goai_traduction(input_text, src_lang, tgt_lang
|
|
|
9 |
auth_token = os.getenv('HF_SPACE_TOKEN')
|
10 |
login(token=auth_token)
|
11 |
|
12 |
+
def split_text_intelligently(text, max_chunk_length=100):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
"""
|
14 |
+
Divise le texte en chunks en respectant les phrases complètes.
|
15 |
"""
|
16 |
+
# Séparation basée sur les phrases (utilise les points, points d'interrogation, etc.)
|
|
|
|
|
|
|
17 |
sentences = re.split(r'([.!?])', text)
|
18 |
chunks = []
|
19 |
current_chunk = ""
|
|
|
20 |
|
21 |
for i in range(0, len(sentences), 2):
|
22 |
# Reconstruire la phrase avec sa ponctuation
|
|
|
25 |
else:
|
26 |
sentence = sentences[i]
|
27 |
|
28 |
+
# Si l'ajout de cette phrase dépasse la longueur maximale, on crée un nouveau chunk
|
29 |
+
if len(current_chunk) + len(sentence) > max_chunk_length and current_chunk:
|
|
|
|
|
|
|
30 |
chunks.append(current_chunk.strip())
|
31 |
current_chunk = sentence
|
|
|
32 |
else:
|
33 |
current_chunk += sentence
|
|
|
34 |
|
35 |
# Ajouter le dernier chunk s'il reste du texte
|
36 |
if current_chunk:
|
|
|
39 |
return chunks
|
40 |
|
41 |
@spaces.GPU
|
42 |
+
def goai_traduction(text, src_lang, tgt_lang, max_chunk_length=100):
|
43 |
+
# Si le texte est trop long, le diviser en chunks
|
44 |
+
if len(text) > max_chunk_length:
|
45 |
+
chunks = split_text_intelligently(text, max_chunk_length)
|
|
|
|
|
|
|
|
|
|
|
46 |
translations = []
|
47 |
|
48 |
for chunk in chunks:
|
|
|
73 |
# ID du token de langue cible
|
74 |
tgt_lang_id = tokenizer.convert_tokens_to_ids(tgt_lang)
|
75 |
|
76 |
+
# Paramètres de génération optimisés pour éviter les répétitions
|
77 |
outputs = model.generate(
|
78 |
**inputs,
|
79 |
forced_bos_token_id=tgt_lang_id,
|
80 |
max_new_tokens=512,
|
81 |
num_beams=5,
|
82 |
+
no_repeat_ngram_size=4,
|
83 |
+
repetition_penalty=2.0,
|
84 |
length_penalty=1.0,
|
85 |
early_stopping=True
|
86 |
)
|
|
|
91 |
return translation
|
92 |
|
93 |
def real_time_traduction(input_text, src_lang, tgt_lang):
|
94 |
+
return goai_traduction(input_text, src_lang, tgt_lang)
|