File size: 1,059 Bytes
6150803 1f034ba 6150803 62607a0 6150803 | 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 | # -*- coding: utf-8 -*-
'''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")
# Use BASE model (faster + safer on HF free CPU)
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() |