| | |
| | '''AI Image Caption Generator - Hugging Face Deployment''' |
| |
|
| | import gradio as gr |
| | from transformers import BlipProcessor, BlipForConditionalGeneration |
| | from PIL import Image |
| | import torch |
| | import warnings |
| |
|
| | warnings.filterwarnings("ignore") |
| |
|
| | |
| | processor = BlipProcessor.from_pretrained( |
| | "Salesforce/blip-image-captioning-base" |
| | ) |
| | model = BlipForConditionalGeneration.from_pretrained( |
| | "Salesforce/blip-image-captioning-base" |
| | ) |
| |
|
| | def generate_caption(image): |
| | image = Image.open(image).convert("RGB") |
| | inputs = processor(image, return_tensors="pt") |
| |
|
| | with torch.no_grad(): |
| | output = model.generate(**inputs) |
| |
|
| | caption = processor.decode(output[0], skip_special_tokens=True) |
| | return caption |
| |
|
| | iface = gr.Interface( |
| | fn=generate_caption, |
| | inputs=gr.Image(type="filepath"), |
| | outputs=gr.Textbox(label="Generated Caption"), |
| | title="AI Image Caption Generator", |
| | description="Upload an image and the model will generate a meaningful caption." |
| | ) |
| |
|
| | iface.launch() |