Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -60,6 +60,9 @@ def _get_vectordb_from_bytes(pdf_bytes: bytes):
|
|
| 60 |
|
| 61 |
def clean_json_response(response_text: str) -> dict:
|
| 62 |
"""Clean and parse JSON response from agents."""
|
|
|
|
|
|
|
|
|
|
| 63 |
try:
|
| 64 |
# Clean the response string
|
| 65 |
cleaned = str(response_text).strip()
|
|
@@ -76,16 +79,26 @@ def clean_json_response(response_text: str) -> dict:
|
|
| 76 |
# Remove any leading/trailing whitespace and newlines
|
| 77 |
cleaned = cleaned.strip()
|
| 78 |
|
| 79 |
-
#
|
| 80 |
-
if cleaned.startswith('"') and cleaned.endswith('"'):
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
# Try to parse as JSON
|
| 84 |
return json.loads(cleaned)
|
|
|
|
| 85 |
except json.JSONDecodeError as e:
|
| 86 |
print(f"JSON parsing error: {e}")
|
| 87 |
-
print(f"
|
| 88 |
-
return {"raw_output": str(response_text), "parse_error": str(e)}
|
| 89 |
"""Format criteria dictionary into markdown for display."""
|
| 90 |
if "raw_output" in criteria:
|
| 91 |
return f"```\n{criteria['raw_output']}\n```"
|
|
@@ -202,17 +215,32 @@ def start_hedis_analysis(pdf_file, measure_name, measurement_year):
|
|
| 202 |
# Create engine and get initial criteria
|
| 203 |
_CURRENT_ENGINE = InteractiveHedisComplianceEngine(vectordb, measure_name, int(measurement_year))
|
| 204 |
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
criteria_display = format_criteria_for_display(_CURRENT_CRITERIA)
|
| 218 |
|
|
@@ -292,17 +320,32 @@ def modify_criteria(suggestions):
|
|
| 292 |
gr.update(visible=True)
|
| 293 |
)
|
| 294 |
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
return (
|
| 308 |
f"## 🔄 Modified Criteria\n\n{criteria_display}",
|
|
|
|
| 60 |
|
| 61 |
def clean_json_response(response_text: str) -> dict:
|
| 62 |
"""Clean and parse JSON response from agents."""
|
| 63 |
+
if not response_text:
|
| 64 |
+
return {"raw_output": "Empty response", "parse_error": "No content received"}
|
| 65 |
+
|
| 66 |
try:
|
| 67 |
# Clean the response string
|
| 68 |
cleaned = str(response_text).strip()
|
|
|
|
| 79 |
# Remove any leading/trailing whitespace and newlines
|
| 80 |
cleaned = cleaned.strip()
|
| 81 |
|
| 82 |
+
# Handle quoted JSON strings
|
| 83 |
+
if len(cleaned) >= 2 and cleaned.startswith('"') and cleaned.endswith('"'):
|
| 84 |
+
try:
|
| 85 |
+
# Try to unquote and parse
|
| 86 |
+
unquoted = cleaned[1:-1]
|
| 87 |
+
# Handle common escape sequences
|
| 88 |
+
unquoted = unquoted.replace('\\"', '"').replace('\\n', '\n').replace('\\t', '\t')
|
| 89 |
+
test_parse = json.loads(unquoted)
|
| 90 |
+
cleaned = unquoted
|
| 91 |
+
except:
|
| 92 |
+
# If unquoting fails, keep original
|
| 93 |
+
pass
|
| 94 |
|
| 95 |
# Try to parse as JSON
|
| 96 |
return json.loads(cleaned)
|
| 97 |
+
|
| 98 |
except json.JSONDecodeError as e:
|
| 99 |
print(f"JSON parsing error: {e}")
|
| 100 |
+
print(f"Original response: {repr(response_text)}")
|
| 101 |
+
return {"raw_output": str(response_text), "parse_error": f"JSON Parse Error: {str(e)}"}
|
| 102 |
"""Format criteria dictionary into markdown for display."""
|
| 103 |
if "raw_output" in criteria:
|
| 104 |
return f"```\n{criteria['raw_output']}\n```"
|
|
|
|
| 215 |
# Create engine and get initial criteria
|
| 216 |
_CURRENT_ENGINE = InteractiveHedisComplianceEngine(vectordb, measure_name, int(measurement_year))
|
| 217 |
|
| 218 |
+
try:
|
| 219 |
+
# Get initial criteria with better error handling
|
| 220 |
+
raw_criteria = _CURRENT_ENGINE.get_hedis_criteria_with_feedback()
|
| 221 |
+
|
| 222 |
+
# Handle different response types
|
| 223 |
+
if isinstance(raw_criteria, dict):
|
| 224 |
+
if "raw_output" in raw_criteria:
|
| 225 |
+
# Try to clean and re-parse if it's raw output
|
| 226 |
+
_CURRENT_CRITERIA = clean_json_response(raw_criteria["raw_output"])
|
| 227 |
+
else:
|
| 228 |
+
_CURRENT_CRITERIA = raw_criteria
|
| 229 |
+
else:
|
| 230 |
+
# If it's not a dict, treat as raw output
|
| 231 |
+
_CURRENT_CRITERIA = clean_json_response(str(raw_criteria))
|
| 232 |
+
|
| 233 |
+
_ANALYSIS_STATE = "criteria_review"
|
| 234 |
+
|
| 235 |
+
except Exception as e:
|
| 236 |
+
print(f"Error getting criteria: {e}")
|
| 237 |
+
return (
|
| 238 |
+
f"❌ Error getting HEDIS criteria: {str(e)}\n\nPlease try again or check the measure code.",
|
| 239 |
+
gr.update(visible=False),
|
| 240 |
+
gr.update(visible=False),
|
| 241 |
+
gr.update(visible=False),
|
| 242 |
+
gr.update(visible=False)
|
| 243 |
+
)
|
| 244 |
|
| 245 |
criteria_display = format_criteria_for_display(_CURRENT_CRITERIA)
|
| 246 |
|
|
|
|
| 320 |
gr.update(visible=True)
|
| 321 |
)
|
| 322 |
|
| 323 |
+
try:
|
| 324 |
+
# Get modified criteria with better error handling
|
| 325 |
+
raw_criteria = _CURRENT_ENGINE.get_hedis_criteria_with_feedback(suggestions.strip())
|
| 326 |
+
|
| 327 |
+
# Handle different response types
|
| 328 |
+
if isinstance(raw_criteria, dict):
|
| 329 |
+
if "raw_output" in raw_criteria:
|
| 330 |
+
# Try to clean and re-parse if it's raw output
|
| 331 |
+
_CURRENT_CRITERIA = clean_json_response(raw_criteria["raw_output"])
|
| 332 |
+
else:
|
| 333 |
+
_CURRENT_CRITERIA = raw_criteria
|
| 334 |
+
else:
|
| 335 |
+
# If it's not a dict, treat as raw output
|
| 336 |
+
_CURRENT_CRITERIA = clean_json_response(str(raw_criteria))
|
| 337 |
+
|
| 338 |
+
criteria_display = format_criteria_for_display(_CURRENT_CRITERIA)
|
| 339 |
+
|
| 340 |
+
except Exception as e:
|
| 341 |
+
print(f"Error modifying criteria: {e}")
|
| 342 |
+
return (
|
| 343 |
+
f"❌ Error modifying criteria: {str(e)}\n\nPlease try again with different suggestions.",
|
| 344 |
+
gr.update(visible=True, value=""),
|
| 345 |
+
gr.update(visible=True),
|
| 346 |
+
gr.update(visible=True),
|
| 347 |
+
gr.update(visible=True)
|
| 348 |
+
)
|
| 349 |
|
| 350 |
return (
|
| 351 |
f"## 🔄 Modified Criteria\n\n{criteria_display}",
|