Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,50 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
# Récupérer le token
|
| 7 |
hf_token = os.environ.get("HF_TOKEN")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
model_name = "
|
| 11 |
|
| 12 |
-
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=hf_token)
|
|
|
|
|
|
|
| 14 |
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
model_name,
|
| 16 |
-
torch_dtype=torch.float16,
|
| 17 |
-
device_map="auto",
|
| 18 |
use_auth_token=hf_token
|
| 19 |
)
|
| 20 |
|
| 21 |
# Prompt système pour Aria
|
| 22 |
-
system_prompt = "
|
| 23 |
|
|
|
|
| 24 |
def chat(message, history=[]):
|
|
|
|
| 25 |
prompt = system_prompt + "\n" + "\n".join([f"Utilisateur: {m[0]}\nAria: {m[1]}" for m in history]) + f"\nUtilisateur: {message}\nAria:"
|
|
|
|
| 26 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
reply = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 29 |
# Récupérer uniquement la réponse d'Aria
|
| 30 |
reply = reply.split("Aria:")[-1].strip()
|
| 31 |
history.append((message, reply))
|
|
|
|
| 32 |
return reply, history
|
| 33 |
|
| 34 |
# Interface Gradio
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
|
| 4 |
import torch
|
| 5 |
|
| 6 |
+
# Récupérer le token depuis les secrets/variables d'environnement
|
| 7 |
hf_token = os.environ.get("HF_TOKEN")
|
| 8 |
|
| 9 |
+
# Nom du modèle MLC quantifié (rapide et léger)
|
| 10 |
+
model_name = "mlc-ai/gemma-2b-it-q4f16_1-MLC"
|
| 11 |
|
| 12 |
+
print("Chargement du tokenizer...")
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=hf_token)
|
| 14 |
+
|
| 15 |
+
print("Chargement du modèle...")
|
| 16 |
model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
model_name,
|
| 18 |
+
torch_dtype=torch.float16, # Float16 pour accélérer si GPU
|
| 19 |
+
device_map="auto", # CPU ou GPU si disponible
|
| 20 |
use_auth_token=hf_token
|
| 21 |
)
|
| 22 |
|
| 23 |
# Prompt système pour Aria
|
| 24 |
+
system_prompt = "Tu es Aria, une IA bienveillante et polie qui répond de façon concise et claire."
|
| 25 |
|
| 26 |
+
# Fonction de chat
|
| 27 |
def chat(message, history=[]):
|
| 28 |
+
# Construire le prompt avec l'historique
|
| 29 |
prompt = system_prompt + "\n" + "\n".join([f"Utilisateur: {m[0]}\nAria: {m[1]}" for m in history]) + f"\nUtilisateur: {message}\nAria:"
|
| 30 |
+
|
| 31 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 32 |
+
|
| 33 |
+
# Génération rapide
|
| 34 |
+
generation_config = GenerationConfig(
|
| 35 |
+
max_new_tokens=200,
|
| 36 |
+
temperature=0.7,
|
| 37 |
+
do_sample=True,
|
| 38 |
+
top_p=0.9
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
outputs = model.generate(**inputs, generation_config=generation_config)
|
| 42 |
reply = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 43 |
+
|
| 44 |
# Récupérer uniquement la réponse d'Aria
|
| 45 |
reply = reply.split("Aria:")[-1].strip()
|
| 46 |
history.append((message, reply))
|
| 47 |
+
|
| 48 |
return reply, history
|
| 49 |
|
| 50 |
# Interface Gradio
|