Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,63 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
import torch
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
# ======================
|
| 8 |
-
# ⚠️ NE PAS METTRE LE TOKEN HUGGINGFACE EN CLAIR DANS LE CODE
|
| 9 |
-
# Ajouter ton token dans les Secrets du Space Hugging Face
|
| 10 |
-
# Ex : "HUGGINGFACE_TOKEN" dans Settings -> Secrets
|
| 11 |
-
# ======================
|
| 12 |
-
HF_TOKEN = os.environ.get("HUGGINGFACE_TOKEN")
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
# Charger le tokenizer
|
| 17 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 18 |
-
|
| 19 |
-
use_auth_token=
|
| 20 |
trust_remote_code=True
|
| 21 |
)
|
| 22 |
|
| 23 |
-
# Charger le modèle
|
| 24 |
model = AutoModelForCausalLM.from_pretrained(
|
| 25 |
-
|
| 26 |
-
use_auth_token=
|
| 27 |
trust_remote_code=True,
|
| 28 |
device_map="auto" # pour GPU si disponible
|
| 29 |
)
|
| 30 |
|
| 31 |
-
# Fonction
|
| 32 |
-
def
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# 🔹 Utiliser le token Hugging Face stocké dans les secrets du Space
|
| 6 |
+
# Assurez-vous d'ajouter votre HF_TOKEN dans la section Secrets
|
| 7 |
+
HF_TOKEN = "use_auth_token_from_secret" # NE PAS mettre votre vrai token ici
|
| 8 |
|
| 9 |
+
# 🔹 Charger le tokenizer
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 11 |
+
"SafaaAI/final_llm_darija_fr_tech",
|
| 12 |
+
use_auth_token=True, # prend le token du Space si configuré
|
| 13 |
trust_remote_code=True
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# 🔹 Charger le modèle
|
| 17 |
model = AutoModelForCausalLM.from_pretrained(
|
| 18 |
+
"SafaaAI/final_llm_darija_fr_tech",
|
| 19 |
+
use_auth_token=True,
|
| 20 |
trust_remote_code=True,
|
| 21 |
device_map="auto" # pour GPU si disponible
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# 🔹 Fonction d'inférence
|
| 25 |
+
def inference(input_text, input_image=None):
|
| 26 |
+
"""
|
| 27 |
+
input_text: texte utilisateur
|
| 28 |
+
input_image: image (optionnelle) pour contexte multimodal
|
| 29 |
+
"""
|
| 30 |
+
# Encode texte
|
| 31 |
+
inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
|
| 32 |
+
|
| 33 |
+
# Générer la réponse
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
output_ids = model.generate(
|
| 36 |
+
**inputs,
|
| 37 |
+
max_length=512,
|
| 38 |
+
do_sample=True,
|
| 39 |
+
top_p=0.9,
|
| 40 |
+
temperature=0.7
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# Décoder la sortie
|
| 44 |
+
response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 45 |
+
return response
|
| 46 |
+
|
| 47 |
+
# 🔹 Interface Gradio
|
| 48 |
+
iface = gr.Interface(
|
| 49 |
+
fn=inference,
|
| 50 |
+
inputs=[
|
| 51 |
+
gr.Textbox(lines=5, label="Entrez votre texte"),
|
| 52 |
+
gr.Image(type="pil", label="Image (optionnelle)")
|
| 53 |
+
],
|
| 54 |
+
outputs=[
|
| 55 |
+
gr.Textbox(lines=10, label="Réponse du modèle")
|
| 56 |
+
],
|
| 57 |
+
title="SafaaAI LLM Darija-FR-Tech",
|
| 58 |
+
description="Modèle multimodal léger pour comprendre la Darija, le français et le langage technique."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# 🔹 Lancer l'application
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
iface.launch()
|