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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -14
app.py CHANGED
@@ -45,7 +45,6 @@ def generate_caption(image_base64, api_key):
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}")
@@ -72,23 +71,19 @@ if uploaded_image and API_KEY:
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:
 
45
  try:
46
  response = requests.post(api_url, json=payload, headers=headers)
47
  if response.status_code == 200:
 
48
  return response.json()
49
  else:
50
  st.error(f"API Error: {response.status_code}, {response.text}")
 
71
  st.write("Generating caption...")
72
  result = generate_caption(image_base64, API_KEY)
73
 
74
+ # Extract and display the generated caption
 
 
 
 
75
  if "error" in result:
76
  st.error(f"Error: {result['error']}")
77
  else:
 
78
  try:
79
+ # Extract the caption from the response
80
+ caption = (
81
+ result.get("choices", [{}])[0]
82
+ .get("message", {})
83
+ .get("content", "No caption generated.")
84
+ )
85
+ st.subheader("Generated Caption")
86
+ st.write(caption)
87
  except Exception as e:
88
  st.error(f"Error processing the response: {e}")
89
  else: