Upload 11 files
Browse files- config.json +132 -0
- handler.py +44 -0
- pipeline.py +44 -0
config.json
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "microsoft/layoutlmv2-base-uncased",
|
| 3 |
+
"architectures": [
|
| 4 |
+
"LayoutLMv2ForQuestionAnswering"
|
| 5 |
+
],
|
| 6 |
+
"attention_probs_dropout_prob": 0.1,
|
| 7 |
+
"convert_sync_batchnorm": true,
|
| 8 |
+
"coordinate_size": 128,
|
| 9 |
+
"detectron2_config_args": {
|
| 10 |
+
"MODEL.ANCHOR_GENERATOR.SIZES": [
|
| 11 |
+
[
|
| 12 |
+
32
|
| 13 |
+
],
|
| 14 |
+
[
|
| 15 |
+
64
|
| 16 |
+
],
|
| 17 |
+
[
|
| 18 |
+
128
|
| 19 |
+
],
|
| 20 |
+
[
|
| 21 |
+
256
|
| 22 |
+
],
|
| 23 |
+
[
|
| 24 |
+
512
|
| 25 |
+
]
|
| 26 |
+
],
|
| 27 |
+
"MODEL.BACKBONE.NAME": "build_resnet_fpn_backbone",
|
| 28 |
+
"MODEL.FPN.IN_FEATURES": [
|
| 29 |
+
"res2",
|
| 30 |
+
"res3",
|
| 31 |
+
"res4",
|
| 32 |
+
"res5"
|
| 33 |
+
],
|
| 34 |
+
"MODEL.MASK_ON": true,
|
| 35 |
+
"MODEL.PIXEL_STD": [
|
| 36 |
+
57.375,
|
| 37 |
+
57.12,
|
| 38 |
+
58.395
|
| 39 |
+
],
|
| 40 |
+
"MODEL.POST_NMS_TOPK_TEST": 1000,
|
| 41 |
+
"MODEL.RESNETS.ASPECT_RATIOS": [
|
| 42 |
+
[
|
| 43 |
+
0.5,
|
| 44 |
+
1.0,
|
| 45 |
+
2.0
|
| 46 |
+
]
|
| 47 |
+
],
|
| 48 |
+
"MODEL.RESNETS.DEPTH": 101,
|
| 49 |
+
"MODEL.RESNETS.NUM_GROUPS": 32,
|
| 50 |
+
"MODEL.RESNETS.OUT_FEATURES": [
|
| 51 |
+
"res2",
|
| 52 |
+
"res3",
|
| 53 |
+
"res4",
|
| 54 |
+
"res5"
|
| 55 |
+
],
|
| 56 |
+
"MODEL.RESNETS.SIZES": [
|
| 57 |
+
[
|
| 58 |
+
32
|
| 59 |
+
],
|
| 60 |
+
[
|
| 61 |
+
64
|
| 62 |
+
],
|
| 63 |
+
[
|
| 64 |
+
128
|
| 65 |
+
],
|
| 66 |
+
[
|
| 67 |
+
256
|
| 68 |
+
],
|
| 69 |
+
[
|
| 70 |
+
512
|
| 71 |
+
]
|
| 72 |
+
],
|
| 73 |
+
"MODEL.RESNETS.STRIDE_IN_1X1": false,
|
| 74 |
+
"MODEL.RESNETS.WIDTH_PER_GROUP": 8,
|
| 75 |
+
"MODEL.ROI_BOX_HEAD.NAME": "FastRCNNConvFCHead",
|
| 76 |
+
"MODEL.ROI_BOX_HEAD.NUM_FC": 2,
|
| 77 |
+
"MODEL.ROI_BOX_HEAD.POOLER_RESOLUTION": 14,
|
| 78 |
+
"MODEL.ROI_HEADS.IN_FEATURES": [
|
| 79 |
+
"p2",
|
| 80 |
+
"p3",
|
| 81 |
+
"p4",
|
| 82 |
+
"p5"
|
| 83 |
+
],
|
| 84 |
+
"MODEL.ROI_HEADS.NAME": "StandardROIHeads",
|
| 85 |
+
"MODEL.ROI_HEADS.NUM_CLASSES": 5,
|
| 86 |
+
"MODEL.ROI_MASK_HEAD.NAME": "MaskRCNNConvUpsampleHead",
|
| 87 |
+
"MODEL.ROI_MASK_HEAD.NUM_CONV": 4,
|
| 88 |
+
"MODEL.ROI_MASK_HEAD.POOLER_RESOLUTION": 7,
|
| 89 |
+
"MODEL.RPN.IN_FEATURES": [
|
| 90 |
+
"p2",
|
| 91 |
+
"p3",
|
| 92 |
+
"p4",
|
| 93 |
+
"p5",
|
| 94 |
+
"p6"
|
| 95 |
+
],
|
| 96 |
+
"MODEL.RPN.POST_NMS_TOPK_TRAIN": 1000,
|
| 97 |
+
"MODEL.RPN.PRE_NMS_TOPK_TEST": 1000,
|
| 98 |
+
"MODEL.RPN.PRE_NMS_TOPK_TRAIN": 2000
|
| 99 |
+
},
|
| 100 |
+
"fast_qkv": true,
|
| 101 |
+
"gradient_checkpointing": false,
|
| 102 |
+
"has_relative_attention_bias": true,
|
| 103 |
+
"has_spatial_attention_bias": true,
|
| 104 |
+
"has_visual_segment_embedding": true,
|
| 105 |
+
"hidden_act": "gelu",
|
| 106 |
+
"hidden_dropout_prob": 0.1,
|
| 107 |
+
"hidden_size": 768,
|
| 108 |
+
"image_feature_pool_shape": [
|
| 109 |
+
7,
|
| 110 |
+
7,
|
| 111 |
+
256
|
| 112 |
+
],
|
| 113 |
+
"initializer_range": 0.02,
|
| 114 |
+
"intermediate_size": 3072,
|
| 115 |
+
"layer_norm_eps": 1e-12,
|
| 116 |
+
"max_2d_position_embeddings": 1024,
|
| 117 |
+
"max_position_embeddings": 512,
|
| 118 |
+
"max_rel_2d_pos": 256,
|
| 119 |
+
"max_rel_pos": 128,
|
| 120 |
+
"model_type": "layoutlmv2",
|
| 121 |
+
"num_attention_heads": 12,
|
| 122 |
+
"num_hidden_layers": 12,
|
| 123 |
+
"output_past": true,
|
| 124 |
+
"pad_token_id": 0,
|
| 125 |
+
"rel_2d_pos_bins": 64,
|
| 126 |
+
"rel_pos_bins": 32,
|
| 127 |
+
"shape_size": 128,
|
| 128 |
+
"torch_dtype": "float32",
|
| 129 |
+
"transformers_version": "4.35.2",
|
| 130 |
+
"type_vocab_size": 2,
|
| 131 |
+
"vocab_size": 30522
|
| 132 |
+
}
|
handler.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import holidays
|
| 4 |
+
import PIL.Image
|
| 5 |
+
import io
|
| 6 |
+
import pytesseract
|
| 7 |
+
|
| 8 |
+
class PreTrainedPipeline():
|
| 9 |
+
def __init__(self, model_path="PrimWong/layout_qa_hparam_tuning"):
|
| 10 |
+
# Initializing the document-question-answering pipeline with the specified model
|
| 11 |
+
self.pipeline = pipeline("document-question-answering", model=model_path)
|
| 12 |
+
self.holidays = holidays.US()
|
| 13 |
+
|
| 14 |
+
def __call__(self, data: Dict[str, Any]) -> str:
|
| 15 |
+
"""
|
| 16 |
+
Process input data for document question answering with optional holiday checking.
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
data (Dict[str, Any]): Input data containing an 'inputs' field with 'image' and 'question',
|
| 20 |
+
and optionally a 'date' field.
|
| 21 |
+
|
| 22 |
+
Returns:
|
| 23 |
+
str: The answer to the question or a holiday message if applicable.
|
| 24 |
+
"""
|
| 25 |
+
inputs = data.get('inputs', {})
|
| 26 |
+
date = data.get("date")
|
| 27 |
+
|
| 28 |
+
# Check if date is provided and if it's a holiday
|
| 29 |
+
if date and date in self.holidays:
|
| 30 |
+
return "Today is a holiday!"
|
| 31 |
+
|
| 32 |
+
# Process the image and question for document question answering
|
| 33 |
+
image_path = inputs.get("image")
|
| 34 |
+
question = inputs.get("question")
|
| 35 |
+
|
| 36 |
+
# Load and process an image
|
| 37 |
+
image = PIL.Image.open(image_path)
|
| 38 |
+
image_text = pytesseract.image_to_string(image) # Use OCR to extract text
|
| 39 |
+
|
| 40 |
+
# Run prediction (Note: this now uses the extracted text, not the image directly)
|
| 41 |
+
prediction = self.pipeline(question=question, context=image_text)
|
| 42 |
+
return prediction["answer"] # Adjust based on actual output format of the model
|
| 43 |
+
|
| 44 |
+
# Note: This script assumes the use of pytesseract for OCR to process images. Ensure pytesseract is configured properly.
|
pipeline.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import holidays
|
| 4 |
+
import PIL.Image
|
| 5 |
+
import io
|
| 6 |
+
import pytesseract
|
| 7 |
+
|
| 8 |
+
class PreTrainedPipeline():
|
| 9 |
+
def __init__(self, model_path="PrimWong/layout_qa_hparam_tuning"):
|
| 10 |
+
# Initializing the document-question-answering pipeline with the specified model
|
| 11 |
+
self.pipeline = pipeline("document-question-answering", model=model_path)
|
| 12 |
+
self.holidays = holidays.US()
|
| 13 |
+
|
| 14 |
+
def __call__(self, data: Dict[str, Any]) -> str:
|
| 15 |
+
"""
|
| 16 |
+
Process input data for document question answering with optional holiday checking.
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
data (Dict[str, Any]): Input data containing an 'inputs' field with 'image' and 'question',
|
| 20 |
+
and optionally a 'date' field.
|
| 21 |
+
|
| 22 |
+
Returns:
|
| 23 |
+
str: The answer to the question or a holiday message if applicable.
|
| 24 |
+
"""
|
| 25 |
+
inputs = data.get('inputs', {})
|
| 26 |
+
date = data.get("date")
|
| 27 |
+
|
| 28 |
+
# Check if date is provided and if it's a holiday
|
| 29 |
+
if date and date in self.holidays:
|
| 30 |
+
return "Today is a holiday!"
|
| 31 |
+
|
| 32 |
+
# Process the image and question for document question answering
|
| 33 |
+
image_path = inputs.get("image")
|
| 34 |
+
question = inputs.get("question")
|
| 35 |
+
|
| 36 |
+
# Load and process an image
|
| 37 |
+
image = PIL.Image.open(image_path)
|
| 38 |
+
image_text = pytesseract.image_to_string(image) # Use OCR to extract text
|
| 39 |
+
|
| 40 |
+
# Run prediction (Note: this now uses the extracted text, not the image directly)
|
| 41 |
+
prediction = self.pipeline(question=question, context=image_text)
|
| 42 |
+
return prediction["answer"] # Adjust based on actual output format of the model
|
| 43 |
+
|
| 44 |
+
# Note: This script assumes the use of pytesseract for OCR to process images. Ensure pytesseract is configured properly.
|