Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,55 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 3 |
|
| 4 |
-
MODEL = "
|
| 5 |
|
|
|
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 7 |
-
model = AutoModelForCausalLM.from_pretrained(MODEL)
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
chatbot = pipeline(
|
| 10 |
"text-generation",
|
| 11 |
model=model,
|
| 12 |
tokenizer=tokenizer,
|
| 13 |
-
|
| 14 |
)
|
| 15 |
|
|
|
|
| 16 |
system_prompt = "Tu es Aria, une IA bienveillante et polie qui répond de façon concise et claire."
|
| 17 |
|
| 18 |
def chat(message, history=[]):
|
|
|
|
|
|
|
|
|
|
| 19 |
context = "\n".join([f"Utilisateur: {m[0]}\nAria: {m[1]}" for m in history])
|
| 20 |
prompt = f"{system_prompt}\n{context}\nUtilisateur: {message}\nAria:"
|
| 21 |
|
| 22 |
-
resp = chatbot(
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
| 25 |
history.append([message, reply])
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
with gr.Blocks() as demo:
|
| 29 |
chat_ui = gr.Chatbot()
|
| 30 |
msg = gr.Textbox(placeholder="Écris un message...")
|
| 31 |
-
msg.submit(chat, [msg, chat_ui], [chat_ui])
|
| 32 |
|
| 33 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 3 |
|
| 4 |
+
MODEL = "prithivMLmods/Llama-SmolTalk-3.2-1B-Instruct"
|
| 5 |
|
| 6 |
+
# Charger le tokenizer
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
|
|
|
| 8 |
|
| 9 |
+
# Charger le modèle en 8 bits pour accélérer et réduire l’usage mémoire
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
MODEL,
|
| 12 |
+
device_map="auto",
|
| 13 |
+
load_in_8bit=True # accélère sur CPU/peu de mémoire
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Pipeline de génération
|
| 17 |
chatbot = pipeline(
|
| 18 |
"text-generation",
|
| 19 |
model=model,
|
| 20 |
tokenizer=tokenizer,
|
| 21 |
+
device_map="auto"
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# Prompt système
|
| 25 |
system_prompt = "Tu es Aria, une IA bienveillante et polie qui répond de façon concise et claire."
|
| 26 |
|
| 27 |
def chat(message, history=[]):
|
| 28 |
+
# Limiter l'historique pour accélérer
|
| 29 |
+
history = history[-3:]
|
| 30 |
+
|
| 31 |
context = "\n".join([f"Utilisateur: {m[0]}\nAria: {m[1]}" for m in history])
|
| 32 |
prompt = f"{system_prompt}\n{context}\nUtilisateur: {message}\nAria:"
|
| 33 |
|
| 34 |
+
resp = chatbot(
|
| 35 |
+
prompt,
|
| 36 |
+
max_new_tokens=60, # plus court pour CPU
|
| 37 |
+
do_sample=True,
|
| 38 |
+
temperature=0.7,
|
| 39 |
+
top_p=0.9,
|
| 40 |
+
repetition_penalty=1.1
|
| 41 |
+
)[0]["generated_text"]
|
| 42 |
|
| 43 |
+
reply = resp.split("Aria:")[-1].strip()
|
| 44 |
history.append([message, reply])
|
| 45 |
+
|
| 46 |
+
# Convertir en tuples pour Gradio
|
| 47 |
+
chat_display = [(m[0], m[1]) for m in history]
|
| 48 |
+
return chat_display, history
|
| 49 |
|
| 50 |
with gr.Blocks() as demo:
|
| 51 |
chat_ui = gr.Chatbot()
|
| 52 |
msg = gr.Textbox(placeholder="Écris un message...")
|
| 53 |
+
msg.submit(chat, [msg, chat_ui], [chat_ui, chat_ui])
|
| 54 |
|
| 55 |
demo.launch()
|