Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,3 @@
|
|
| 1 |
-
import threading
|
| 2 |
-
import os
|
| 3 |
-
from flask import Flask, request, jsonify
|
| 4 |
import gradio as gr
|
| 5 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 6 |
import torch
|
|
@@ -9,23 +6,23 @@ MODEL_NAME = "microsoft/phi-3-mini-4k-instruct"
|
|
| 9 |
MAX_TOKENS = 300
|
| 10 |
TEMPERATURE = 0.7
|
| 11 |
|
|
|
|
| 12 |
print("Chargement du modèle...")
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 14 |
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
MODEL_NAME,
|
| 16 |
-
torch_dtype=torch.float32,
|
| 17 |
)
|
| 18 |
-
print("Modèle chargé
|
| 19 |
|
|
|
|
| 20 |
def chat_with_phi3(message, history=None):
|
| 21 |
-
"""Conversation Gradio & Flask"""
|
| 22 |
if history is None:
|
| 23 |
history = []
|
| 24 |
|
| 25 |
system_prompt = (
|
| 26 |
-
"Tu es FINANFA, un assistant médical
|
| 27 |
-
"Tu réponds
|
| 28 |
-
"avec précision mais de manière compréhensible pour tous. "
|
| 29 |
"Si une question n’est pas médicale, dis poliment que tu ne peux pas répondre."
|
| 30 |
)
|
| 31 |
|
|
@@ -47,54 +44,46 @@ def chat_with_phi3(message, history=None):
|
|
| 47 |
response = response.split("Assistant :")[-1].strip()
|
| 48 |
return response
|
| 49 |
|
| 50 |
-
def gradio_chat(message, history):
|
| 51 |
-
return chat_with_phi3(message, history)
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
"Comment prévenir l’hypertension ?",
|
| 63 |
-
"Quelle est la cause de la fièvre typhoïde ?",
|
| 64 |
-
"Quels sont les effets secondaires du paracétamol ?",
|
| 65 |
-
],
|
| 66 |
-
)
|
| 67 |
|
| 68 |
-
app = Flask(__name__)
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
"
|
| 84 |
-
|
| 85 |
-
"
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
port = int(os.getenv("PORT", 5000))
|
| 95 |
-
app.run(host="0.0.0.0", port=port)
|
| 96 |
|
| 97 |
-
|
| 98 |
-
flask_thread = threading.Thread(target=launch_flask)
|
| 99 |
-
flask_thread.start()
|
| 100 |
-
launch_gradio()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
|
|
|
| 6 |
MAX_TOKENS = 300
|
| 7 |
TEMPERATURE = 0.7
|
| 8 |
|
| 9 |
+
# === Chargement du modèle ===
|
| 10 |
print("Chargement du modèle...")
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
MODEL_NAME,
|
| 14 |
+
torch_dtype=torch.float32, # pas de GPU
|
| 15 |
)
|
| 16 |
+
print("✅ Modèle chargé avec succès")
|
| 17 |
|
| 18 |
+
# === Fonction principale du chatbot ===
|
| 19 |
def chat_with_phi3(message, history=None):
|
|
|
|
| 20 |
if history is None:
|
| 21 |
history = []
|
| 22 |
|
| 23 |
system_prompt = (
|
| 24 |
+
"Tu es FINANFA, un assistant médical intelligent et empathique. "
|
| 25 |
+
"Tu ne réponds qu'aux questions liées à la santé, aux maladies ou aux médicaments. "
|
|
|
|
| 26 |
"Si une question n’est pas médicale, dis poliment que tu ne peux pas répondre."
|
| 27 |
)
|
| 28 |
|
|
|
|
| 44 |
response = response.split("Assistant :")[-1].strip()
|
| 45 |
return response
|
| 46 |
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# === Fonction API simulée (Flutter peut l’appeler via POST /api) ===
|
| 49 |
+
def api_endpoint(message: str, history_json: str = "[]"):
|
| 50 |
+
import json
|
| 51 |
+
try:
|
| 52 |
+
history = json.loads(history_json)
|
| 53 |
+
except Exception:
|
| 54 |
+
history = []
|
| 55 |
+
response = chat_with_phi3(message, history)
|
| 56 |
+
return {"response": response}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
|
|
|
| 58 |
|
| 59 |
+
# === Interface Gradio ===
|
| 60 |
+
with gr.Blocks(title="FINANFA — Chatbot Médical") as demo:
|
| 61 |
+
gr.Markdown(
|
| 62 |
+
"""
|
| 63 |
+
# 🏥 FINANFA — Assistant Médical
|
| 64 |
+
Cet assistant répond uniquement aux questions médicales.
|
| 65 |
+
⚠️ Il ne remplace pas un avis médical professionnel.
|
| 66 |
+
"""
|
| 67 |
+
)
|
| 68 |
|
| 69 |
+
chat = gr.ChatInterface(
|
| 70 |
+
fn=chat_with_phi3,
|
| 71 |
+
title="FINANFA - Assistant Médical",
|
| 72 |
+
description="Posez vos questions médicales en toute sécurité.",
|
| 73 |
+
examples=[
|
| 74 |
+
"Quels sont les symptômes du paludisme ?",
|
| 75 |
+
"Comment prévenir le diabète ?",
|
| 76 |
+
"Quels médicaments pour la fièvre ?"
|
| 77 |
+
]
|
| 78 |
+
)
|
| 79 |
|
| 80 |
+
with gr.Row():
|
| 81 |
+
gr.Markdown("### 🔗 API Flutter")
|
| 82 |
+
message_box = gr.Textbox(label="Message (POST /api)", placeholder="Votre texte ici...")
|
| 83 |
+
history_box = gr.Textbox(label="Historique JSON (optionnel)", value="[]")
|
| 84 |
+
api_button = gr.Button("Tester API /api (simulateur)")
|
| 85 |
+
api_output = gr.JSON(label="Réponse API")
|
| 86 |
|
| 87 |
+
api_button.click(api_endpoint, inputs=[message_box, history_box], outputs=api_output)
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|