jinhybr commited on
Commit
cb6a0c6
·
verified ·
1 Parent(s): 9754968

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -1
app.py CHANGED
@@ -1,3 +1,33 @@
 
1
  import gradio as gr
 
 
2
 
3
- gr.load("models/jinhybr/Fine-tune-UDOP-CDIP").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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", gr.Textbox(label = "Question" )],
26
+ outputs=gr.Textbox(label = "Response" ),
27
+ title="Demo: UDOP for DocVQA",
28
+ description=description,
29
+ article=article,
30
+ examples=[["example_1.png", "When is the coffee break?"]],
31
+ cache_examples=True)
32
+
33
+ demo.launch()