dlaima commited on
Commit
da984d3
·
verified ·
1 Parent(s): 5c9d73d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -2
app.py CHANGED
@@ -35,11 +35,23 @@ def get_completion(image, parameters=None, endpoint_url=endpoint_url):
35
 
36
  response = requests.post(endpoint_url, headers=headers, data=json.dumps(data))
37
 
38
- # Check for errors
39
  if response.status_code != 200:
40
  return {"error": response.text}
41
 
42
- return json.loads(response.content.decode("utf-8"))
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  # Helper function to download and process the image from a URL
45
  def caption_image(image_url):
 
35
 
36
  response = requests.post(endpoint_url, headers=headers, data=json.dumps(data))
37
 
 
38
  if response.status_code != 200:
39
  return {"error": response.text}
40
 
41
+ try:
42
+ # Try parsing the response as JSON
43
+ response_data = json.loads(response.content.decode("utf-8"))
44
+
45
+ # Check if it's a list and extract the first item
46
+ if isinstance(response_data, list) and len(response_data) > 0:
47
+ return response_data[0]
48
+ elif isinstance(response_data, dict):
49
+ return response_data
50
+ else:
51
+ return {"error": "Unexpected response format"}
52
+ except json.JSONDecodeError:
53
+ return {"error": "Failed to decode API response"}
54
+
55
 
56
  # Helper function to download and process the image from a URL
57
  def caption_image(image_url):