El-Alberto67 commited on
Commit
018ef1a
·
verified ·
1 Parent(s): 66faf04

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +33 -0
main.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+
5
+ # Charger le modèle
6
+ model_name = "mistralai/Mistral-7B-Instruct-v0.2"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ model_name,
10
+ torch_dtype=torch.float16,
11
+ device_map="auto"
12
+ )
13
+
14
+ # Prompt système pour personnaliser Aria
15
+ system_prompt = """Tu es Aria, une IA bienveillante et polie qui répond de façon concise et claire."""
16
+
17
+ def chat(message, history=[]):
18
+ prompt = system_prompt + "\n" + "\n".join([f"Utilisateur: {m[0]}\nAria: {m[1]}" for m in history]) + f"\nUtilisateur: {message}\nAria:"
19
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
20
+ outputs = model.generate(**inputs, max_new_tokens=200)
21
+ reply = tokenizer.decode(outputs[0], skip_special_tokens=True)
22
+ # Récupérer uniquement la réponse d'Aria
23
+ reply = reply.split("Aria:")[-1].strip()
24
+ history.append((message, reply))
25
+ return reply, history
26
+
27
+ # Interface Gradio
28
+ with gr.Blocks() as demo:
29
+ chatbot = gr.Chatbot()
30
+ msg = gr.Textbox(placeholder="Écris un message...")
31
+ msg.submit(chat, [msg, chatbot], [chatbot, chatbot])
32
+
33
+ demo.launch()