SafaaAI commited on
Commit
eb77eeb
·
verified ·
1 Parent(s): bd94b1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -23
app.py CHANGED
@@ -1,40 +1,46 @@
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
 
3
 
4
- # Chemin vers ton modèle sur HF Hub
5
  MODEL_PATH = "SafaaAI/final_llm_darija_fr_tech"
 
6
 
 
7
  print("Chargement du tokenizer...")
8
- tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, trust_remote_code=True)
9
 
 
10
  print("Chargement du modèle...")
11
  model = AutoModelForCausalLM.from_pretrained(
12
  MODEL_PATH,
13
- trust_remote_code=True,
14
- device_map="auto", # utilise GPU si dispo, sinon CPU
15
- load_in_4bit=False, # désactive BitsAndBytes pour éviter l'erreur
16
- load_in_8bit=False
17
  )
18
 
19
- # Crée un pipeline text generation
20
- generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
 
 
 
 
 
21
 
22
  # Fonction pour Gradio
23
- def chat_with_model(prompt, image=None):
24
- """
25
- prompt: texte de l'utilisateur
26
- image: optionnel, pour futur traitement multimodal
27
- """
28
- response = generator(prompt, max_length=200, do_sample=True)[0]["generated_text"]
29
- return response
30
 
31
  # Interface Gradio
32
- iface = gr.Interface(
33
- fn=chat_with_model,
34
- inputs=[gr.Textbox(label="Votre question"), gr.Image(label="Image (optionnel)")],
35
- outputs=[gr.Textbox(label="Réponse du modèle")],
36
- title="SafaaAI LLM - Darija & Français Technique",
37
- description="Chattez avec le LLM multimodal léger (texte et image)."
38
- )
 
 
 
39
 
40
- iface.launch()
 
 
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
+ import torch
4
 
5
+ # Chemins locaux sur Spaces
6
  MODEL_PATH = "SafaaAI/final_llm_darija_fr_tech"
7
+ TOKENIZER_PATH = "tokenizer_safe"
8
 
9
+ # Chargement du tokenizer sans trust_remote_code
10
  print("Chargement du tokenizer...")
11
+ tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_PATH)
12
 
13
+ # Chargement du modèle
14
  print("Chargement du modèle...")
15
  model = AutoModelForCausalLM.from_pretrained(
16
  MODEL_PATH,
17
+ device_map="auto" # utilise le GPU si disponible
 
 
 
18
  )
19
 
20
+ # Création du pipeline de génération de texte
21
+ generator = pipeline(
22
+ "text-generation",
23
+ model=model,
24
+ tokenizer=tokenizer,
25
+ device=0 if torch.cuda.is_available() else -1
26
+ )
27
 
28
  # Fonction pour Gradio
29
+ def generate_text(prompt, max_length=200):
30
+ output = generator(prompt, max_length=max_length, do_sample=True, top_p=0.95)
31
+ return output[0]["generated_text"]
 
 
 
 
32
 
33
  # Interface Gradio
34
+ with gr.Blocks() as demo:
35
+ gr.Markdown("# SafaaAI LLM - Chat Texte")
36
+
37
+ with gr.Row():
38
+ prompt_input = gr.Textbox(label="Entrez votre texte ici", lines=3)
39
+ submit_btn = gr.Button("Générer")
40
+
41
+ output_text = gr.Textbox(label="Résultat généré", lines=10)
42
+
43
+ submit_btn.click(generate_text, inputs=prompt_input, outputs=output_text)
44
 
45
+ # Lancer l'application
46
+ demo.launch()