|
import torch |
|
from transformers import AutoProcessor, AutoModelForVision2Seq |
|
from PIL import Image |
|
import gradio as gr |
|
|
|
|
|
saved_folder_path = "model_folder" |
|
|
|
|
|
processor = AutoProcessor.from_pretrained(saved_folder_path) |
|
model = AutoModelForVision2Seq.from_pretrained(saved_folder_path) |
|
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(label="Upload an Image"), |
|
outputs=gr.Textbox(label="Generated Caption", lines=2), |
|
|
|
live=True, |
|
title="๐ธ Image Caption Generator", |
|
description="Upload an chest x-ray image to generate a descriptive caption using our AI model. Built with Transformers and Gradio.", |
|
theme="allenai/gradio-theme", |
|
css=".output { font-size: 16px; padding: 10px; border: 1px solid #ccc; border-radius: 5px; }", |
|
) |
|
|
|
|
|
interface.launch() |
|
|