|
import gradio as gr
|
|
from transformers import AutoProcessor, PaliGemmaForConditionalGeneration
|
|
import torch
|
|
|
|
model_id = "pyimagesearch/finetuned_paligemma_vqav2_small"
|
|
model = PaliGemmaForConditionalGeneration.from_pretrained(model_id)
|
|
processor = AutoProcessor.from_pretrained("google/paligemma-3b-pt-224")
|
|
|
|
def process_image(image, prompt):
|
|
|
|
inputs = processor(image.convert("RGB"), prompt, return_tensors="pt")
|
|
|
|
try:
|
|
|
|
output = model.generate(**inputs, max_new_tokens=20)
|
|
|
|
|
|
decoded_output = processor.decode(output[0], skip_special_tokens=True)
|
|
|
|
|
|
return decoded_output[len(prompt):]
|
|
except IndexError as e:
|
|
print(f"IndexError: {e}")
|
|
return "An error occurred during processing."
|
|
|
|
inputs = [
|
|
gr.Image(type="pil"),
|
|
gr.Textbox(label="Prompt", placeholder="Enter your question")
|
|
]
|
|
outputs = gr.Textbox(label="Answer")
|
|
|
|
demo = gr.Interface(fn=process_image, inputs=inputs, outputs=outputs, title="Visual Question Answering with Fine-tuned PaliGemma Model", description="Upload an image and ask questions to get answers.")
|
|
|
|
demo.launch() |