Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,13 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
import os
|
| 5 |
-
import
|
|
|
|
|
|
|
| 6 |
# === CONFIGURATION ===
|
| 7 |
-
MODEL_NAME = "facebook/MobileLLM-Pro"
|
| 8 |
INFO_FILE = "infos_medicaux.txt"
|
|
|
|
| 9 |
MAX_TOKENS = 800
|
| 10 |
TEMPERATURE = 0.6
|
| 11 |
|
|
@@ -16,6 +19,7 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 16 |
MODEL_NAME,
|
| 17 |
torch_dtype=torch.float32
|
| 18 |
)
|
|
|
|
| 19 |
print("✅ Modèle chargé avec succès")
|
| 20 |
|
| 21 |
# === CHARGEMENT DU CONTEXTE MÉDICAL ===
|
|
@@ -27,7 +31,16 @@ else:
|
|
| 27 |
medical_context = ""
|
| 28 |
print("⚠️ Aucun fichier infos_medicaux.txt trouvé.")
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def chat_with_finanfa(message, history=None):
|
| 32 |
if history is None:
|
| 33 |
history = []
|
|
@@ -48,7 +61,7 @@ def chat_with_finanfa(message, history=None):
|
|
| 48 |
conversation += f"Utilisateur : {user_msg}\nAssistant : {bot_msg}\n"
|
| 49 |
conversation += f"Utilisateur : {message}\nAssistant :"
|
| 50 |
|
| 51 |
-
# Génération
|
| 52 |
inputs = tokenizer(conversation, return_tensors="pt", truncation=True)
|
| 53 |
with torch.no_grad():
|
| 54 |
outputs = model.generate(
|
|
@@ -61,6 +74,10 @@ def chat_with_finanfa(message, history=None):
|
|
| 61 |
|
| 62 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 63 |
response = response.split("Assistant :")[-1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
return response
|
| 65 |
|
| 66 |
# === INTERFACE GRADIO ===
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
import os
|
| 5 |
+
import csv
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
# === CONFIGURATION ===
|
| 9 |
+
MODEL_NAME = "facebook/MobileLLM-Pro" # ✅ Léger et rapide sur CPU
|
| 10 |
INFO_FILE = "infos_medicaux.txt"
|
| 11 |
+
LOG_FILE = "historique_questions.csv"
|
| 12 |
MAX_TOKENS = 800
|
| 13 |
TEMPERATURE = 0.6
|
| 14 |
|
|
|
|
| 19 |
MODEL_NAME,
|
| 20 |
torch_dtype=torch.float32
|
| 21 |
)
|
| 22 |
+
model.eval()
|
| 23 |
print("✅ Modèle chargé avec succès")
|
| 24 |
|
| 25 |
# === CHARGEMENT DU CONTEXTE MÉDICAL ===
|
|
|
|
| 31 |
medical_context = ""
|
| 32 |
print("⚠️ Aucun fichier infos_medicaux.txt trouvé.")
|
| 33 |
|
| 34 |
+
# === FONCTION DE LOG DES QUESTIONS ===
|
| 35 |
+
def log_question(user_message, response):
|
| 36 |
+
file_exists = os.path.exists(LOG_FILE)
|
| 37 |
+
with open(LOG_FILE, "a", encoding="utf-8", newline="") as csvfile:
|
| 38 |
+
writer = csv.writer(csvfile)
|
| 39 |
+
if not file_exists:
|
| 40 |
+
writer.writerow(["Timestamp", "Question", "Réponse"])
|
| 41 |
+
writer.writerow([datetime.now().isoformat(), user_message, response])
|
| 42 |
+
|
| 43 |
+
# === FONCTION DE CHAT ===
|
| 44 |
def chat_with_finanfa(message, history=None):
|
| 45 |
if history is None:
|
| 46 |
history = []
|
|
|
|
| 61 |
conversation += f"Utilisateur : {user_msg}\nAssistant : {bot_msg}\n"
|
| 62 |
conversation += f"Utilisateur : {message}\nAssistant :"
|
| 63 |
|
| 64 |
+
# Génération de la réponse
|
| 65 |
inputs = tokenizer(conversation, return_tensors="pt", truncation=True)
|
| 66 |
with torch.no_grad():
|
| 67 |
outputs = model.generate(
|
|
|
|
| 74 |
|
| 75 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 76 |
response = response.split("Assistant :")[-1].strip()
|
| 77 |
+
|
| 78 |
+
# Enregistrement automatique dans le CSV
|
| 79 |
+
log_question(message, response)
|
| 80 |
+
|
| 81 |
return response
|
| 82 |
|
| 83 |
# === INTERFACE GRADIO ===
|