Spaces:
Runtime error
Runtime error
File size: 9,736 Bytes
16d9792 818b538 ef357c3 a51fd05 ef357c3 a51fd05 ef357c3 a51fd05 818b538 16d9792 ed692a3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
import gradio as gr
import transformers
import torch
from transformers import pipeline
from datetime import datetime
import os
from huggingface_hub import login
# Get token from environment variable
hf_token = os.getenv("hf_token")
if hf_token is None:
raise ValueError("Please set HF_TOKEN environment variable with your Hugging Face token")
# Login with the token
login(token=hf_token)
# Configuration des modèles
def load_models():
# Modèle médical
medical_pipe = transformers.pipeline(
"text-generation",
model="ContactDoctor/Bio-Medical-Llama-3-2-1B-CoT-012025",
torch_dtype=torch.bfloat16,
device_map="auto",
)
# Modèle de traduction
translator = pipeline("translation", model="facebook/nllb-200-distilled-600M")
return medical_pipe, translator
medical_pipe, translator = load_models()
# Message système pour guider le chatbot
system_message = {
"role": "system",
"content": (
"You are a helpful, respectful, and knowledgeable medical assistant developed by the AI team at AfriAI Solutions, Senegal. "
"Provide brief, clear definitions when answering medical questions. After giving a concise response, ask the user if they would like more information about specific aspects such as symptoms, causes, or treatments. "
"If the user declines further information, respond briefly and ask if there's anything else they need help with."
"Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. "
"Ensure your responses are socially unbiased and positive. If a question does not make sense, explain why instead of providing incorrect information. "
"If you don't know the answer to a question, avoid sharing false information. "
"Encourage users to consult healthcare professionals for accurate diagnoses and personalized advice."
),
}
max_history = 10 # Limite de l'historique de conversation
# Traduction français -> anglais
def translate_fr_to_en(text):
translated = translator(text, src_lang="fra_Latn", tgt_lang="eng_Latn")
return translated[0]['translation_text']
# Traduction anglais -> français
def translate_en_to_fr(text):
translated = translator(text, src_lang="eng_Latn", tgt_lang="fra_Latn")
return translated[0]['translation_text']
# Gestion des salutations et phrases de fermeture
def handle_special_phrases(user_input):
user_input_lower = user_input.lower()
# Salutations
greetings = ["bonjour", "salut", "hello", "hi", "coucou", "bonsoir"]
if any(greet in user_input_lower for greet in greetings):
now = datetime.now()
if 5 <= now.hour < 12:
return "Bonjour ! Je suis l'assistant médical AfriAI. Comment puis-je vous aider aujourd'hui ?"
elif 12 <= now.hour < 18:
return "Bon après-midi ! Je suis l'assistant médical AfriAI. En quoi puis-je vous aider ?"
else:
return "Bonsoir ! Je suis l'assistant médical AfriAI. Comment puis-je vous aider ce soir ?"
# Remerciements
thanks = ["merci", "thank you", "thanks", "je vous remercie"]
if any(thank in user_input_lower for thank in thanks):
return "Je vous en prie ! N'hésitez pas si vous avez d'autres questions médicales."
# Fermeture
goodbyes = ["au revoir", "bye", "goodbye", "à plus", "à bientôt"]
if any(goodbye in user_input_lower for goodbye in goodbyes):
return "Au revoir ! Prenez soin de vous. N'hésitez pas à revenir si vous avez d'autres questions."
return None
# Génération de réponse médicale
def generate_medical_response(messages):
# Création du prompt
prompt = medical_pipe.tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=False
)
# Tokens d'arrêt
stop_tokens = [
medical_pipe.tokenizer.eos_token_id,
medical_pipe.tokenizer.convert_tokens_to_ids("<|eot_id|>"),
]
# Génération de la réponse
outputs = medical_pipe(
prompt,
max_new_tokens=512,
eos_token_id=stop_tokens,
do_sample=True,
temperature=0.4,
top_k=150,
top_p=0.75,
)
return outputs[0]["generated_text"][len(prompt):].strip()
# Fonction principale du chatbot
def chat_with_medical_bot(user_input, chat_history):
# Vérifier les phrases spéciales (salutations, remerciements, etc.)
special_response = handle_special_phrases(user_input)
if special_response:
chat_history.append((user_input, special_response))
return "", chat_history
# Traduire l'entrée utilisateur en anglais
try:
user_input_en = translate_fr_to_en(user_input)
except:
user_input_en = user_input # En cas d'échec, utiliser l'original
# Initialiser l'historique des messages si vide
if not hasattr(chat_with_medical_bot, "messages"):
chat_with_medical_bot.messages = [system_message]
# Ajouter le message utilisateur
chat_with_medical_bot.messages.append({"role": "user", "content": user_input_en})
# Limiter l'historique
if len(chat_with_medical_bot.messages) > max_history * 2:
chat_with_medical_bot.messages = [system_message] + chat_with_medical_bot.messages[-max_history * 2:]
# Générer la réponse médicale
try:
response_en = generate_medical_response(chat_with_medical_bot.messages)
# Traduire la réponse en français
try:
response_fr = translate_en_to_fr(response_en)
except:
response_fr = response_en # En cas d'échec, utiliser l'original
except Exception as e:
response_fr = f"Désolé, une erreur s'est produite : {str(e)}"
# Ajouter la réponse à l'historique des messages
chat_with_medical_bot.messages.append({"role": "assistant", "content": response_en})
# Ajouter à l'historique de chat Gradio
chat_history.append((user_input, response_fr))
return "", chat_history
# CSS personnalisé pour le thème AfriAI Solutions
custom_css = """
:root {
--primary-color: #2C5F2D;
--secondary-color: #97BC62;
--accent-color: #E0E0E0;
--text-color: #333333;
}
.gradio-container {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
#chatbot {
background-color: white;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
height: 500px;
}
.user, .bot {
padding: 12px 16px;
border-radius: 18px;
margin: 8px 0;
line-height: 1.4;
max-width: 80%;
}
.user {
background-color: var(--secondary-color);
color: white;
margin-left: auto;
border-bottom-right-radius: 4px;
}
.bot {
background-color: var(--accent-color);
color: var(--text-color);
margin-right: auto;
border-bottom-left-radius: 4px;
}
#header {
background-color: var(--primary-color);
color: white;
padding: 20px;
border-radius: 10px 10px 0 0;
text-align: center;
}
#header img {
max-width: 150px;
margin-bottom: 10px;
}
#header h1 {
margin: 0;
font-size: 24px;
}
#header p {
margin: 5px 0 0;
font-size: 16px;
opacity: 0.9;
}
footer {
text-align: center;
padding: 10px;
font-size: 12px;
color: var(--text-color);
opacity: 0.7;
}
#component-0 {
border-radius: 0 0 10px 10px !important;
}
#component-1 {
border-radius: 0 0 10px 10px !important;
}
"""
# Interface Gradio
with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as demo:
# En-tête avec logo et description
with gr.Column(elem_id="header"):
gr.Markdown("""
<div style="text-align: center;">
<img src="https://via.placeholder.com/150x50/2C5F2D/FFFFFF?text=AfriAI+Solutions" alt="AfriAI Solutions Logo">
<h1>Assistant Médical AfriAI</h1>
<p>Transformez vos questions médicales en solutions claires et précises</p>
</div>
""")
# Zone de chat
chatbot = gr.Chatbot(
label="Conversation",
elem_id="chatbot",
bubble_full_width=False,
avatar_images=(
"https://via.placeholder.com/40/97BC62/FFFFFF?text=VOUS",
"https://via.placeholder.com/40/2C5F2D/FFFFFF?text=AI"
)
)
# Zone de saisie
msg = gr.Textbox(
label="Posez votre question médicale en français",
placeholder="Bonjour, quels sont les symptômes du diabète ?",
container=False
)
# Boutons
with gr.Row():
clear = gr.ClearButton([msg, chatbot], value="Effacer la conversation")
submit = gr.Button("Envoyer", variant="primary")
# Événements
msg.submit(chat_with_medical_bot, [msg, chatbot], [msg, chatbot])
submit.click(chat_with_medical_bot, [msg, chatbot], [msg, chatbot])
# Exemples
gr.Examples(
examples=[
"Bonjour, quels sont les symptômes du diabète ?",
"Comment traiter une migraine ?",
"Expliquez-moi ce qu'est l'hypertension artérielle",
"Quelles sont les causes de la toux persistante ?"
],
inputs=msg,
label="Exemples de questions"
)
# Pied de page
gr.Markdown("""
<div style="text-align: center; font-size: 12px; color: #666; margin-top: 20px;">
<p>Cet assistant médical ne remplace pas un avis médical professionnel. Consultez toujours un médecin pour des problèmes de santé sérieux.</p>
<p>© 2024 AfriAI Solutions - Sénégal</p>
</div>
""", elem_id="footer")
# Démarrer l'application
if __name__ == "__main__":
demo.launch()
|