Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,16 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
# Charger le modèle
|
|
|
|
|
|
|
| 5 |
model = AutoModelForCausalLM.from_pretrained(
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
gpu_layers=20, # Ou ajusté selon ton environnement
|
| 10 |
-
hf=True # Indique que tu télécharges depuis Hugging Face
|
| 11 |
)
|
| 12 |
|
| 13 |
-
# Prompt système
|
| 14 |
system_prompt = """Tu es un compagnon d'écoute doux et bienveillant.
|
| 15 |
Ton rôle est d'aider la personne à clarifier ce qu'elle vit, sans juger, sans analyser.
|
| 16 |
|
|
@@ -24,21 +24,37 @@ Ne donne pas de conseils. Pose seulement une question douce à la fois si besoin
|
|
| 24 |
Parle en français naturel, sans anglicismes.
|
| 25 |
"""
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
prompt = system_prompt + "\n\n"
|
| 30 |
for user_msg, bot_msg in history:
|
| 31 |
-
prompt += f"Utilisateur: {user_msg}\nAssistant: {bot_msg}\n"
|
| 32 |
-
prompt += f"Utilisateur: {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
response = model(prompt, max_new_tokens=256, temperature=0.5)
|
| 35 |
return response
|
| 36 |
|
| 37 |
# Interface Gradio
|
| 38 |
iface = gr.ChatInterface(
|
| 39 |
chat,
|
| 40 |
-
title="Écoute Empathique (Phi-
|
| 41 |
-
description="Un compagnon d'écoute
|
| 42 |
)
|
| 43 |
|
| 44 |
-
iface
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 3 |
|
| 4 |
+
# Charger le modèle et le tokenizer
|
| 5 |
+
model_name = "microsoft/Phi-4-mini-instruct"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
model = AutoModelForCausalLM.from_pretrained(
|
| 8 |
+
model_name,
|
| 9 |
+
device_map="auto", # Utilise automatiquement CPU ou GPU
|
| 10 |
+
torch_dtype="auto", # Gère le float 16 ou 32 selon l'environnement
|
|
|
|
|
|
|
| 11 |
)
|
| 12 |
|
| 13 |
+
# Prompt système pour écoute empathique
|
| 14 |
system_prompt = """Tu es un compagnon d'écoute doux et bienveillant.
|
| 15 |
Ton rôle est d'aider la personne à clarifier ce qu'elle vit, sans juger, sans analyser.
|
| 16 |
|
|
|
|
| 24 |
Parle en français naturel, sans anglicismes.
|
| 25 |
"""
|
| 26 |
|
| 27 |
+
# Création d'un pipeline conversationnel
|
| 28 |
+
chat_pipeline = pipeline(
|
| 29 |
+
"text-generation",
|
| 30 |
+
model=model,
|
| 31 |
+
tokenizer=tokenizer,
|
| 32 |
+
max_new_tokens=256,
|
| 33 |
+
temperature=0.5,
|
| 34 |
+
top_p=0.9,
|
| 35 |
+
repetition_penalty=1.1,
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Fonction de chat
|
| 39 |
+
def chat(user_message, history):
|
| 40 |
prompt = system_prompt + "\n\n"
|
| 41 |
for user_msg, bot_msg in history:
|
| 42 |
+
prompt += f"Utilisateur : {user_msg}\nAssistant : {bot_msg}\n"
|
| 43 |
+
prompt += f"Utilisateur : {user_message}\nAssistant :"
|
| 44 |
+
|
| 45 |
+
response = chat_pipeline(prompt)[0]["generated_text"]
|
| 46 |
+
|
| 47 |
+
# Extraire uniquement la dernière réponse générée
|
| 48 |
+
if "Assistant :" in response:
|
| 49 |
+
response = response.split("Assistant :")[-1].strip()
|
| 50 |
|
|
|
|
| 51 |
return response
|
| 52 |
|
| 53 |
# Interface Gradio
|
| 54 |
iface = gr.ChatInterface(
|
| 55 |
chat,
|
| 56 |
+
title="Écoute Empathique (Phi-4 Mini)",
|
| 57 |
+
description="Un compagnon d'écoute structuré, doux, inspiré de la CNV.",
|
| 58 |
)
|
| 59 |
|
| 60 |
+
iface
|