SafaaAI commited on
Commit
3c2d14f
·
verified ·
1 Parent(s): 826a313

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -47
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 os
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
- MODEL_PATH = "SafaaAI/final_llm_darija_fr_tech"
 
 
15
 
16
- # Charger le tokenizer
17
  tokenizer = AutoTokenizer.from_pretrained(
18
- MODEL_PATH,
19
- use_auth_token=HF_TOKEN,
20
  trust_remote_code=True
21
  )
22
 
23
- # Charger le modèle
24
  model = AutoModelForCausalLM.from_pretrained(
25
- MODEL_PATH,
26
- use_auth_token=HF_TOKEN,
27
  trust_remote_code=True,
28
  device_map="auto" # pour GPU si disponible
29
  )
30
 
31
- # Fonction pour générer les réponses
32
- def chat_with_model(user_input, history=[]):
33
- # Ajouter le contexte
34
- full_input = "\n".join([f"Utilisateur: {h[0]}\nModèle: {h[1]}" for h in history] + [f"Utilisateur: {user_input}"])
35
-
36
- inputs = tokenizer(full_input, return_tensors="pt").to(model.device)
37
- output_ids = model.generate(**inputs, max_new_tokens=200)
38
- output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
39
-
40
- # Extraire seulement la réponse du modèle après le dernier "Utilisateur: ..."
41
- if "Utilisateur:" in output_text:
42
- output_text = output_text.split("Utilisateur:")[-1].strip()
43
- if "Modèle:" in output_text:
44
- output_text = output_text.split("Modèle:")[-1].strip()
45
-
46
- history.append((user_input, output_text))
47
- return output_text, history
48
-
49
- # Interface Gradio
50
- with gr.Blocks() as demo:
51
- gr.Markdown("# évaluation 🤖")
52
-
53
- chatbot = gr.Chatbot()
54
- msg = gr.Textbox(label="Votre message")
55
- clear = gr.Button("Effacer l'historique")
56
-
57
- msg.submit(chat_with_model, inputs=[msg, chatbot], outputs=[chatbot, chatbot])
58
- clear.click(lambda: [], None, chatbot)
59
-
60
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
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()