Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -95,14 +95,95 @@
|
|
95 |
# if __name__ == "__main__":
|
96 |
# demo.launch()
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
import torch
|
99 |
import gradio as gr
|
100 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
101 |
|
102 |
# --- Config ---
|
103 |
MODEL_REPO = "Toadoum/ngambay-fr-v1"
|
104 |
-
FR_CODE = "
|
105 |
-
NG_CODE = "
|
|
|
|
|
|
|
|
|
106 |
|
107 |
# --- Device selection ---
|
108 |
device = 0 if torch.cuda.is_available() else -1
|
@@ -118,55 +199,102 @@ translator = pipeline(
|
|
118 |
device=device,
|
119 |
)
|
120 |
|
121 |
-
def translate_fr_to_ng(text
|
122 |
if not text or not text.strip():
|
123 |
return ""
|
124 |
out = translator(
|
125 |
text,
|
126 |
src_lang=FR_CODE,
|
127 |
tgt_lang=NG_CODE,
|
128 |
-
max_new_tokens=
|
129 |
-
do_sample=
|
130 |
-
temperature=temperature if temperature > 0 else None,
|
131 |
)
|
132 |
return out[0]["translation_text"]
|
133 |
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
)
|
164 |
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
if __name__ == "__main__":
|
172 |
-
demo.launch()
|
|
|
95 |
# if __name__ == "__main__":
|
96 |
# demo.launch()
|
97 |
|
98 |
+
# import torch
|
99 |
+
# import gradio as gr
|
100 |
+
# from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
101 |
+
|
102 |
+
# # --- Config ---
|
103 |
+
# MODEL_REPO = "Toadoum/ngambay-fr-v1"
|
104 |
+
# FR_CODE = "sba_Latn"
|
105 |
+
# NG_CODE = "fr_Latn" # Ngambay (Saba) Latin
|
106 |
+
|
107 |
+
# # --- Device selection ---
|
108 |
+
# device = 0 if torch.cuda.is_available() else -1
|
109 |
+
|
110 |
+
# # --- Load model & tokenizer once ---
|
111 |
+
# tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO)
|
112 |
+
# model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_REPO)
|
113 |
+
|
114 |
+
# translator = pipeline(
|
115 |
+
# task="translation",
|
116 |
+
# model=model,
|
117 |
+
# tokenizer=tokenizer,
|
118 |
+
# device=device,
|
119 |
+
# )
|
120 |
+
|
121 |
+
# def translate_fr_to_ng(text, max_new_tokens=256, temperature=0.0):
|
122 |
+
# if not text or not text.strip():
|
123 |
+
# return ""
|
124 |
+
# out = translator(
|
125 |
+
# text,
|
126 |
+
# src_lang=FR_CODE,
|
127 |
+
# tgt_lang=NG_CODE,
|
128 |
+
# max_new_tokens=max_new_tokens,
|
129 |
+
# do_sample=temperature > 0,
|
130 |
+
# temperature=temperature if temperature > 0 else None,
|
131 |
+
# )
|
132 |
+
# return out[0]["translation_text"]
|
133 |
+
|
134 |
+
# with gr.Blocks(title="Français → Ngambay · Toadoum/ngambay-fr-v1") as demo:
|
135 |
+
# gr.Markdown(
|
136 |
+
# """
|
137 |
+
# # Français → Ngambay (v1)
|
138 |
+
# Model: **Toadoum/ngambay-fr-v1**
|
139 |
+
# Entrez un texte en français et obtenez la traduction en Ngambay.
|
140 |
+
# """
|
141 |
+
# )
|
142 |
+
|
143 |
+
# with gr.Row():
|
144 |
+
# with gr.Column():
|
145 |
+
# src = gr.Textbox(
|
146 |
+
# label="Texte source (Français)",
|
147 |
+
# placeholder="Entrez le texte à traduire…",
|
148 |
+
# lines=8
|
149 |
+
# )
|
150 |
+
# max_new_tokens = gr.Slider(
|
151 |
+
# minimum=32, maximum=1024, value=256, step=32,
|
152 |
+
# label="Max new tokens"
|
153 |
+
# )
|
154 |
+
# temperature = gr.Slider(
|
155 |
+
# minimum=0.0, maximum=1.0, value=0.0, step=0.05,
|
156 |
+
# label="Creativity (temperature)"
|
157 |
+
# )
|
158 |
+
# btn = gr.Button("Traduire", variant="primary")
|
159 |
+
# with gr.Column():
|
160 |
+
# tgt = gr.Textbox(
|
161 |
+
# label="Traduction (Ngambay)",
|
162 |
+
# lines=8
|
163 |
+
# )
|
164 |
+
|
165 |
+
# btn.click(
|
166 |
+
# fn=translate_fr_to_ng,
|
167 |
+
# inputs=[src, max_new_tokens, temperature],
|
168 |
+
# outputs=[tgt]
|
169 |
+
# )
|
170 |
+
|
171 |
+
# if __name__ == "__main__":
|
172 |
+
# demo.launch()
|
173 |
+
|
174 |
+
|
175 |
import torch
|
176 |
import gradio as gr
|
177 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
178 |
|
179 |
# --- Config ---
|
180 |
MODEL_REPO = "Toadoum/ngambay-fr-v1"
|
181 |
+
FR_CODE = "fr_Latn" # Français
|
182 |
+
NG_CODE = "sba_Latn" # Ngambay (Saba) Latin
|
183 |
+
|
184 |
+
# --- Inference params (fixés pour l'utilisateur) ---
|
185 |
+
MAX_NEW_TOKENS = 256
|
186 |
+
TEMPERATURE = 0.0
|
187 |
|
188 |
# --- Device selection ---
|
189 |
device = 0 if torch.cuda.is_available() else -1
|
|
|
199 |
device=device,
|
200 |
)
|
201 |
|
202 |
+
def translate_fr_to_ng(text: str) -> str:
|
203 |
if not text or not text.strip():
|
204 |
return ""
|
205 |
out = translator(
|
206 |
text,
|
207 |
src_lang=FR_CODE,
|
208 |
tgt_lang=NG_CODE,
|
209 |
+
max_new_tokens=MAX_NEW_TOKENS,
|
210 |
+
do_sample=False, # TEMPERATURE=0.0 -> deterministic
|
|
|
211 |
)
|
212 |
return out[0]["translation_text"]
|
213 |
|
214 |
+
theme = gr.themes.Soft(
|
215 |
+
primary_hue="indigo",
|
216 |
+
radius_size="lg",
|
217 |
+
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui"]
|
218 |
+
).set(
|
219 |
+
body_background_fill="#f7f7fb",
|
220 |
+
button_primary_text_color="#ffffff"
|
221 |
+
)
|
222 |
|
223 |
+
CUSTOM_CSS = """
|
224 |
+
.gradio-container {max-width: 980px !important;}
|
225 |
+
.header-card {
|
226 |
+
background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
|
227 |
+
color: white; padding: 22px; border-radius: 18px;
|
228 |
+
box-shadow: 0 10px 30px rgba(79,70,229,.25);
|
229 |
+
}
|
230 |
+
.header-title { font-size: 26px; font-weight: 800; margin: 0 0 6px 0; }
|
231 |
+
.header-sub { opacity: .95; font-size: 14px; }
|
232 |
+
.brand { display:flex; align-items:center; gap:10px; }
|
233 |
+
.badge {
|
234 |
+
display:inline-block; background: rgba(255,255,255,.18);
|
235 |
+
padding: 4px 10px; border-radius: 999px; font-size: 12px;
|
236 |
+
border: 1px solid rgba(255,255,255,.25);
|
237 |
+
}
|
238 |
+
.footer-note {
|
239 |
+
margin-top: 8px; color: #64748b; font-size: 12px; text-align: center;
|
240 |
+
}
|
241 |
+
"""
|
242 |
+
|
243 |
+
with gr.Blocks(title="Français → Ngambay · Toadoum/ngambay-fr-v1", theme=theme, css=CUSTOM_CSS, fill_height=True, analytics_enabled=False) as demo:
|
244 |
+
with gr.Column():
|
245 |
+
with gr.Group(elem_classes=["header-card"]):
|
246 |
+
gr.HTML(
|
247 |
+
"""
|
248 |
+
<div class="brand">
|
249 |
+
<div>
|
250 |
+
<div class="header-title">Français → Ngambay (v1)</div>
|
251 |
+
<div class="header-sub">Traduction rapide et fidèle pour la langue la plus parlée au Tchad.</div>
|
252 |
+
</div>
|
253 |
+
<span class="badge">Modèle : Toadoum/ngambay-fr-v1</span>
|
254 |
+
</div>
|
255 |
+
"""
|
256 |
)
|
257 |
|
258 |
+
with gr.Row():
|
259 |
+
with gr.Column(scale=5):
|
260 |
+
src = gr.Textbox(
|
261 |
+
label="Texte source (Français)",
|
262 |
+
placeholder="Saisissez votre texte en français…",
|
263 |
+
lines=8,
|
264 |
+
autofocus=True
|
265 |
+
)
|
266 |
+
with gr.Row():
|
267 |
+
btn = gr.Button("Traduire", variant="primary", scale=3)
|
268 |
+
clear_btn = gr.Button("Effacer", scale=1)
|
269 |
+
|
270 |
+
gr.Examples(
|
271 |
+
examples=[
|
272 |
+
["Bonjour, comment allez-vous aujourd’hui ?"],
|
273 |
+
["La réunion de sensibilisation aura lieu demain au centre communautaire."],
|
274 |
+
["Merci pour votre participation et votre soutien."],
|
275 |
+
["Veuillez suivre les recommandations de santé pour protéger votre famille."]
|
276 |
+
],
|
277 |
+
inputs=[src],
|
278 |
+
label="Exemples (cliquez pour remplir)"
|
279 |
+
)
|
280 |
+
|
281 |
+
with gr.Column(scale=5):
|
282 |
+
tgt = gr.Textbox(
|
283 |
+
label="Traduction (Ngambay)",
|
284 |
+
lines=8,
|
285 |
+
interactive=False,
|
286 |
+
show_copy_button=True
|
287 |
+
)
|
288 |
+
meta = gr.Markdown(
|
289 |
+
f"**Paramètres** : `max_new_tokens={MAX_NEW_TOKENS}`, `temperature={TEMPERATURE}` · "
|
290 |
+
f"`src_lang={FR_CODE}` → `tgt_lang={NG_CODE}`",
|
291 |
+
elem_id="meta"
|
292 |
+
)
|
293 |
+
|
294 |
+
gr.Markdown('<div class="footer-note">Astuce : collez un paragraphe complet pour un meilleur contexte.</div>')
|
295 |
+
|
296 |
+
btn.click(translate_fr_to_ng, inputs=src, outputs=tgt)
|
297 |
+
clear_btn.click(lambda: ("", ""), outputs=[src, tgt])
|
298 |
|
299 |
if __name__ == "__main__":
|
300 |
+
demo.queue(api_open=False, concurrency_count=4).launch(show_api=False)
|