unography's picture
Update app.py
db6a57c verified
raw
history blame
1.18 kB
#!/usr/bin/env python
from __future__ import annotations
import gradio as gr
import PIL.Image
import spaces
import torch
from transformers import AutoProcessor, BlipForConditionalGeneration
DESCRIPTION = "# Image Captioning with LongCap"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model_id = "unography/blip-long-cap"
processor = AutoProcessor.from_pretrained(model_id)
model = BlipForConditionalGeneration.from_pretrained(model_id).to(device)
@spaces.GPU(duration=30)
def run(image: PIL.Image.Image) -> str:
inputs = processor(images=image, return_tensors="pt").to(device)
out = model.generate(pixel_values=inputs.pixel_values, num_beams=3, max_length=300)
generated_caption = processor.decode(out[0], skip_special_tokens=True)
return generated_caption
with gr.Blocks(css="style.css") as demo:
gr.Markdown(DESCRIPTION)
input_image = gr.Image(type="pil")
run_button = gr.Button("Caption")
output = gr.Textbox(label="Result")
run_button.click(
fn=run,
inputs=input_image,
outputs=output,
api_name="caption",
)
if __name__ == "__main__":
demo.queue(max_size=20).launch()