Spaces:
Sleeping
Sleeping
File size: 1,680 Bytes
d2237d8 4a49fad d2237d8 4a49fad 38e01ae 9dcc0d8 d2237d8 4a49fad 9dcc0d8 daeb894 d2237d8 4a49fad daeb894 4a49fad |
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import os
#from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
from transformers import pipeline
from huggingface_hub import login
# Safe writable cache dirs in Hugging Face Spaces move to space settings vars`
#os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
#os.environ["HF_HOME"] = "/tmp/hf_home"
#os.environ["HF_HUB_CACHE"] = "/tmp/hf_hub"
MODEL_ID = "TypicaAI/magbert-ner"
#HF_TOKEN = os.getenv("HF_TOKEN")
#tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
#model = AutoModelForTokenClassification.from_pretrained(MODEL_ID, token=HF_TOKEN)
#ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer, aggregation_strategy="first")
# Initialize global pipeline
ner_pipeline = None
# Authenticate using the secret `HFTOKEN`
def authenticate_with_token():
"""Authenticate with the Hugging Face API using the HFTOKEN secret."""
hf_token = os.getenv("HF_TOKEN") # Retrieve the token from environment variables
if not hf_token:
raise ValueError("HF_TOKEN is not set. Please add it to the Secrets in your Space settings.")
login(token=hf_token)
def load_healthcare_ner_pipeline():
"""Load the Hugging Face pipeline for Healthcare NER."""
global ner_pipeline
if ner_pipeline is None:
# Authenticate and initialize pipeline
authenticate_with_token()
ner_pipeline = pipeline(
"token-classification",
model=MODEL_ID,
aggregation_strategy="first" # Groups B- and I- tokens into entities
)
return ner_pipeline
# Get NER pipeline
ner_pipeline = load_healthcare_ner_pipeline()
|