Spaces:
Paused
Paused
File size: 2,954 Bytes
ebd3f73 f263316 a91ce54 ebd3f73 a91ce54 ebd3f73 3b5e294 ebd3f73 3b5e294 ebd3f73 6335c31 ebd3f73 b7d24be ebd3f73 b7d24be f263316 b7d24be ebd3f73 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
import gradio as gr
import re
from transformers import DonutProcessor, VisionEncoderDecoderModel
import torch
from PIL import Image
def process_filename(filename, question):
print(f"Image file: {filename}")
print(f"Question: {question}")
image = Image.open(filename).convert("RGB")
return process_image(image)
def process_image(set_use_cache, set_return_dict_in_generate, set_early_stopping, set_output_scores, image, question):
repo_id = "naver-clova-ix/donut-base-finetuned-docvqa"
print(f"Model repo: {repo_id}")
processor = DonutProcessor.from_pretrained(repo_id)
model = VisionEncoderDecoderModel.from_pretrained(repo_id)
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Device used: {device}")
model.to(device)
# prepare decoder inputs
prompt = f"<s_docvqa><s_question>{question}</s_question><s_answer>"
decoder_input_ids = processor.tokenizer(prompt, add_special_tokens=False, return_tensors="pt").input_ids
pixel_values = processor(image, return_tensors="pt").pixel_values
outputs = model.generate(
pixel_values.to(device),
decoder_input_ids=decoder_input_ids.to(device),
max_length=model.decoder.config.max_position_embeddings,
pad_token_id=processor.tokenizer.pad_token_id,
eos_token_id=processor.tokenizer.eos_token_id,
use_cache=set_use_cache=="True",
bad_words_ids=[[processor.tokenizer.unk_token_id]],
return_dict_in_generate=set_return_dict_in_generate=="True",
early_stopping=set_early_stopping=="True",
output_scores=set_output_scores=="True"
)
sequence_data = processor.batch_decode(outputs.sequences)
print(f"Sequence data: {sequence_data}")
sequence = sequence_data[0]
print(f"Sequence: {sequence}")
sequence = sequence.replace(processor.tokenizer.eos_token, "").replace(processor.tokenizer.pad_token, "")
sequence = re.sub(r"<.*?>", "", sequence, count=1).strip() # remove first task start token
print(processor.token2json(sequence))
return processor.token2json(sequence)['answer']
description = "DocVQA (document visual question answering)"
demo = gr.Interface(
fn=process_image,
inputs=[
gr.Radio(["True", "False"], value="True", label="Use cache", info="Define model.generate() use_cache value"),
gr.Radio(["True", "False"], value="True", label="Dict in generate", info="Define model.generate() return_dict_in_generate value"),
gr.Radio(["True", "False"], value="True", label="Early stopping", info="Define model.generate() early_stopping value"),
gr.Radio(["True", "False"], value="True", label="Output scores", info="Define model.generate() output_scores value"),
"image",
gr.Textbox(label = "Question" )
],
outputs=gr.Textbox(label = "Response" ),
title="Extract data from image",
description=description,
cache_examples=True)
demo.launch()
|