File size: 835 Bytes
0e0b81c
 
 
 
 
5b026ad
33ae704
b45b86a
 
d740dd0
b45b86a
33ae704
b45b86a
f1869ff
b45b86a
 
f1869ff
b45b86a
38ba283
b45b86a
 
 
1e0a666
 
33ae704
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
import os

# ✅ Prevent Hugging Face from trying to load TensorFlow
os.environ["TRANSFORMERS_NO_TF"] = "1"

from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline

# Load tokenizer and model once during startup
try:
    cache_dir = "model_cache"
    model_name = "deepset/roberta-base-squad2"

    print(f"Loading QA tokenizer from {model_name}...")
    tokenizer = AutoTokenizer.from_pretrained(model_name, cache_dir=cache_dir)

    print(f"Loading QA model from {model_name}...")
    model = AutoModelForQuestionAnswering.from_pretrained(model_name, cache_dir=cache_dir)

    print("QA Model and tokenizer loaded successfully!")
except Exception as e:
    print(f"Error loading model: {str(e)}")
    raise

def load_qa_pipeline():
    return pipeline("question-answering", model=model, tokenizer=tokenizer)