SafaaAI commited on
Commit
50a0022
·
verified ·
1 Parent(s): fe0e125

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -28
app.py CHANGED
@@ -1,51 +1,49 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
3
  import os
4
 
5
- # 🔹 Récupère le token Hugging Face depuis les secrets du Space
6
  hf_token = os.environ.get("HF_TOKEN")
7
  if hf_token is None:
8
- raise ValueError("⚠️ Le token Hugging Face (HF_TOKEN) n'est pas trouvé. "
9
- "Ajoute-le dans les secrets du Space.")
10
 
11
  # 🔹 ID du modèle
12
  model_id = "SafaaAI/final_llm_darija_fr_tech"
13
 
14
- # 🔹 Création de la pipeline (texte uniquement) avec trust_remote_code=True
15
- pipe = pipeline(
16
- "text-generation",
17
- model=model_id,
18
  token=hf_token,
19
- device_map="auto", # CPU si pas de GPU
20
- trust_remote_code=True # ✅ Autoriser le code custom
21
  )
22
 
23
- # 🔹 Fonction de chat
24
  def chat_with_model(message, history):
25
  history = history or []
26
- outputs = pipe(
27
- message,
28
- max_new_tokens=200,
29
- do_sample=True,
30
- top_p=0.9,
31
- temperature=0.7,
32
- )
33
- response = outputs[0]["generated_text"]
34
-
35
- # Nettoyage
36
- if response.startswith(message):
37
- response = response[len(message):].strip()
38
-
39
  history.append((message, response))
40
  return history, history
41
 
42
- # 🔹 Interface Gradio
43
  with gr.Blocks() as demo:
44
- gr.Markdown("## 💬 Chatbot SafaaAI - LLM (Darija + Français + Technique)")
45
-
46
  chatbot = gr.Chatbot(type="messages")
47
  msg = gr.Textbox(label="Écris ton message ici")
48
- clear = gr.Button("🧹 Effacer la conversation")
49
 
50
  state = gr.State([])
51
 
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
  import os
5
 
6
+ # 🔹 Token Hugging Face
7
  hf_token = os.environ.get("HF_TOKEN")
8
  if hf_token is None:
9
+ raise ValueError("⚠️ HF_TOKEN manquant dans les secrets du Space.")
 
10
 
11
  # 🔹 ID du modèle
12
  model_id = "SafaaAI/final_llm_darija_fr_tech"
13
 
14
+ # 🔹 Charger tokenizer + modèle (forcé en text-only)
15
+ tokenizer = AutoTokenizer.from_pretrained(model_id, token=hf_token, trust_remote_code=True)
16
+ model = AutoModelForCausalLM.from_pretrained(
17
+ model_id,
18
  token=hf_token,
19
+ trust_remote_code=True,
20
+ device_map="auto"
21
  )
22
 
23
+ # 🔹 Fonction de génération texte
24
  def chat_with_model(message, history):
25
  history = history or []
26
+ inputs = tokenizer(message, return_tensors="pt").to(model.device)
27
+
28
+ with torch.no_grad():
29
+ outputs = model.generate(
30
+ **inputs,
31
+ max_new_tokens=200,
32
+ do_sample=True,
33
+ temperature=0.7,
34
+ top_p=0.9
35
+ )
36
+
37
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
38
  history.append((message, response))
39
  return history, history
40
 
41
+ # 🔹 UI Gradio
42
  with gr.Blocks() as demo:
43
+ gr.Markdown("## 💬 SafaaAI - Multimodal LLM (mode texte seulement)")
 
44
  chatbot = gr.Chatbot(type="messages")
45
  msg = gr.Textbox(label="Écris ton message ici")
46
+ clear = gr.Button("🧹 Effacer")
47
 
48
  state = gr.State([])
49