An interactive API for model lookup within HuggingFace and Flair

Tasks

HF_TASKS and FLAIR_TASKS are namespace objects that can enable tab-completion when searching for specific tasks within the HFModelHub and FlairModelHub

class HF_TASKS[source]

HF_TASKS(*args, **kwargs)

A list of all HuggingFace tasks for valid API lookup as attribtues to get tab-completion and typo-proofing

Usage Examples:

Sample code from: 02_model_hub.ipynb (View Notebook for more context)

_all_ = ['HF_TASKS']

Sample code from: 02_model_hub.ipynb (View Notebook for more context)

mk_class('HF_TASKS', **_hf_tasks,
        doc="A list of all HuggingFace tasks for valid API lookup as attribtues to get tab-completion and typo-proofing")
Possible tasks:
* fill-mask
* question-answering
* summarization
* table-question-answering
* text-classification
* text-generation
* text2text-generation
* token-classification
* translation
* zero-shot-classification
* conversational
* text-to-speech
* automatic-speech-recognition
* audio-source-seperation
* voice-activity-detection

class FLAIR_TASKS[source]

FLAIR_TASKS(*args, **kwargs)

A list of all Flair tasks for valid API lookup as attribtues to get tab-completion and typo-proofing

Usage Examples:

Sample code from: 02_model_hub.ipynb (View Notebook for more context)

mk_class('FLAIR_TASKS', **_flair_tasks,
        doc="A list of all Flair tasks for valid API lookup as attribtues to get tab-completion and typo-proofing")

Sample code from: 02_model_hub.ipynb (View Notebook for more context)

_all_ = ['FLAIR_TASKS']
Possible tasks:
* ner
* chunk
* frame
* pos
* upos
* embeddings

class HFModelResult[source]

HFModelResult(model_info:ModelInfo)

A very basic class for storing a HuggingFace model returned through an API request

They have 4 properties:

  • name: The modelId from the modelInfo. This also includes the model author's name, such as "IlyaGusev/mbart_ru_sum_gazeta"
  • tags: Any tags that were included in HugginFace in relation to the model.
  • tasks: These are the tasks dictated for the model.

We look inside of modelInfo.pipeline_tag as well as the tags for if there is any overlap

HFModelResult.to_dict[source]

HFModelResult.to_dict()

Returns HFModelResult as a dictionary with the keys:

  • model_name
  • tags
  • tasks
  • model_info

class HFModelHub[source]

HFModelHub(username=None, password=None)

A class for interacting with the HF model hub API, and searching for models by name or task

Can optionally include your HuggingFace login for authorized access (but is not required)

Usage Examples:

Sample code from: 09a_tutorial.easy_text_generator.ipynb (View Notebook for more context)

hub = HFModelHub()
models = hub.search_model_by_task('text-generation'); models

Sample code from: 04_embeddings.ipynb (View Notebook for more context)

_flair_hub = FlairModelHub()
_hf_hub = HFModelHub()

Sample code from: 05a_tutorial.token_tagging.ipynb (View Notebook for more context)

hub = HFModelHub()
model = hub.search_model_by_name('sshleifer/tiny-dbmdz-bert', user_uploaded=True)[0]; model

The model search hub creates a friendly end-user API when searching through HuggingFace (and Flair, as we will see later). Usage is extremely simple as well.

HFModelHub.search_model_by_task[source]

HFModelHub.search_model_by_task(task:str, as_dict=False, user_uploaded=False)

Searches HuggingFace Model API for all pretrained models relating to task and returns a list of HFModelResults

Optionally can return all models as a dict rather than a list

If user_uploaded is False, will only return models originating in HuggingFace (such as distilgpt2)

Usage:

hub = HFModelHubSearch()
    hub.search_model_by_task('summarization')
    # OR #
    hub.search_model_by_task(HF_TASKS.SUMMARIZATION)

Usage Examples:

Sample code from: 08_translation.ipynb (View Notebook for more context)

hub = HFModelHub()
model = hub.search_model_by_task("translation")[-1]
translations = translator.translate(text = text, t5_prefix="translate English to German", model_name_or_path=model, mini_batch_size=1, min_length=0, max_length=100, early_stopping=True)
test_eq(translations, ['Das Maschinenlernen wird die Welt in Kürze übernehmen.',
 'Maschinen können in vielen Sprachen sprechen.'])

Sample code from: 09a_tutorial.easy_text_generator.ipynb (View Notebook for more context)

hub = HFModelHub()
models = hub.search_model_by_task('text-generation'); models

More in depth examples are below:

There are also cases where a dict may be easier to work with (perhaps utilizing a network API, or ease of use for some). We can instead return a dictionary of HFModelResult objects too by passing as_dict=True to any search call:

models = hub.search_model_by_task('summarization', as_dict=True);
models['t5-11b']
{'model_name': 't5-11b',
 'tags': ['pytorch',
  'tf',
  't5',
  'lm-head',
  'seq2seq',
  'en',
  'fr',
  'ro',
  'de',
  'dataset:c4',
  'arxiv:1910.10683',
  'transformers',
  'summarization',
  'translation',
  'license:apache-2.0',
  'text2text-generation'],
 'tasks': ['summarization', 'text2text-generation', 'translation'],
 'model_info': <huggingface_hub.hf_api.ModelInfo at 0x7fc1f2c7dca0>}

