Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import os
|
|
| 3 |
import io
|
| 4 |
from PIL import Image
|
| 5 |
import requests
|
| 6 |
-
import base64
|
| 7 |
import warnings
|
| 8 |
import gradio as gr
|
| 9 |
|
|
@@ -25,42 +24,38 @@ if not endpoint_url:
|
|
| 25 |
def generate_caption(image):
|
| 26 |
"""
|
| 27 |
Sends an image to the Hugging Face Inference Endpoint for caption generation.
|
| 28 |
-
|
| 29 |
:param image: An image in PIL format.
|
| 30 |
:return: Generated caption or error message.
|
| 31 |
"""
|
| 32 |
try:
|
| 33 |
headers = {"Authorization": f"Bearer {hf_api_key}"}
|
| 34 |
-
|
| 35 |
-
# Convert the image to RGB and
|
| 36 |
buffered = io.BytesIO()
|
| 37 |
-
image = image.convert("RGB")
|
| 38 |
image.save(buffered, format="JPEG")
|
| 39 |
buffered.seek(0)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
if response.status_code == 200:
|
| 50 |
-
# Handle response as a list or dictionary
|
| 51 |
if isinstance(response_data, list):
|
| 52 |
-
# Assuming the first item contains the generated text
|
| 53 |
return response_data[0].get("generated_text", "No caption generated.")
|
| 54 |
elif isinstance(response_data, dict):
|
| 55 |
return response_data.get("generated_text", "No caption generated.")
|
| 56 |
else:
|
| 57 |
return f"Unexpected response format: {response_data}"
|
| 58 |
else:
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
f"Error: {response.status_code} - {response.text}\n"
|
| 62 |
-
f"Headers: {headers}\nEndpoint: {endpoint_url}"
|
| 63 |
-
)
|
| 64 |
except Exception as e:
|
| 65 |
return f"An error occurred: {str(e)}"
|
| 66 |
|
|
@@ -91,13 +86,12 @@ demo = gr.Interface(
|
|
| 91 |
title="Image Captioning App",
|
| 92 |
description=(
|
| 93 |
"Upload an image or use one of the predefined samples to generate a caption. "
|
| 94 |
-
"This app uses a Hugging Face Inference Endpoint for the Salesforce/blip-image-captioning-
|
| 95 |
),
|
| 96 |
)
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
# Launch the Gradio demo
|
| 100 |
-
demo.launch()
|
| 101 |
-
|
| 102 |
|
| 103 |
|
|
|
|
| 3 |
import io
|
| 4 |
from PIL import Image
|
| 5 |
import requests
|
|
|
|
| 6 |
import warnings
|
| 7 |
import gradio as gr
|
| 8 |
|
|
|
|
| 24 |
def generate_caption(image):
|
| 25 |
"""
|
| 26 |
Sends an image to the Hugging Face Inference Endpoint for caption generation.
|
| 27 |
+
Sends raw image bytes (not base64).
|
| 28 |
:param image: An image in PIL format.
|
| 29 |
:return: Generated caption or error message.
|
| 30 |
"""
|
| 31 |
try:
|
| 32 |
headers = {"Authorization": f"Bearer {hf_api_key}"}
|
| 33 |
+
|
| 34 |
+
# Convert the image to RGB and save as JPEG into buffer
|
| 35 |
buffered = io.BytesIO()
|
| 36 |
+
image = image.convert("RGB")
|
| 37 |
image.save(buffered, format="JPEG")
|
| 38 |
buffered.seek(0)
|
| 39 |
+
|
| 40 |
+
# Send raw image bytes to the endpoint
|
| 41 |
+
response = requests.post(endpoint_url, headers=headers, data=buffered.getvalue())
|
| 42 |
+
|
| 43 |
+
# Try to parse JSON response safely
|
| 44 |
+
try:
|
| 45 |
+
response_data = response.json()
|
| 46 |
+
except ValueError:
|
| 47 |
+
return f"Invalid response (not JSON): {response.text}"
|
| 48 |
+
|
| 49 |
if response.status_code == 200:
|
|
|
|
| 50 |
if isinstance(response_data, list):
|
|
|
|
| 51 |
return response_data[0].get("generated_text", "No caption generated.")
|
| 52 |
elif isinstance(response_data, dict):
|
| 53 |
return response_data.get("generated_text", "No caption generated.")
|
| 54 |
else:
|
| 55 |
return f"Unexpected response format: {response_data}"
|
| 56 |
else:
|
| 57 |
+
return f"Error {response.status_code}: {response.text}"
|
| 58 |
+
|
|
|
|
|
|
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
return f"An error occurred: {str(e)}"
|
| 61 |
|
|
|
|
| 86 |
title="Image Captioning App",
|
| 87 |
description=(
|
| 88 |
"Upload an image or use one of the predefined samples to generate a caption. "
|
| 89 |
+
"This app uses a Hugging Face Inference Endpoint for the Salesforce/blip-image-captioning-large model."
|
| 90 |
),
|
| 91 |
)
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
| 94 |
# Launch the Gradio demo
|
| 95 |
+
demo.launch()
|
|
|
|
| 96 |
|
| 97 |
|