|
import gradio as gr |
|
from huggingface_hub import hf_hub_download |
|
from PIL import Image |
|
import torch |
|
from transformers import AutoImageProcessor, AutoModelForObjectDetection |
|
|
|
gr.load("models/microsoft/table-transformer-structure-recognition").launch() |
|
|
|
processor = AutoImageProcessor.from_pretrained("microsoft/table-transformer-structure-recognition") |
|
model = AutoModelForObjectDetection.from_pretrained("microsoft/table-transformer-structure-recognition") |
|
|
|
|
|
def predict(image): |
|
|
|
inputs = processor(images=image, return_tensors="pt") |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
|
|
|
|
predicted_boxes = outputs.pred_boxes[0].cpu().numpy() |
|
predicted_classes = outputs.logits.argmax(-1).cpu().numpy() |
|
|
|
return {"boxes": predicted_boxes.tolist(), "classes": predicted_classes.tolist()} |
|
|
|
|
|
interface = gr.Interface( |
|
fn=predict, |
|
inputs=gr.Image(type="pil"), |
|
outputs="json", |
|
) |
|
|
|
|
|
interface.launch() |
|
|