|
import torch |
|
from transformers import AutoProcessor, AutoModelForCausalLM |
|
from PIL import Image |
|
import gradio as gr |
|
|
|
|
|
processor = AutoProcessor.from_pretrained("microsoft/git-large-textcaps") |
|
|
|
|
|
model = AutoModelForCausalLM.from_pretrained("microsoft/git-large-textcaps") |
|
|
|
|
|
custom_weights_path = "model_folder/pytorch_model.bin" |
|
model.load_state_dict(torch.load(custom_weights_path, map_location=torch.device("cpu"))) |
|
model.eval() |
|
|
|
|
|
def generate_caption(image): |
|
|
|
image = Image.fromarray(image) |
|
|
|
|
|
inputs = processor(images=image, return_tensors="pt") |
|
pixel_values = inputs.pixel_values |
|
|
|
|
|
generated_ids = model.generate(pixel_values=pixel_values, max_length=50) |
|
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] |
|
|
|
return generated_caption |
|
|
|
|
|
interface = gr.Interface( |
|
fn=generate_caption, |
|
inputs=gr.Image(), |
|
outputs=gr.Textbox(), |
|
live=True |
|
) |
|
|
|
|
|
interface.launch() |
|
|