Download app/model_loader.py from Juslin/elim-api: direct link, hf CLI and curl.
- Browser
- Download file 835 Bytes
-
https://huggingface.co/spaces/Juslin/elim-api/resolve/main/app/model_loader.py
- Command line
-
hf download hf://spaces/Juslin/elim-api/app/model_loader.py
-
curl -L -o model_loader.py https://huggingface.co/spaces/Juslin/elim-api/resolve/main/app/model_loader.py
835 Bytes
| 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) | |