Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import os
|
|
| 3 |
import fitz # PyMuPDF
|
| 4 |
from groq import Groq
|
| 5 |
from langchain_groq import ChatGroq
|
|
|
|
| 6 |
|
| 7 |
RAID_WEIGHTS = {
|
| 8 |
"Dolor": 0.21,
|
|
@@ -65,11 +66,29 @@ def evaluate_raid(patient_text, clinical_record):
|
|
| 65 |
# Extraer JSON de la respuesta
|
| 66 |
start = response.content.find('{')
|
| 67 |
end = response.content.rfind('}') + 1
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# Validar estructura
|
| 71 |
if not all(key in json_response['raid_scores'] for key in RAID_WEIGHTS):
|
| 72 |
raise ValueError("Faltan componentes del RAID en la respuesta")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
return json_response
|
| 75 |
|
|
|
|
| 3 |
import fitz # PyMuPDF
|
| 4 |
from groq import Groq
|
| 5 |
from langchain_groq import ChatGroq
|
| 6 |
+
import json
|
| 7 |
|
| 8 |
RAID_WEIGHTS = {
|
| 9 |
"Dolor": 0.21,
|
|
|
|
| 66 |
# Extraer JSON de la respuesta
|
| 67 |
start = response.content.find('{')
|
| 68 |
end = response.content.rfind('}') + 1
|
| 69 |
+
try:
|
| 70 |
+
json_response = json.loads(response.content[start:end])
|
| 71 |
+
except json.JSONDecodeError:
|
| 72 |
+
# Si falla el parseo directo, intentar extraer substring
|
| 73 |
+
json_str = response.content.split('```json')[1].split('```')[0].strip()
|
| 74 |
+
json_response = json.loads(json_str)
|
| 75 |
|
| 76 |
# Validar estructura
|
| 77 |
if not all(key in json_response['raid_scores'] for key in RAID_WEIGHTS):
|
| 78 |
raise ValueError("Faltan componentes del RAID en la respuesta")
|
| 79 |
+
|
| 80 |
+
# Validaci贸n adicional
|
| 81 |
+
if not isinstance(json_response, dict) or 'raid_scores' not in json_response:
|
| 82 |
+
raise ValueError("Respuesta en formato incorrecto")
|
| 83 |
+
|
| 84 |
+
# C谩lculo de verificaci贸n
|
| 85 |
+
calculated_total = sum(
|
| 86 |
+
json_response['raid_scores'][k] * v
|
| 87 |
+
for k, v in RAID_WEIGHTS.items()
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# Redondear a 2 decimales para coincidir
|
| 91 |
+
json_response['raid_total'] = round(calculated_total, 2)
|
| 92 |
|
| 93 |
return json_response
|
| 94 |
|