Add pneumonia detection app with Grad-CAM
Browse files
app.py
CHANGED
|
@@ -102,17 +102,21 @@ def query_medgemma(message, history, image=None):
|
|
| 102 |
# Multimodal format: Send image as base64 in the content
|
| 103 |
image_base64 = image_to_base64(image)
|
| 104 |
|
| 105 |
-
# TGI multimodal format for MedGemma
|
| 106 |
payload = {
|
| 107 |
"inputs": {
|
| 108 |
"text": message,
|
| 109 |
"image": image_base64
|
| 110 |
},
|
| 111 |
"parameters": {
|
| 112 |
-
"max_new_tokens":
|
| 113 |
-
"temperature": 0.
|
| 114 |
"do_sample": True,
|
| 115 |
-
"return_full_text": False
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
}
|
| 117 |
}
|
| 118 |
|
|
@@ -121,23 +125,30 @@ def query_medgemma(message, history, image=None):
|
|
| 121 |
"inputs": message,
|
| 122 |
"image": image_base64,
|
| 123 |
"parameters": {
|
| 124 |
-
"max_new_tokens":
|
| 125 |
-
"temperature": 0.
|
| 126 |
"do_sample": True,
|
| 127 |
-
"return_full_text": False
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
}
|
| 129 |
}
|
| 130 |
|
| 131 |
else:
|
| 132 |
-
# Text-only format
|
| 133 |
payload = {
|
| 134 |
"inputs": message,
|
| 135 |
"parameters": {
|
| 136 |
-
"max_new_tokens":
|
| 137 |
-
"temperature": 0.
|
| 138 |
"do_sample": True,
|
| 139 |
"return_full_text": False,
|
| 140 |
-
"stop": ["<|im_end|>", "</s>"]
|
|
|
|
|
|
|
|
|
|
| 141 |
}
|
| 142 |
}
|
| 143 |
payload_alt = None
|
|
@@ -187,15 +198,26 @@ def query_medgemma(message, history, image=None):
|
|
| 187 |
def medical_chat(message, history, uploaded_image):
|
| 188 |
"""Handle medical chat with context from pneumonia detection"""
|
| 189 |
|
| 190 |
-
#
|
| 191 |
-
context_message = message
|
| 192 |
if uploaded_image is not None:
|
| 193 |
-
context_message = f"""
|
| 194 |
-
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
response = query_medgemma(context_message, history, uploaded_image)
|
| 198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
# Add the exchange to history
|
| 200 |
history.append([message, response])
|
| 201 |
return history, ""
|
|
|
|
| 102 |
# Multimodal format: Send image as base64 in the content
|
| 103 |
image_base64 = image_to_base64(image)
|
| 104 |
|
| 105 |
+
# TGI multimodal format for MedGemma with better stopping
|
| 106 |
payload = {
|
| 107 |
"inputs": {
|
| 108 |
"text": message,
|
| 109 |
"image": image_base64
|
| 110 |
},
|
| 111 |
"parameters": {
|
| 112 |
+
"max_new_tokens": 200, # Reduced for faster response
|
| 113 |
+
"temperature": 0.3, # Lower temperature for more focused responses
|
| 114 |
"do_sample": True,
|
| 115 |
+
"return_full_text": False,
|
| 116 |
+
"stop": ["<|im_end|>", "</s>", "<|endoftext|>", "\n\n", "El personal", "Llamé", "Hice"],
|
| 117 |
+
"repetition_penalty": 1.2, # Prevent repetition
|
| 118 |
+
"top_p": 0.8, # Nucleus sampling
|
| 119 |
+
"top_k": 40 # Limit token choices
|
| 120 |
}
|
| 121 |
}
|
| 122 |
|
|
|
|
| 125 |
"inputs": message,
|
| 126 |
"image": image_base64,
|
| 127 |
"parameters": {
|
| 128 |
+
"max_new_tokens": 200,
|
| 129 |
+
"temperature": 0.3,
|
| 130 |
"do_sample": True,
|
| 131 |
+
"return_full_text": False,
|
| 132 |
+
"stop": ["<|im_end|>", "</s>", "<|endoftext|>", "\n\n"],
|
| 133 |
+
"repetition_penalty": 1.2,
|
| 134 |
+
"top_p": 0.8,
|
| 135 |
+
"top_k": 40
|
| 136 |
}
|
| 137 |
}
|
| 138 |
|
| 139 |
else:
|
| 140 |
+
# Text-only format with better parameters
|
| 141 |
payload = {
|
| 142 |
"inputs": message,
|
| 143 |
"parameters": {
|
| 144 |
+
"max_new_tokens": 200,
|
| 145 |
+
"temperature": 0.3,
|
| 146 |
"do_sample": True,
|
| 147 |
"return_full_text": False,
|
| 148 |
+
"stop": ["<|im_end|>", "</s>", "<|endoftext|>", "\n\n", "El personal", "Llamé", "Hice"],
|
| 149 |
+
"repetition_penalty": 1.2,
|
| 150 |
+
"top_p": 0.8,
|
| 151 |
+
"top_k": 40
|
| 152 |
}
|
| 153 |
}
|
| 154 |
payload_alt = None
|
|
|
|
| 198 |
def medical_chat(message, history, uploaded_image):
|
| 199 |
"""Handle medical chat with context from pneumonia detection"""
|
| 200 |
|
| 201 |
+
# Always add medical context to keep the model focused
|
|
|
|
| 202 |
if uploaded_image is not None:
|
| 203 |
+
context_message = f"""Eres MedGemma, un asistente médico especializado en radiología. Analiza esta radiografía de tórax y responde la siguiente pregunta médica de forma precisa y profesional:
|
| 204 |
+
|
| 205 |
+
Pregunta: {message}
|
| 206 |
+
|
| 207 |
+
Contexto: Esta es una radiografía de tórax para detectar neumonía. Enfócate en proporcionar información médica relevante, síntomas, diagnósticos o recomendaciones. Siempre menciona que se debe consultar a un profesional médico."""
|
| 208 |
+
else:
|
| 209 |
+
context_message = f"""Eres MedGemma, un asistente médico especializado. Responde la siguiente pregunta médica de forma precisa y profesional:
|
| 210 |
+
|
| 211 |
+
Pregunta: {message}
|
| 212 |
+
|
| 213 |
+
Instrucciones: Proporciona información médica precisa y relevante. Si no es una pregunta médica, redirige hacia temas de salud relacionados. Siempre menciona que se debe consultar a un profesional médico para diagnósticos definitivos."""
|
| 214 |
|
| 215 |
response = query_medgemma(context_message, history, uploaded_image)
|
| 216 |
|
| 217 |
+
# Clean the response if it contains non-medical content
|
| 218 |
+
if any(keyword in response.lower() for keyword in ["requests", "python", "código", "api", "import", "status code"]):
|
| 219 |
+
response = "Como asistente médico, me especializo en temas de salud y medicina. ¿Podrías hacer una pregunta relacionada con medicina, síntomas, diagnósticos o radiografías? Estoy aquí para ayudarte con consultas médicas."
|
| 220 |
+
|
| 221 |
# Add the exchange to history
|
| 222 |
history.append([message, response])
|
| 223 |
return history, ""
|