Spaces:
Build error
Build error
Primer push
Browse files- .ipynb_checkpoints/app-checkpoint.py +0 -0
- .ipynb_checkpoints/requirements-checkpoint.txt +0 -0
- app.py +59 -0
- requirements.txt +4 -0
.ipynb_checkpoints/app-checkpoint.py
ADDED
|
File without changes
|
.ipynb_checkpoints/requirements-checkpoint.txt
ADDED
|
File without changes
|
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import re
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
from transformers import DonutProcessor, VisionEncoderDecoderModel
|
| 9 |
+
from datasets import load_dataset
|
| 10 |
+
import torch
|
| 11 |
+
|
| 12 |
+
def get_attributes(input_img):
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
access_token = os.environ.get('key')
|
| 16 |
+
processor = DonutProcessor.from_pretrained("AA-supply/donut-finetuned-lic-1", use_auth_token=access_token)
|
| 17 |
+
model = VisionEncoderDecoderModel.from_pretrained("AA-supply/donut-finetuned-lic-1", use_auth_token=access_token)
|
| 18 |
+
|
| 19 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 20 |
+
|
| 21 |
+
model.eval()
|
| 22 |
+
model.to(device)
|
| 23 |
+
|
| 24 |
+
pixel_values = processor(input_img, return_tensors="pt").pixel_values
|
| 25 |
+
pixel_values = pixel_values.to(device)
|
| 26 |
+
print(pixel_values.size())
|
| 27 |
+
# prepare decoder inputs
|
| 28 |
+
task_prompt = "<s_cord-v2>"
|
| 29 |
+
decoder_input_ids = processor.tokenizer(task_prompt, add_special_tokens=False, return_tensors="pt").input_ids
|
| 30 |
+
decoder_input_ids = decoder_input_ids.to(device)
|
| 31 |
+
|
| 32 |
+
# autoregressively generate sequence
|
| 33 |
+
outputs = model.generate(
|
| 34 |
+
pixel_values,
|
| 35 |
+
decoder_input_ids=decoder_input_ids,
|
| 36 |
+
max_length=model.decoder.config.max_position_embeddings,
|
| 37 |
+
early_stopping=True,
|
| 38 |
+
pad_token_id=processor.tokenizer.pad_token_id,
|
| 39 |
+
eos_token_id=processor.tokenizer.eos_token_id,
|
| 40 |
+
use_cache=True,
|
| 41 |
+
num_beams=1,
|
| 42 |
+
bad_words_ids=[[processor.tokenizer.unk_token_id]],
|
| 43 |
+
return_dict_in_generate=True,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# turn into JSON
|
| 47 |
+
seq = processor.batch_decode(outputs.sequences)[0]
|
| 48 |
+
seq = seq.replace(processor.tokenizer.eos_token, "").replace(processor.tokenizer.pad_token, "")
|
| 49 |
+
seq = re.sub(r"<.*?>", "", seq, count=1).strip() # remove first task start token
|
| 50 |
+
seq = processor.token2json(seq)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
return str(seq)
|
| 57 |
+
|
| 58 |
+
demo = gr.Interface(get_attributes, "image", "label")
|
| 59 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
numpy
|
| 4 |
+
datasets
|