Spaces:
Runtime error
Runtime error
harish3110
commited on
Commit
β’
afa689f
1
Parent(s):
854b899
Updated app.py to take pdfs
Browse files
app.py
CHANGED
@@ -1,55 +1,15 @@
|
|
1 |
-
import re
|
2 |
import gradio as gr
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
from transformers import DonutProcessor, VisionEncoderDecoderModel
|
6 |
-
|
7 |
-
model_name = "harish3110/donut-quandri-all-data"
|
8 |
-
|
9 |
-
processor = DonutProcessor.from_pretrained(model_name)
|
10 |
-
model = VisionEncoderDecoderModel.from_pretrained(model_name)
|
11 |
-
|
12 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
-
model.to(device)
|
14 |
-
|
15 |
-
def process_document(image):
|
16 |
-
# prepare encoder inputs
|
17 |
-
pixel_values = processor(image, return_tensors="pt").pixel_values
|
18 |
-
|
19 |
-
# prepare decoder inputs
|
20 |
-
task_prompt = "<s_cord-v2>"
|
21 |
-
decoder_input_ids = processor.tokenizer(task_prompt, add_special_tokens=False, return_tensors="pt").input_ids
|
22 |
-
|
23 |
-
# generate answer
|
24 |
-
outputs = model.generate(
|
25 |
-
pixel_values.to(device),
|
26 |
-
decoder_input_ids=decoder_input_ids.to(device),
|
27 |
-
max_length=model.decoder.config.max_position_embeddings,
|
28 |
-
early_stopping=True,
|
29 |
-
pad_token_id=processor.tokenizer.pad_token_id,
|
30 |
-
eos_token_id=processor.tokenizer.eos_token_id,
|
31 |
-
use_cache=True,
|
32 |
-
num_beams=1,
|
33 |
-
bad_words_ids=[[processor.tokenizer.unk_token_id]],
|
34 |
-
return_dict_in_generate=True,
|
35 |
-
)
|
36 |
-
|
37 |
-
# postprocess
|
38 |
-
sequence = processor.batch_decode(outputs.sequences)[0]
|
39 |
-
sequence = sequence.replace(processor.tokenizer.eos_token, "").replace(processor.tokenizer.pad_token, "")
|
40 |
-
sequence = re.sub(r"<.*?>", "", sequence, count=1).strip() # remove first task start token
|
41 |
-
|
42 |
-
return processor.token2json(sequence)
|
43 |
-
|
44 |
description = "Gradio Demo for Donut trained on Quandri internal dataset."
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
demo.launch(auth=("quandri", "quandri1234"))
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from helpers import majority_vote_dicts
|
3 |
+
from model import process_document, process_pdf
|
4 |
|
5 |
+
input_interface = gr.inputs.File(label="Upload a PDF file")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
description = "Gradio Demo for Donut trained on Quandri internal dataset."
|
7 |
+
gradio_interface = gr.Interface(fn=process_pdf,
|
8 |
+
inputs=input_interface,
|
9 |
+
outputs= 'json',
|
10 |
+
title="Demo: Donut π© for Document Parsing on Quandri Dataset",
|
11 |
+
description=description,
|
12 |
+
examples=[["data/aviva.pdf"], ["data/wawanesa.pdf"], ["data/sgi.pdf"]],
|
13 |
+
)
|
14 |
+
|
15 |
+
gradio_interface.launch()
|
|
|
|