abjasrees commited on
Commit
675cb5f
·
verified ·
1 Parent(s): 962ac80

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -27
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
- # Remove any leading/trailing quotes if the entire thing is quoted
80
- if cleaned.startswith('"') and cleaned.endswith('"'):
81
- cleaned = cleaned[1:-1]
 
 
 
 
 
 
 
 
 
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"Cleaned response: {repr(cleaned)}")
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
- # Get initial criteria with better error handling
206
- raw_criteria = _CURRENT_ENGINE.get_hedis_criteria_with_feedback()
207
-
208
- # Clean the criteria if needed
209
- if isinstance(raw_criteria, dict) and "raw_output" in raw_criteria:
210
- # Try to clean and re-parse if it's raw output
211
- _CURRENT_CRITERIA = clean_json_response(raw_criteria["raw_output"])
212
- else:
213
- _CURRENT_CRITERIA = raw_criteria
214
-
215
- _ANALYSIS_STATE = "criteria_review"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- # Get modified criteria with better error handling
296
- raw_criteria = _CURRENT_ENGINE.get_hedis_criteria_with_feedback(suggestions.strip())
297
-
298
- # Clean the criteria if needed
299
- if isinstance(raw_criteria, dict) and "raw_output" in raw_criteria:
300
- # Try to clean and re-parse if it's raw output
301
- _CURRENT_CRITERIA = clean_json_response(raw_criteria["raw_output"])
302
- else:
303
- _CURRENT_CRITERIA = raw_criteria
304
-
305
- criteria_display = format_criteria_for_display(_CURRENT_CRITERIA)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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}",