Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Charger le modèle et le tokenizer
|
6 |
+
model_name = "Dorian2B/Vera-Instruct"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
|
9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
+
model = model.to(device)
|
11 |
+
|
12 |
+
def generate_response(message, history):
|
13 |
+
# Formatage de l'historique de conversation
|
14 |
+
prompt = ""
|
15 |
+
if history:
|
16 |
+
for user_msg, bot_msg in history:
|
17 |
+
prompt += f"<|user|>{user_msg}</s>\n<|assistant|>{bot_msg}</s>\n"
|
18 |
+
prompt += f"<|user|>{message}</s>\n<|assistant|>"
|
19 |
+
|
20 |
+
# Encodage et génération
|
21 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
22 |
+
outputs = model.generate(
|
23 |
+
**inputs,
|
24 |
+
max_new_tokens=512,
|
25 |
+
do_sample=True,
|
26 |
+
temperature=0.7,
|
27 |
+
top_p=0.9,
|
28 |
+
repetition_penalty=1.1,
|
29 |
+
eos_token_id=tokenizer.eos_token_id,
|
30 |
+
)
|
31 |
+
|
32 |
+
# Décodage et nettoyage de la réponse
|
33 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
34 |
+
response = response.split("<|assistant|>")[-1].strip()
|
35 |
+
|
36 |
+
return response
|
37 |
+
|
38 |
+
# Interface Gradio avec ChatInterface
|
39 |
+
demo = gr.ChatInterface(
|
40 |
+
fn=generate_response,
|
41 |
+
title="Chat avec Vera-Instruct",
|
42 |
+
description="Discutez avec le modèle Vera-Instruct de Dorian2B",
|
43 |
+
examples=["Bonjour, comment ça va ?",
|
44 |
+
"Explique-moi le théorème de Pythagore",
|
45 |
+
"Donne-moi une recette de cookies"],
|
46 |
+
theme="soft"
|
47 |
+
)
|
48 |
+
|
49 |
+
# Pour Hugging Face Spaces, utilisez launch()
|
50 |
+
if __name__ == "__main__":
|
51 |
+
demo.launch()
|