Shridhartd commited on
Commit
22c395c
·
verified ·
1 Parent(s): 4fd9f60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -4
app.py CHANGED
@@ -22,14 +22,21 @@ MODEL_URL = "https://api-inference.huggingface.co/models/Salesforce/blip-image-c
22
 
23
  def get_caption(image_path):
24
  """Sends image to Hugging Face API for captioning"""
25
- headers = {"Authorization": f"Bearer {HUGGINGFACE_API_KEY}"}
 
 
 
26
 
27
  with open(image_path, "rb") as image_file:
28
- files = {"image": image_file} # Change from "file" to "image"
29
- response = requests.post(MODEL_URL, headers=headers, files=files)
 
30
 
31
  if response.status_code == 200:
32
- return response.json()[0]['generated_text']
 
 
 
33
  else:
34
  return f"Error: {response.text}"
35
 
 
22
 
23
  def get_caption(image_path):
24
  """Sends image to Hugging Face API for captioning"""
25
+ headers = {
26
+ "Authorization": f"Bearer {HUGGINGFACE_API_KEY}",
27
+ "Content-Type": "application/octet-stream"
28
+ }
29
 
30
  with open(image_path, "rb") as image_file:
31
+ image_data = image_file.read()
32
+
33
+ response = requests.post(MODEL_URL, headers=headers, data=image_data)
34
 
35
  if response.status_code == 200:
36
+ try:
37
+ return response.json()[0]['generated_text']
38
+ except (IndexError, KeyError):
39
+ return f"Error: Unexpected response format: {response.text}"
40
  else:
41
  return f"Error: {response.text}"
42