File size: 1,711 Bytes
75fd3f8 d849e74 cdcdec7 d849e74 ffb68f6 75fd3f8 ffb68f6 b72051c cdcdec7 75fd3f8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import gradio as gr
import requests
from PIL import Image
import os
# Set your Inference Endpoint URL and API key
INFERENCE_ENDPOINT = "https://your-endpoint-url" # Replace with your endpoint URL
API_TOKEN = "your-api-token" # Replace with your Hugging Face API token
def generate_caption(image):
"""
Sends an image to the Hugging Face Inference Endpoint for caption generation.
:param image: An image in PIL format.
:return: Generated caption or error message.
"""
headers = {"Authorization": f"Bearer {API_TOKEN}"}
files = {"inputs": image}
response = requests.post(INFERENCE_ENDPOINT, headers=headers, files=files)
if response.status_code == 200:
return response.json().get("generated_text", "No caption generated.")
else:
return f"Error: {response.status_code} - {response.text}"
#Open the images
Image1=Image.open('https://huggingface.co/spaces/dlaima/Multiple_Image_captioning/resolve/main/image1.jpg')
Image2=Image.open('https://huggingface.co/spaces/dlaima/Multiple_Image_captioning/resolve/main/image2.jpeg')
Image3=Image.open('https://huggingface.co/spaces/dlaima/Multiple_Image_captioning/resolve/main/image3.jpeg')
# Gradio interface
demo = gr.Interface(
fn=generate_caption,
inputs=gr.Image(type="pil", label="Upload Image"),
outputs=gr.Textbox(label="Generated Caption"),
examples=[Image1, Image2, Image3],
title="Image Captioning App",
description=(
"Upload an image or use one of the predefined samples to generate a caption. "
"This app uses a Hugging Face Inference Endpoint for the `Salesforce/blip-image-captioning-base` model."
),
)
if __name__ == "__main__":
demo.launch()
|