Spaces:
Build error
Build error
Update app.py
Browse files20 unmodified lines2122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611720 unmodified linesTESSERACT_LABEL = "Tesseract (HF default)"
def predict(image: Image.Image, question: str, ocr_engine: str): image_np = np.array(image)
if ocr_engine == PADDLE_OCR_LABEL: ocr_result = OCR.ocr(image_np, cls=False)[0] words = [x[1][0] for x in ocr_result] boxes = np.asarray([x[0] for x in ocr_result]) # (n_boxes, 4, 2)
for box in boxes: cv2.polylines(image_np, [box.reshape(-1, 1, 2).astype(int)], True, (0, 255, 255), 3)
x1 = boxes[:, :, 0].min(1) * 1000 / image.width y1 = boxes[:, :, 1].min(1) * 1000 / image.height x2 = boxes[:, :, 0].max(1) * 1000 / image.width y2 = boxes[:, :, 1].max(1) * 1000 / image.height
# (n_boxes, 4) in xyxy format boxes = np.stack([x1, y1, x2, y2], axis=1).astype(int)
elif ocr_engine == TESSERACT_LABEL: words, boxes = apply_tesseract(image, None, "")
for x1, y1, x2, y2 in boxes: x1 = int(x1 * image.width / 1000) y1 = int(y1 * image.height / 1000) x2 = int(x2 * image.width / 1000) y2 = int(y2 * image.height / 1000) cv2.rectangle(image_np, (x1, y1), (x2, y2), (0, 255, 255), 3)
else: raise ValueError(f"Unsupported ocr_engine={ocr_engine}")
token_ids = TOKENIZER(question)["input_ids"] token_boxes = [[0] * 4] * (len(token_ids) - 1) + [[1000] * 4] n_question_tokens = len(token_ids)
token_ids.append(TOKENIZER.sep_token_id) token_boxes.append([1000] * 4)
for word, box in zip(words, boxes): new_ids = TOKENIZER(word, add_special_tokens=False)["input_ids"] token_ids.extend(new_ids) token_boxes.extend([box] * len(new_ids))
token_ids.append(TOKENIZER.sep_token_id) token_boxes.append([1000] * 4)
with torch.inference_mode(): outputs = MODEL( input_ids=torch.tensor(token_ids).unsqueeze(0), bbox=torch.tensor(token_boxes).unsqueeze(0), )
start_scores = outputs.start_logits.squeeze(0).softmax(-1)[n_question_tokens:] end_scores = outputs.end_logits.squeeze(0).softmax(-1)[n_question_tokens:]
span_scores = start_scores.view(-1, 1) * end_scores.view(1, -1) span_scores = torch.triu(span_scores) # don't allow start < end
score, indices = span_scores.flatten().max(-1) start_idx = n_question_tokens + indices // span_scores.shape[1] end_idx = n_question_tokens + indices % span_scores.shape[1]
answer = TOKENIZER.decode(token_ids[start_idx : end_idx + 1])
return answer, score, image_np
import cv2import gradio as grimport numpy as npimport torchfrom paddleocr import PaddleOCRfrom PIL import Imagefrom transformers import AutoTokenizer, LayoutLMForQuestionAnsweringfrom transformers.pipelines.document_question_answering import apply_tesseract
model_tag = "impira/layoutlm-document-qa"MODEL = LayoutLMForQuestionAnswering.from_pretrained(model_tag).eval()TOKENIZER = AutoTokenizer.from_pretrained(model_tag)OCR = PaddleOCR( lang="en", det_limit_side_len=10_000, det_db_score_mode="slow",)
PADDLE_OCR_LABEL = "PaddleOCR (en)"TESSERACT_LABEL = "Tesseract (HF default)"
def predict(image: Image.Image, question: str, ocr_engine: str): image_np = np.array(image)
app.py+91