Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
MODEL_PATH = "SafaaAI/final_llm_darija_fr_tech"
|
|
|
|
| 6 |
|
|
|
|
| 7 |
print("Chargement du tokenizer...")
|
| 8 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 9 |
|
|
|
|
| 10 |
print("Chargement du modèle...")
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
MODEL_PATH,
|
| 13 |
-
|
| 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 |
-
#
|
| 20 |
-
generator = pipeline(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Fonction pour Gradio
|
| 23 |
-
def
|
| 24 |
-
|
| 25 |
-
|
| 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 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
| 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()
|