Spaces:
Running on Zero
Running on Zero
v11 sur ZeroGPU — quotidien fr→pul 41.98, pul→fr 42.42, Coran fr→pul 52.84
Browse files
app.py
CHANGED
|
@@ -3,9 +3,25 @@ import gradio as gr
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
from peft import PeftModel
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
BASE_MODEL = "facebook/nllb-200-distilled-1.3B"
|
| 7 |
LORA_REPO = "kawkumputer/pulaar-ai-nllb-1.3b-v11"
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
LANG_FR = "fra_Latn"
|
| 11 |
LANG_PUL = "fuv_Latn"
|
|
@@ -23,16 +39,19 @@ tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
|
| 23 |
print("Chargement du modèle de base...")
|
| 24 |
base_model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 25 |
BASE_MODEL,
|
| 26 |
-
torch_dtype=
|
| 27 |
low_cpu_mem_usage=True,
|
| 28 |
)
|
| 29 |
|
| 30 |
print(f"Application des adaptateurs LoRA depuis {LORA_REPO}...")
|
| 31 |
model = PeftModel.from_pretrained(base_model, LORA_REPO)
|
| 32 |
model.eval()
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
|
|
|
|
| 36 |
def translate(text: str, direction: str) -> str:
|
| 37 |
if not text or not text.strip():
|
| 38 |
return ""
|
|
@@ -43,7 +62,7 @@ def translate(text: str, direction: str) -> str:
|
|
| 43 |
return_tensors="pt",
|
| 44 |
truncation=True,
|
| 45 |
max_length=256,
|
| 46 |
-
)
|
| 47 |
forced_bos = tokenizer.convert_tokens_to_ids(tgt_lang)
|
| 48 |
with torch.no_grad():
|
| 49 |
out = model.generate(
|
|
@@ -64,7 +83,9 @@ with gr.Blocks(title="PulaarAI — Traducteur Pulaar") as demo:
|
|
| 64 |
"Développé par Hamath Kane, Abou Sy, Bocar Amadou Ba (ARPRIM) "
|
| 65 |
"& Alassane Mountaga Sall « Dembel »\n\n"
|
| 66 |
"Basé sur **NLLB-200-distilled-1.3B** + LoRA fine-tuné · "
|
| 67 |
-
"
|
|
|
|
|
|
|
| 68 |
)
|
| 69 |
|
| 70 |
gr.Markdown(
|
|
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
from peft import PeftModel
|
| 5 |
|
| 6 |
+
# ZeroGPU fournit un GPU partage aux Spaces des comptes Pro. Le paquet `spaces`
|
| 7 |
+
# n'existe que dans cet environnement : hors ZeroGPU on retombe sur un
|
| 8 |
+
# decorateur neutre, et le Space continue de tourner sur CPU. Resilier
|
| 9 |
+
# l'abonnement ne casse donc rien, ce sera seulement plus lent.
|
| 10 |
+
try:
|
| 11 |
+
import spaces
|
| 12 |
+
sur_gpu = spaces.GPU(duration=120)
|
| 13 |
+
ZEROGPU = True
|
| 14 |
+
except Exception:
|
| 15 |
+
def sur_gpu(fonction):
|
| 16 |
+
return fonction
|
| 17 |
+
ZEROGPU = False
|
| 18 |
+
|
| 19 |
BASE_MODEL = "facebook/nllb-200-distilled-1.3B"
|
| 20 |
LORA_REPO = "kawkumputer/pulaar-ai-nllb-1.3b-v11"
|
| 21 |
+
|
| 22 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 23 |
+
DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
|
| 24 |
+
print(f"ZeroGPU : {ZEROGPU} | peripherique : {DEVICE} | precision : {DTYPE}")
|
| 25 |
|
| 26 |
LANG_FR = "fra_Latn"
|
| 27 |
LANG_PUL = "fuv_Latn"
|
|
|
|
| 39 |
print("Chargement du modèle de base...")
|
| 40 |
base_model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 41 |
BASE_MODEL,
|
| 42 |
+
torch_dtype=DTYPE,
|
| 43 |
low_cpu_mem_usage=True,
|
| 44 |
)
|
| 45 |
|
| 46 |
print(f"Application des adaptateurs LoRA depuis {LORA_REPO}...")
|
| 47 |
model = PeftModel.from_pretrained(base_model, LORA_REPO)
|
| 48 |
model.eval()
|
| 49 |
+
if DEVICE == "cuda":
|
| 50 |
+
model = model.to(DEVICE)
|
| 51 |
+
print(f"Modele pret sur {DEVICE}.")
|
| 52 |
|
| 53 |
|
| 54 |
+
@sur_gpu
|
| 55 |
def translate(text: str, direction: str) -> str:
|
| 56 |
if not text or not text.strip():
|
| 57 |
return ""
|
|
|
|
| 62 |
return_tensors="pt",
|
| 63 |
truncation=True,
|
| 64 |
max_length=256,
|
| 65 |
+
).to(DEVICE)
|
| 66 |
forced_bos = tokenizer.convert_tokens_to_ids(tgt_lang)
|
| 67 |
with torch.no_grad():
|
| 68 |
out = model.generate(
|
|
|
|
| 83 |
"Développé par Hamath Kane, Abou Sy, Bocar Amadou Ba (ARPRIM) "
|
| 84 |
"& Alassane Mountaga Sall « Dembel »\n\n"
|
| 85 |
"Basé sur **NLLB-200-distilled-1.3B** + LoRA fine-tuné · "
|
| 86 |
+
+ ("⚡ Inférence GPU — quelques secondes par traduction"
|
| 87 |
+
if DEVICE == "cuda" else
|
| 88 |
+
"⚠️ Inférence CPU — comptez 20-40 secondes par traduction")
|
| 89 |
)
|
| 90 |
|
| 91 |
gr.Markdown(
|