Download handler.py from izzelbas/bert-base-ugm-cs-curriculum-qa: direct link, hf CLI and curl.
- Browser
- Download file 755 Bytes
-
https://huggingface.co/izzelbas/bert-base-ugm-cs-curriculum-qa/resolve/main/handler.py
- Command line
-
hf download hf://izzelbas/bert-base-ugm-cs-curriculum-qa/handler.py
-
curl -L -o handler.py https://huggingface.co/izzelbas/bert-base-ugm-cs-curriculum-qa/resolve/main/handler.py
755 Bytes
| from transformers import pipeline, AutoTokenizer, AutoModelForQuestionAnswering | |
| class EndpointHandler: | |
| def __init__(self, path=""): | |
| self.model = AutoModelForQuestionAnswering.from_pretrained(path) | |
| self.tokenizer = AutoTokenizer.from_pretrained(path) | |
| self.qa_pipeline = pipeline("question-answering", model=self.model, tokenizer=self.tokenizer) | |
| def __call__(self, data): | |
| inputs = data.get("inputs", {}) | |
| question = inputs.get("question") | |
| context = inputs.get("context") | |
| if not question or not context: | |
| return {"error": "Both 'question' and 'context' must be provided."} | |
| result = self.qa_pipeline(question=question, context=context) | |
| return result |