Spaces:
Sleeping
Sleeping
import gradio as gr | |
import PIL.Image | |
import transformers | |
from transformers import PaliGemmaForConditionalGeneration, PaliGemmaProcessor | |
import torch | |
import os | |
import string | |
import functools | |
import re | |
import numpy as np | |
import spaces | |
from PIL import Image | |
model_id = "mattraj/curacel-autodamage-1" | |
COLORS = ['#4285f4', '#db4437', '#f4b400', '#0f9d58', '#e48ef1'] | |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
model = PaliGemmaForConditionalGeneration.from_pretrained(model_id, torch_dtype=torch.bfloat16).eval().to(device) | |
processor = PaliGemmaProcessor.from_pretrained(model_id) | |
###### Transformers Inference | |
def infer( | |
image: PIL.Image.Image, | |
text: str, | |
max_new_tokens: int | |
) -> str: | |
inputs = processor(text=text, images=image, return_tensors="pt", padding="longest", do_convert_rgb=True).to(device).to(dtype=model.dtype) | |
with torch.no_grad(): | |
generated_ids = model.generate( | |
**inputs, | |
max_length=2048 | |
) | |
result = processor.decode(generated_ids[0], skip_special_tokens=True) | |
return result | |
######## Demo | |
INTRO_TEXT = """## Curacel Auto Damage demo\n\n | |
Finetuned from: google/paligemma-3b-pt-448 | |
""" | |
with gr.Blocks(css="style.css") as demo: | |
gr.Markdown(INTRO_TEXT) | |
with gr.Tab("Text Generation"): | |
with gr.Column(): | |
image = gr.Image(type="pil") | |
text_input = gr.Text(label="Input Text") | |
text_output = gr.Text(label="Text Output") | |
chat_btn = gr.Button() | |
chat_inputs = [ | |
image, | |
text_input | |
] | |
chat_outputs = [ | |
text_output | |
] | |
chat_btn.click( | |
fn=infer, | |
inputs=chat_inputs, | |
outputs=chat_outputs, | |
) | |
examples = [["./car-1.png", "detect Front-Windscreen-Damage ; Headlight-Damage ; Major-Rear-Bumper-Dent ; Rear-windscreen-Damage ; RunningBoard-Dent ; Sidemirror-Damage ; Signlight-Damage ; Taillight-Damage ; bonnet-dent ; doorouter-dent ; doorouter-scratch ; fender-dent ; front-bumper-dent ; front-bumper-scratch ; medium-Bodypanel-Dent ; paint-chip ; paint-trace ; pillar-dent ; quaterpanel-dent ; rear-bumper-dent ; rear-bumper-scratch ; roof-dent"]] | |
gr.Markdown("") | |
gr.Examples( | |
examples=examples, | |
inputs=chat_inputs, | |
) | |
######### | |
if __name__ == "__main__": | |
demo.queue(max_size=10).launch(debug=True) |