Spaces:
Sleeping
Sleeping
Ajout app.py – interface chat HelixaV2
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Charge ton modèle (witflag/HelixaV2)
|
| 6 |
+
model_id = "witflag/HelixaV2"
|
| 7 |
+
|
| 8 |
+
# On essaie de charger avec device_map pour utiliser GPU si disponible
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
model_id,
|
| 12 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 13 |
+
device_map="auto",
|
| 14 |
+
trust_remote_code=True, # souvent nécessaire pour DeepSeek
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
pipe = pipeline(
|
| 18 |
+
"text-generation",
|
| 19 |
+
model=model,
|
| 20 |
+
tokenizer=tokenizer,
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
def chat_with_model(message, history):
|
| 24 |
+
# Formatte l'historique pour DeepSeek (chat template)
|
| 25 |
+
messages = []
|
| 26 |
+
for user_msg, assistant_msg in history:
|
| 27 |
+
messages.append({"role": "user", "content": user_msg})
|
| 28 |
+
if assistant_msg:
|
| 29 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 30 |
+
messages.append({"role": "user", "content": message})
|
| 31 |
+
|
| 32 |
+
# Génération
|
| 33 |
+
output = pipe(
|
| 34 |
+
messages,
|
| 35 |
+
max_new_tokens=400,
|
| 36 |
+
do_sample=True,
|
| 37 |
+
temperature=0.7,
|
| 38 |
+
top_p=0.9,
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
return output[0]["generated_text"][-1]["content"] # on renvoie seulement la dernière réponse
|
| 42 |
+
|
| 43 |
+
# Interface Gradio avec historique de chat
|
| 44 |
+
demo = gr.ChatInterface(
|
| 45 |
+
fn=chat_with_model,
|
| 46 |
+
title="HelixaV2 – Ton modèle DeepSeek dupliqué",
|
| 47 |
+
description="Pose-moi toutes les questions que tu veux ! (60 requêtes/jour possible si tu espaces)",
|
| 48 |
+
examples=["Explique-moi la relativité en 3 phrases", "Écris un poème sur Casablanca", "Comment faire un tagine simple ?"],
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
demo.launch()
|