mrbeliever commited on
Commit
99dbe33
1 Parent(s): fc4d401

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -8
app.py CHANGED
@@ -42,11 +42,17 @@ def generate_caption(image_base64, api_key):
42
  "temperature": 0,
43
  }
44
 
45
- response = requests.post(api_url, json=payload, headers=headers)
46
- if response.status_code == 200:
47
- return response.json()
48
- else:
49
- return {"error": response.text}
 
 
 
 
 
 
50
 
51
  # File uploader for image
52
  uploaded_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
@@ -66,13 +72,25 @@ if uploaded_image and API_KEY:
66
  st.write("Generating caption...")
67
  result = generate_caption(image_base64, API_KEY)
68
 
 
 
 
 
69
  # Display the result
70
  if "error" in result:
71
  st.error(f"Error: {result['error']}")
72
  else:
73
- caption = result.get("messages", [{}])[-1].get("content", [{}])[0].get("text", "No caption generated.")
74
- st.subheader("Generated Caption")
75
- st.write(caption)
 
 
 
 
 
 
 
 
76
  else:
77
  if not API_KEY:
78
  st.warning("Please set the `NEBIUS_API_KEY` environment variable.")
 
42
  "temperature": 0,
43
  }
44
 
45
+ try:
46
+ response = requests.post(api_url, json=payload, headers=headers)
47
+ if response.status_code == 200:
48
+ st.write("API response received successfully.") # Debugging message
49
+ return response.json()
50
+ else:
51
+ st.error(f"API Error: {response.status_code}, {response.text}")
52
+ return {"error": response.text}
53
+ except Exception as e:
54
+ st.error(f"An exception occurred: {e}")
55
+ return {"error": str(e)}
56
 
57
  # File uploader for image
58
  uploaded_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
 
72
  st.write("Generating caption...")
73
  result = generate_caption(image_base64, API_KEY)
74
 
75
+ # Debug: Display the full API response
76
+ st.write("Full API Response:")
77
+ st.json(result)
78
+
79
  # Display the result
80
  if "error" in result:
81
  st.error(f"Error: {result['error']}")
82
  else:
83
+ # Extract caption from the response
84
+ try:
85
+ # Safely navigate the response to extract the caption
86
+ messages = result.get("choices", [{}])[0].get("message", {}).get("content", "")
87
+ if messages:
88
+ st.subheader("Generated Caption")
89
+ st.write(messages)
90
+ else:
91
+ st.error("No caption generated. The response structure might have changed.")
92
+ except Exception as e:
93
+ st.error(f"Error processing the response: {e}")
94
  else:
95
  if not API_KEY:
96
  st.warning("Please set the `NEBIUS_API_KEY` environment variable.")