Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 = {
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
with open(image_path, "rb") as image_file:
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
if response.status_code == 200:
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
|