SafaaAI commited on
Commit
c8b7226
·
verified ·
1 Parent(s): 9edf621

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -22
app.py CHANGED
@@ -1,40 +1,36 @@
1
- import gradio as gr
2
- import torch
3
- from transformers import AutoTokenizer, AutoModelForCausalLM
4
  import os
 
 
 
5
 
6
- # 🔹 Récupérez le token depuis les secrets du Space
7
- # Le token sera automatiquement chargé dans les variables d'environnement
8
  hf_token = os.environ.get("HF_TOKEN")
9
-
10
- # Vérification si le token est bien chargé
11
  if hf_token is None:
12
- raise ValueError("⚠️ Le token Hugging Face (HF_TOKEN) n'est pas trouvé. "
13
- "Vérifie que tu l’as bien ajouté dans les secrets du Space.")
14
 
15
- # 🔹 Charger le tokenizer
16
- tokenizer = AutoTokenizer.from_pretrained(
17
  "SafaaAI/final_llm_darija_fr_tech",
18
- token=hf_token, # Utilisez le token pour l'authentification
 
19
  trust_remote_code=True
20
  )
21
 
22
  # 🔹 Charger le modèle
23
  model = AutoModelForCausalLM.from_pretrained(
24
  "SafaaAI/final_llm_darija_fr_tech",
25
- token=hf_token, # Utilisez le token pour l'authentification
26
- trust_remote_code=True,
27
- device_map="auto" # Pour GPU si disponible
 
28
  )
29
 
30
- # 🔹 Fonction d'inférence
31
  def chat_with_model(message, history):
32
  history = history or []
33
-
34
- # Encode texte
35
  inputs = tokenizer(message, return_tensors="pt").to(model.device)
36
 
37
- # Générer la réponse
38
  with torch.no_grad():
39
  output_ids = model.generate(
40
  **inputs,
@@ -44,14 +40,13 @@ def chat_with_model(message, history):
44
  temperature=0.7
45
  )
46
 
47
- # Décoder la sortie
48
  response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
49
  history.append((message, response))
50
  return history, history
51
 
52
  # 🔹 Interface Gradio
53
  with gr.Blocks() as demo:
54
- gr.Markdown("## 💬 Chatbot SafaaAI - LLM (Darija + Français + Technique)")
55
 
56
  chatbot = gr.Chatbot()
57
  msg = gr.Textbox(label="Écris ton message ici")
@@ -62,6 +57,6 @@ with gr.Blocks() as demo:
62
  msg.submit(chat_with_model, [msg, state], [chatbot, state])
63
  clear.click(lambda: ([], []), None, [chatbot, state])
64
 
65
- # 🔹 Lancer l'application
66
  if __name__ == "__main__":
67
  demo.launch()
 
 
 
 
1
  import os
2
+ import torch
3
+ import gradio as gr
4
+ from transformers import RobertaTokenizerFast, AutoModelForCausalLM
5
 
6
+ # 🔹 Récupération du token Hugging Face depuis les "Secrets" du Space
 
7
  hf_token = os.environ.get("HF_TOKEN")
 
 
8
  if hf_token is None:
9
+ raise ValueError("⚠️ Le secret HF_TOKEN nest pas défini dans ton Space.")
 
10
 
11
+ # 🔹 Charger le tokenizer (en ignorant tokenizer.json corrompu)
12
+ tokenizer = RobertaTokenizerFast.from_pretrained(
13
  "SafaaAI/final_llm_darija_fr_tech",
14
+ use_fast=True,
15
+ token=hf_token,
16
  trust_remote_code=True
17
  )
18
 
19
  # 🔹 Charger le modèle
20
  model = AutoModelForCausalLM.from_pretrained(
21
  "SafaaAI/final_llm_darija_fr_tech",
22
+ device_map="auto", # GPU si dispo, sinon CPU
23
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
24
+ token=hf_token,
25
+ trust_remote_code=True
26
  )
27
 
28
+ # 🔹 Fonction dinférence (chatbot)
29
  def chat_with_model(message, history):
30
  history = history or []
31
+
 
32
  inputs = tokenizer(message, return_tensors="pt").to(model.device)
33
 
 
34
  with torch.no_grad():
35
  output_ids = model.generate(
36
  **inputs,
 
40
  temperature=0.7
41
  )
42
 
 
43
  response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
44
  history.append((message, response))
45
  return history, history
46
 
47
  # 🔹 Interface Gradio
48
  with gr.Blocks() as demo:
49
+ gr.Markdown("## 💬 Chatbot SafaaAI - LLM Darija + Français + Technique")
50
 
51
  chatbot = gr.Chatbot()
52
  msg = gr.Textbox(label="Écris ton message ici")
 
57
  msg.submit(chat_with_model, [msg, state], [chatbot, state])
58
  clear.click(lambda: ([], []), None, [chatbot, state])
59
 
60
+ # 🔹 Lancer l’app
61
  if __name__ == "__main__":
62
  demo.launch()