ahmedmbutt
commited on
Commit
•
2dda520
1
Parent(s):
92c48c7
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,36 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceApi
|
3 |
+
import requests
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
|
7 |
+
# Initialize the Hugging Face Inference API
|
8 |
+
model_id = "Salesforce/blip-image-captioning-large"
|
9 |
+
api = InferenceApi(repo_id=model_id)
|
10 |
+
|
11 |
+
def caption_image(image):
|
12 |
+
# Convert the PIL image to bytes
|
13 |
+
buffered = io.BytesIO()
|
14 |
+
image.save(buffered, format="JPEG")
|
15 |
+
img_bytes = buffered.getvalue()
|
16 |
+
|
17 |
+
# Call the Hugging Face inference API
|
18 |
+
response = api(inputs=img_bytes)
|
19 |
+
|
20 |
+
# Check the response and format it properly
|
21 |
+
if isinstance(response, list) and response:
|
22 |
+
return response[0]["generated_text"]
|
23 |
+
else:
|
24 |
+
return "Error generating caption"
|
25 |
+
|
26 |
+
# Set up the Gradio interface
|
27 |
+
interface = gr.Interface(
|
28 |
+
fn=caption_image,
|
29 |
+
inputs=gr.inputs.Image(type="pil"),
|
30 |
+
outputs="text",
|
31 |
+
title="Image Captioning",
|
32 |
+
description="Generate captions for images using the BLIP model."
|
33 |
+
)
|
34 |
+
|
35 |
+
# Launch the interface
|
36 |
+
interface.launch()
|