File size: 1,312 Bytes
85bfdae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Dict, Any
from transformers import pipeline
import holidays
import PIL.Image
import io

class PreTrainedPipeline():
    def __init__(self, model_path="PrimWong/layout_qa_hparam_tuning"):
        # Initialize the document-question-answering pipeline with the specified model
        self.pipeline = pipeline("document-question-answering", model=model_path)
        self.holidays = holidays.US()

    def __call__(self, data: Dict[str, Any]) -> str:
        """
        Process input data for document question answering with optional holiday checking.

        Args:
            data (Dict[str, Any]): Input data containing a 'text' field possibly along with 'image',
                                   and optionally a 'date' field.

        Returns:
            str: The answer or processed information based on the text, or a holiday message if applicable.
        """
        text = data.get("inputs")
        date = data.get("date")

        # Check if the date is a holiday
        if date and date in self.holidays:
            return "Today is a holiday!"

        # Run prediction using only the text input
        prediction = self.pipeline(question=text, image="What information do you need?")
        return prediction["answer"]  # Adjust based on actual output format of the model