Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

models: add support for Hugging Face model hub #1974

Merged
merged 3 commits into from
Nov 25, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions flair/models/sequence_tagger_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,24 @@ def _fetch_model(model_name) -> str:
unzip_file(Path(flair.cache_root) / cache_dir / 'freeIndirect.zip', Path(flair.cache_root) / cache_dir)
model_name = str(Path(flair.cache_root) / cache_dir / 'freeIndirect' / 'final-model.pt')

# Fallback to Hugging Face model hub
if not Path(model_name).exists() and not model_name.startswith("http"):
# e.g. stefan-it/flair-ner-conll03 is a valid namespace
# and stefan-it/flair-ner-conll03@main supports specifying a commit/branch name
hf_model_name = "model.bin"
revision = "main"

if "@" in model_name:
model_name_splitted = model_name.split("@")
revision = model_name_splitted[-1]
model_name = model_name_splitted[0]

# Lazy import
from transformers import file_utils

url = file_utils.hf_bucket_url(model_id=model_name, revision=revision, filename=hf_model_name)
model_name = file_utils.cached_path(url_or_filename=url, cache_dir=flair.cache_root)

return model_name

def get_transition_matrix(self):
Expand Down