Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import UdopProcessor, UdopForConditionalGeneration
|
| 5 |
+
|
| 6 |
+
repo_id = "microsoft/udop-large"
|
| 7 |
+
|
| 8 |
+
processor = UdopProcessor.from_pretrained(repo_id)
|
| 9 |
+
model = UdopForConditionalGeneration.from_pretrained(repo_id)
|
| 10 |
+
|
| 11 |
+
def process_document(image, question):
|
| 12 |
+
|
| 13 |
+
pixel_values = processor(image, return_tensors="pt").pixel_values
|
| 14 |
+
encoding = processor(images=image, text=question, return_tensors="pt")
|
| 15 |
+
outputs = model.generate(**encoding, max_new_tokens=20)
|
| 16 |
+
generated_text = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 17 |
+
|
| 18 |
+
return generated_text
|
| 19 |
+
|
| 20 |
+
description = "Unofficial Gradio Demo for UDOP for DocVQA (document visual question answering). To use it, simply upload your image and type a question and click 'submit', or click one of the examples to load them. Read more at the links below."
|
| 21 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/pdf/2212.02623.pdf' target='_blank'>Unifying Vision, Text, and Layout for Universal Document Processing</a> | <a href='https://github.com/microsoft/UDOP' target='_blank'>Github Repo</a></p>"
|
| 22 |
+
|
| 23 |
+
demo = gr.Interface(
|
| 24 |
+
fn=process_document,
|
| 25 |
+
inputs=["image", "text"],
|
| 26 |
+
outputs="text",
|
| 27 |
+
title="Demo: UDOP for DocVQA",
|
| 28 |
+
description=description,
|
| 29 |
+
article=article,
|
| 30 |
+
enable_queue=True,
|
| 31 |
+
# examples=[["example_1.png", "When is the coffee break?"], ["example_2.jpeg", "What's the population of Stoddard?"]],
|
| 32 |
+
cache_examples=False)
|
| 33 |
+
|
| 34 |
+
demo.launch()
|