Update app.py
Browse files
app.py
CHANGED
|
@@ -8,8 +8,7 @@ from datetime import datetime
|
|
| 8 |
|
| 9 |
MODEL_NAME = "google/gemma-2b-it"
|
| 10 |
INFO_FILE = "infos_medicaux.txt"
|
| 11 |
-
|
| 12 |
-
MAX_TOKENS = 1500
|
| 13 |
TEMPERATURE = 0.7
|
| 14 |
|
| 15 |
print("Chargement du modèle...")
|
|
@@ -23,24 +22,17 @@ print("Modèle chargé avec succès")
|
|
| 23 |
if os.path.exists(INFO_FILE):
|
| 24 |
with open(INFO_FILE, "r", encoding="utf-8") as f:
|
| 25 |
medical_context = f.read()
|
| 26 |
-
print(f"
|
| 27 |
else:
|
| 28 |
medical_context = ""
|
| 29 |
-
print("
|
|
|
|
| 30 |
|
| 31 |
-
# === INITIALISATION DU FICHIER CSV ===
|
| 32 |
-
if not os.path.exists(CSV_FILE):
|
| 33 |
-
with open(CSV_FILE, mode="w", newline="", encoding="utf-8") as file:
|
| 34 |
-
writer = csv.writer(file)
|
| 35 |
-
writer.writerow(["timestamp", "user_message", "assistant_response"])
|
| 36 |
-
|
| 37 |
-
# === FONCTION PRINCIPALE DU CHATBOT ===
|
| 38 |
@spaces.GPU
|
| 39 |
def chat_with_finanfa(message, history=None):
|
| 40 |
if history is None:
|
| 41 |
history = []
|
| 42 |
|
| 43 |
-
# Instructions système
|
| 44 |
system_prompt = (
|
| 45 |
"Tu es FINANFA, un assistant médical intelligent, empathique et précis. "
|
| 46 |
"Tu t'appuies sur les connaissances médicales fournies dans ton contexte. "
|
|
@@ -48,7 +40,6 @@ def chat_with_finanfa(message, history=None):
|
|
| 48 |
"Si une question n’est pas médicale, tu dis poliment que tu ne peux pas répondre."
|
| 49 |
)
|
| 50 |
|
| 51 |
-
# Construction du contexte conversationnel
|
| 52 |
conversation = f"Système : {system_prompt}\n"
|
| 53 |
if medical_context:
|
| 54 |
conversation += f"Connaissances médicales : {medical_context}\n"
|
|
@@ -56,7 +47,6 @@ def chat_with_finanfa(message, history=None):
|
|
| 56 |
conversation += f"Utilisateur : {user_msg}\nAssistant : {bot_msg}\n"
|
| 57 |
conversation += f"Utilisateur : {message}\nAssistant :"
|
| 58 |
|
| 59 |
-
# Génération de la réponse
|
| 60 |
inputs = tokenizer(conversation, return_tensors="pt", truncation=True)
|
| 61 |
outputs = model.generate(
|
| 62 |
**inputs,
|
|
@@ -69,21 +59,13 @@ def chat_with_finanfa(message, history=None):
|
|
| 69 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 70 |
response = response.split("Assistant :")[-1].strip()
|
| 71 |
|
| 72 |
-
# Sauvegarde dans le CSV
|
| 73 |
-
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 74 |
-
with open(CSV_FILE, mode="a", newline="", encoding="utf-8") as file:
|
| 75 |
-
writer = csv.writer(file)
|
| 76 |
-
writer.writerow([timestamp, message, response])
|
| 77 |
-
|
| 78 |
return response
|
| 79 |
|
| 80 |
-
# === INTERFACE GRADIO ===
|
| 81 |
with gr.Blocks(title="FINANFA — Chatbot Médical") as demo:
|
| 82 |
gr.Markdown(
|
| 83 |
"""
|
| 84 |
# 🏥 **FINANFA — Assistant Médical**
|
| 85 |
Cet assistant répond uniquement aux questions médicales
|
| 86 |
-
et utilise un fichier `.txt` pour enrichir ses réponses.
|
| 87 |
⚠️ Il ne remplace pas un avis médical professionnel.
|
| 88 |
"""
|
| 89 |
)
|
|
@@ -99,9 +81,4 @@ with gr.Blocks(title="FINANFA — Chatbot Médical") as demo:
|
|
| 99 |
]
|
| 100 |
)
|
| 101 |
|
| 102 |
-
# Bouton pour télécharger le CSV
|
| 103 |
-
with gr.Row():
|
| 104 |
-
gr.Markdown("### 📥 Télécharger les conversations")
|
| 105 |
-
gr.File(CSV_FILE, label="Conversations sauvegardées")
|
| 106 |
-
|
| 107 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 8 |
|
| 9 |
MODEL_NAME = "google/gemma-2b-it"
|
| 10 |
INFO_FILE = "infos_medicaux.txt"
|
| 11 |
+
MAX_TOKENS = 3500
|
|
|
|
| 12 |
TEMPERATURE = 0.7
|
| 13 |
|
| 14 |
print("Chargement du modèle...")
|
|
|
|
| 22 |
if os.path.exists(INFO_FILE):
|
| 23 |
with open(INFO_FILE, "r", encoding="utf-8") as f:
|
| 24 |
medical_context = f.read()
|
| 25 |
+
print(f"Contexte médical chargé depuis {INFO_FILE} ({len(medical_context)} caractères)")
|
| 26 |
else:
|
| 27 |
medical_context = ""
|
| 28 |
+
print("Aucun fichier infos_medicaux.txt trouvé. FINANFA fonctionnera sans contexte médical local.")
|
| 29 |
+
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
@spaces.GPU
|
| 32 |
def chat_with_finanfa(message, history=None):
|
| 33 |
if history is None:
|
| 34 |
history = []
|
| 35 |
|
|
|
|
| 36 |
system_prompt = (
|
| 37 |
"Tu es FINANFA, un assistant médical intelligent, empathique et précis. "
|
| 38 |
"Tu t'appuies sur les connaissances médicales fournies dans ton contexte. "
|
|
|
|
| 40 |
"Si une question n’est pas médicale, tu dis poliment que tu ne peux pas répondre."
|
| 41 |
)
|
| 42 |
|
|
|
|
| 43 |
conversation = f"Système : {system_prompt}\n"
|
| 44 |
if medical_context:
|
| 45 |
conversation += f"Connaissances médicales : {medical_context}\n"
|
|
|
|
| 47 |
conversation += f"Utilisateur : {user_msg}\nAssistant : {bot_msg}\n"
|
| 48 |
conversation += f"Utilisateur : {message}\nAssistant :"
|
| 49 |
|
|
|
|
| 50 |
inputs = tokenizer(conversation, return_tensors="pt", truncation=True)
|
| 51 |
outputs = model.generate(
|
| 52 |
**inputs,
|
|
|
|
| 59 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 60 |
response = response.split("Assistant :")[-1].strip()
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
return response
|
| 63 |
|
|
|
|
| 64 |
with gr.Blocks(title="FINANFA — Chatbot Médical") as demo:
|
| 65 |
gr.Markdown(
|
| 66 |
"""
|
| 67 |
# 🏥 **FINANFA — Assistant Médical**
|
| 68 |
Cet assistant répond uniquement aux questions médicales
|
|
|
|
| 69 |
⚠️ Il ne remplace pas un avis médical professionnel.
|
| 70 |
"""
|
| 71 |
)
|
|
|
|
| 81 |
]
|
| 82 |
)
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|