Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app (1).py +117 -0
- requirements (2).txt +4 -0
app (1).py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import transformers
|
| 4 |
+
import torch
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Chargement des modèles
|
| 8 |
+
translator = transformers.pipeline("translation", model="facebook/nllb-200-distilled-600M")
|
| 9 |
+
text_gen_pipeline = transformers.pipeline(
|
| 10 |
+
"text-generation",
|
| 11 |
+
model="ContactDoctor/Bio-Medical-Llama-3-2-1B-CoT-012025",
|
| 12 |
+
torch_dtype=torch.bfloat16,
|
| 13 |
+
device_map="auto"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Message système initial
|
| 17 |
+
system_message = {
|
| 18 |
+
"role": "system",
|
| 19 |
+
"content": (
|
| 20 |
+
"You are a helpful, respectful, and knowledgeable medical assistant developed by the AI team at AfriAI Solutions, Senegal. "
|
| 21 |
+
"Provide brief, clear definitions when answering medical questions. After giving a concise response, ask the user if they would like more information about symptoms, causes, or treatments. "
|
| 22 |
+
"Always encourage users to consult healthcare professionals for personalized advice."
|
| 23 |
+
)
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
messages = [system_message]
|
| 27 |
+
max_history = 10
|
| 28 |
+
|
| 29 |
+
# Expressions reconnues
|
| 30 |
+
salutations = ["bonjour", "salut", "bonsoir", "coucou"]
|
| 31 |
+
remerciements = ["merci", "je vous remercie", "thanks"]
|
| 32 |
+
au_revoir = ["au revoir", "à bientôt", "bye", "bonne journée", "à la prochaine"]
|
| 33 |
+
|
| 34 |
+
def detect_smalltalk(user_input):
|
| 35 |
+
lower_input = user_input.lower().strip()
|
| 36 |
+
|
| 37 |
+
if any(phrase in lower_input for phrase in salutations):
|
| 38 |
+
return "Bonjour ! Comment puis-je vous aider aujourd'hui ?", True
|
| 39 |
+
if any(phrase in lower_input for phrase in remerciements):
|
| 40 |
+
return "Avec plaisir ! Souhaitez-vous poser une autre question médicale ?", True
|
| 41 |
+
if any(phrase in lower_input for phrase in au_revoir):
|
| 42 |
+
return "Au revoir ! Prenez soin de votre santé et n'hésitez pas à revenir si besoin.", True
|
| 43 |
+
|
| 44 |
+
return "", False
|
| 45 |
+
|
| 46 |
+
def medical_chatbot(user_input):
|
| 47 |
+
global messages
|
| 48 |
+
|
| 49 |
+
# Gestion des interactions sociales
|
| 50 |
+
smalltalk_response, handled = detect_smalltalk(user_input)
|
| 51 |
+
if handled:
|
| 52 |
+
return smalltalk_response
|
| 53 |
+
|
| 54 |
+
# Traduction français -> anglais
|
| 55 |
+
translated = translator(user_input, src_lang="fra_Latn", tgt_lang="eng_Latn")[0]['translation_text']
|
| 56 |
+
|
| 57 |
+
messages.append({"role": "user", "content": translated})
|
| 58 |
+
if len(messages) > max_history * 2:
|
| 59 |
+
messages = [system_message] + messages[-max_history * 2:]
|
| 60 |
+
|
| 61 |
+
# Préparation du prompt
|
| 62 |
+
prompt = text_gen_pipeline.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=False)
|
| 63 |
+
|
| 64 |
+
# Génération
|
| 65 |
+
response = text_gen_pipeline(
|
| 66 |
+
prompt,
|
| 67 |
+
max_new_tokens=512,
|
| 68 |
+
do_sample=True,
|
| 69 |
+
temperature=0.4,
|
| 70 |
+
top_k=150,
|
| 71 |
+
top_p=0.75,
|
| 72 |
+
eos_token_id=[
|
| 73 |
+
text_gen_pipeline.tokenizer.eos_token_id,
|
| 74 |
+
text_gen_pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
| 75 |
+
]
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
output = response[0]['generated_text'][len(prompt):].strip()
|
| 79 |
+
|
| 80 |
+
# Traduction anglais -> français
|
| 81 |
+
translated_back = translator(output, src_lang="eng_Latn", tgt_lang="fra_Latn")[0]['translation_text']
|
| 82 |
+
|
| 83 |
+
messages.append({"role": "assistant", "content": translated_back})
|
| 84 |
+
return translated_back
|
| 85 |
+
|
| 86 |
+
# Chargement du logo
|
| 87 |
+
logo = Image.open("AfriAI_Solutions.jpg")
|
| 88 |
+
|
| 89 |
+
# Interface Gradio
|
| 90 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo")) as demo:
|
| 91 |
+
with gr.Row():
|
| 92 |
+
gr.Image(value=logo, show_label=False, show_download_button=False, interactive=False, height=150)
|
| 93 |
+
|
| 94 |
+
gr.Markdown(
|
| 95 |
+
"""
|
| 96 |
+
# 🤖 Chatbot Médical AfriAI Solutions
|
| 97 |
+
**Posez votre question médicale en français.**
|
| 98 |
+
Le chatbot vous répondra brièvement et avec bienveillance, puis vous demandera si vous souhaitez plus de détails.
|
| 99 |
+
""",
|
| 100 |
+
elem_id="title"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
chatbot = gr.Chatbot(label="Chat avec le Médecin Virtuel", height=400)
|
| 104 |
+
msg = gr.Textbox(label="Votre question", placeholder="Exemple : Quels sont les symptômes du paludisme ?")
|
| 105 |
+
|
| 106 |
+
clear = gr.Button("Effacer la conversation", variant="secondary")
|
| 107 |
+
|
| 108 |
+
def respond(message, history):
|
| 109 |
+
response = medical_chatbot(message)
|
| 110 |
+
history = history or []
|
| 111 |
+
history.append((message, response))
|
| 112 |
+
return "", history
|
| 113 |
+
|
| 114 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 115 |
+
clear.click(lambda: ("", []), None, [msg, chatbot])
|
| 116 |
+
|
| 117 |
+
demo.launch()
|
requirements (2).txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
transformers>=4.38.0
|
| 3 |
+
torch>=2.1.0
|
| 4 |
+
pillow>=9.0.0
|