| import gradio as gr |
| import requests |
| from PIL import Image |
| import os |
|
|
| |
| INFERENCE_ENDPOINT = "https://your-endpoint-url" |
| API_TOKEN = "your-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}" |
|
|
|
|
|
|
| |
|
|
| demo = gr.Interface( |
| fn=generate_caption, |
| inputs=gr.inputs.Image(type="file", label="Upload Image"), |
| outputs=gr.outputs.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() |
|
|