SafaaAI commited on
Commit
0959f0c
·
verified ·
1 Parent(s): 3c2d14f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -57
app.py CHANGED
@@ -1,63 +1,50 @@
 
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()
 
1
+ import os
2
  import gradio as gr
3
+ from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ # Récupération du token depuis les secrets
6
+ hf_token = os.environ.get("HF_TOKEN")
7
+
8
+ # Vérification si le token est bien chargé
9
+ if hf_token is None:
10
+ raise ValueError("⚠️ Le token Hugging Face (HF_TOKEN) n'est pas trouvé. "
11
+ "Vérifie que tu l’as bien ajouté dans les secrets du Space.")
12
 
13
+ # Initialisation du client Hugging Face Inference
14
+ client = InferenceClient(
15
+ "SafaaAI/final_llm_darija_fr_tech",
16
+ token=hf_token
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  )
18
 
19
+ # Fonction de génération de texte
20
+ def chat_with_model(message, history):
21
+ history = history or []
22
+ response = ""
23
+
24
+ # Appel au modèle Hugging Face
25
+ for output in client.text_generation(
26
+ message,
27
+ max_new_tokens=256,
28
+ stream=True
29
+ ):
30
+ response += output.token.text
31
+
32
+ history.append((message, response))
33
+ return history, history
34
+
35
+ # Interface Gradio
36
+ with gr.Blocks() as demo:
37
+ gr.Markdown("## 💬 Chatbot SafaaAI - LLM (Darija + Français + Technique)")
38
+
39
+ chatbot = gr.Chatbot()
40
+ msg = gr.Textbox(label="Écris ton message ici")
41
+ clear = gr.Button("🧹 Effacer la conversation")
42
+
43
+ state = gr.State([])
44
+
45
+ msg.submit(chat_with_model, [msg, state], [chatbot, state])
46
+ clear.click(lambda: ([], []), None, [chatbot, state])
47
+
48
+ # Lancer l’app
49
  if __name__ == "__main__":
50
+ demo.launch()