Spaces:
Sleeping
Sleeping
File size: 2,963 Bytes
ddb7273 3a912c5 ddb7273 3a912c5 8fe0386 3a912c5 ddb7273 3a912c5 7f21ba9 ddb7273 7f21ba9 ddb7273 fffe8df ddb7273 fffe8df ddb7273 |
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 72 73 74 75 76 77 78 79 80 81 82 |
import re
import torch
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from transformers import DonutProcessor, VisionEncoderDecoderModel
from PIL import Image
from io import BytesIO
origins = [
"https://duithive.vercel.app",
"https://staging-duithive.vercel.app",
"http://localhost:3000",
]
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
processor = DonutProcessor.from_pretrained("naver-clova-ix/donut-base-finetuned-cord-v2", use_fast=False)
model = VisionEncoderDecoderModel.from_pretrained("naver-clova-ix/donut-base-finetuned-cord-v2")
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
task_prompt = "<s_cord-v2>"
decoder_input_ids = processor.tokenizer(task_prompt, add_special_tokens=False, return_tensors="pt").input_ids
# def generateOutput(fileData):
# pil_image = Image.open(BytesIO(fileData))
# resized_image = pil_image.resize((800, 600)).convert('RGB')
# rgb_image = Image.new('RGB', resized_image.size)
# rgb_image.paste(resized_image)
# output_buffer = BytesIO()
# rgb_image.save(output_buffer, format="JPEG", quality = 100)
# jpeg_image = Image.open(BytesIO(output_buffer.getvalue()))
# pixel_values = processor(jpeg_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=True,
# bad_words_ids=[[processor.tokenizer.unk_token_id]],
# return_dict_in_generate=True,
# )
# return outputs
def generateOutput(fileData):
pil_image = Image.open(BytesIO(fileData))
pil_image.resize((800, 600))
pixel_values = processor(pil_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=True,
bad_words_ids=[[processor.tokenizer.unk_token_id]],
return_dict_in_generate=True,
)
return outputs
@app.post("/ocr/")
async def analyze_image(file: UploadFile = File(...)):
content = await file.read()
outputs = generateOutput(content)
sequence = processor.batch_decode(outputs.sequences)[0]
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
return processor.token2json(sequence)
|