adisaljusi commited on
Commit
c981ebe
·
1 Parent(s): 816b68d

Fix JSON parsing in OpenAI response handling

Browse files
Files changed (1) hide show
  1. app.py +5 -2
app.py CHANGED
@@ -71,8 +71,11 @@ def classify_with_openai(image_path):
71
  )
72
 
73
  try:
74
- parsed_response = json.loads(response.output_text)
75
- except json.JSONDecodeError:
 
 
 
76
  parsed_response = {
77
  "raw_response": response.output_text,
78
  "warning": "OpenAI response was not valid JSON.",
 
71
  )
72
 
73
  try:
74
+ text = response.output_text.strip()
75
+ if text.startswith("```"):
76
+ text = text.split("\n", 1)[1].rsplit("```", 1)[0].strip()
77
+ parsed_response = json.loads(text)
78
+ except (json.JSONDecodeError, IndexError):
79
  parsed_response = {
80
  "raw_response": response.output_text,
81
  "warning": "OpenAI response was not valid JSON.",