This will return a dictionary of the name, the HuggingFace tags affiliated with the model, the dictated tasks, and an instance of huggingface_hub's ModelInfo.

HFModelHub.search_model_by_name[source]

HFModelHub.search_model_by_name(name:str, as_dict=False, user_uploaded=False)

Searches HuggingFace Model API for all pretrained models containing name and returns a list of HFModelResults

Optionally can return all models as dict rather than a list

If user_uploaded is False, will only return models originating from HuggingFace (such as distilgpt2)

Usage:

hub = HFModelHubSearch()
  hub.search_model_by_name('gpt2')

Usage Examples:

Sample code from: 05a_tutorial.token_tagging.ipynb (View Notebook for more context)

hub = HFModelHub()
model = hub.search_model_by_name('sshleifer/tiny-dbmdz-bert', user_uploaded=True)[0]; model

Sample code from: 04a_tutorial.embeddings.ipynb (View Notebook for more context)

hub = HFModelHub()
models = hub.search_model_by_name('gpt2'); models

Sample code from: 06_sequence_classification.ipynb (View Notebook for more context)

hub = HFModelHub()
model = hub.search_model_by_name("nlptown/bert-base", user_uploaded=True)[0]
classifier = EasySequenceClassifier()
sentences = classifier.tag_text(text=example_text, 
                               model_name_or_path=model,
                               mini_batch_size=1)
for pred, truth in zip(preds, truth_lbls):
    test_eq(pred.value, truth.value)
    test_close(pred.score, truth.score, 1e-4)

With search_model_by_name you're allowed a bit more freedom in what you wish to search for. search_model_by_name downloads the entire list of models from HuggingFace then performs partial string matching. As a result you can search for all models by a particular user by doing:

hub.search_model_by_name('Callidior', user_uploaded=True)
[Model Name: Callidior/bert2bert-base-arxiv-titlegen, Tasks: [summarization]]

Or (as implied by the function name) any model type itself:

hub.search_model_by_name('gpt2', user_uploaded=True)[5:10]
[Model Name: 850886470/xxy_gpt2_chinese, Tasks: [],
 Model Name: AlekseyKorshuk/gpt2-wikitext2, Tasks: [],
 Model Name: ComCom-Dev/gpt2-bible-test, Tasks: [],
 Model Name: DHBaek/gpt2-stackoverflow-question-contents-generator, Tasks: [text-generation],
 Model Name: DeepESP/gpt2-spanish, Tasks: [text-generation]]

Flair has a series of extra models available for use that are not available through HuggingFace such as sentiment, communicative-functions, and more. FLAIR_MODELS is a convience holder for quick lookup of these models (as no such list is easily available currently). When shown as results on the API they will be given the same flair prefix for convience.

class FlairModelResult[source]

FlairModelResult(model_info:ModelInfo) :: HFModelResult

A version of HFModelResult for Flair specifically.

Includes which backend the model was found (such as on HuggingFace or Flair's private model list)

class FlairModelHub[source]

FlairModelHub(username=None, password=None)

A class for interacting with the HF model hub API, and searching for Flair models by name or task

Can optionally include your HuggingFace login for authorized access (but is not required)

Usage Examples:

Sample code from: 04_embeddings.ipynb (View Notebook for more context)

_flair_hub = FlairModelHub()
_hf_hub = HFModelHub()

Sample code from: 02_model_hub.ipynb (View Notebook for more context)

hub = FlairModelHub()

FlairModelHub is extremely similar to HFModelHub, with the two differences being that it will only return Flair models, and it has access to the other Flair models available that can't be accessed through the HuggingFace model hub

hub = FlairModelHub()

FlairModelHub.search_model_by_name[source]

FlairModelHub.search_model_by_name(name:str, as_dict=False, user_uploaded=False)

Searches HuggingFace Model API for all flair models containing name and returns a list of HFModelResults

Optionally can return all models as dict rather than a list

If user_uploaded is False, will only return models originating from Flair (such as flair/chunk-english-fast)

Usage:

hub = FlairModelHubSearch()
  hub.search_model_by_name('flair/chunk-english-fast')

seach_model_by_name will also let you search for models without needing the flair prefix, such as:

hub.search_model_by_name('sentiment')
[Model Name: flair/sentiment, Tasks: [text-classification], Source: Flair's Private Model Hub,
 Model Name: flair/en-sentiment, Tasks: [text-classification], Source: Flair's Private Model Hub,
 Model Name: flair/sentiment-fast, Tasks: [text-classification], Source: Flair's Private Model Hub]

FlairModelHub.search_model_by_task[source]

FlairModelHub.search_model_by_task(task:str, as_dict=False, user_uploaded=False)

Searches HuggingFace Model API for all flair models for task and returns a list of HFModelResults

Optionally can return all models as dict rather than a list

If user_uploaded is False, will only return models originating from Flair (such as flair/chunk-english-fast)

Usage:

hub = FlairModelHubSearch()
    hub.search_model_by_task('ner')
    # OR: #
    hub.search_model_by_task(FLAIR_TASKS.NAMED_ENTITY_RECOGNITION)

Since we have a FLAIR_TASKS object declared earlier, we can utilize it when searching for models by a task. Similar to search_model_by_name you should not include flair/ in your search results, and instead search through the task key such as ner or FLAIR_TASKS.NAMED_ENTITY_RECOGNITION