Close Menu
  • Breaking News
  • Business
  • Career
  • Sports
  • Climate
  • Science
    • Tech
  • Culture
  • Health
  • Lifestyle
  • Facebook
  • Instagram
  • TikTok
Categories
  • Breaking News (5,055)
  • Business (313)
  • Career (4,289)
  • Climate (213)
  • Culture (4,256)
  • Education (4,473)
  • Finance (203)
  • Health (857)
  • Lifestyle (4,142)
  • Science (4,160)
  • Sports (318)
  • Tech (174)
  • Uncategorized (1)
Hand Picked

Study shows UNL will cut teaching jobs while non-teaching salaries surge 32%

October 31, 2025

Ghosts of gains past for markets this Halloween

October 31, 2025

Einstein’s Relativity And Hypothetical Particles Tested With Record-Breaking Black Hole Collisions

October 31, 2025

Carrie Underwood says she is ‘truly blessed’ as she breaks record held by Shania Twain

October 31, 2025
Facebook X (Twitter) Instagram
  • About us
  • Contact us
  • Disclaimer
  • Privacy Policy
  • Terms and services
Facebook X (Twitter) Instagram
onlyfacts24
  • Breaking News

    Ghosts of gains past for markets this Halloween

    October 31, 2025

    Lamar Jackson throws 4 touchdowns in thrilling Ravens return vs Dolphins

    October 31, 2025

    Russia-Ukraine war: List of key events, day 1,345 | Vladimir Putin News

    October 31, 2025

    Hang Seng Index, Nifty 50, CSI 300

    October 31, 2025

    Fox News ‘Antisemitism Exposed’ Newsletter: Will New York City’s next mayor hate Jews?

    October 30, 2025
  • Business

    Global Topic: Panasonic’s environmental solutions in China—building a sustainable business model | Business Solutions | Products & Solutions | Topics

    October 29, 2025

    Google Business Profile New Report Negative Review Extortion Scams

    October 23, 2025

    Land Topic is Everybody’s Business

    October 20, 2025

    Global Topic: Air India selects Panasonic Avionics’ Astrova for 34 widebody aircraft | Business Solutions | Products & Solutions | Topics

    October 19, 2025

    Business Engagement | IUCN

    October 14, 2025
  • Career

    Carrie Underwood says she is ‘truly blessed’ as she breaks record held by Shania Twain

    October 31, 2025

    One Small Step Forward: Experiential Learning for a Changing Career

    October 31, 2025

    Students make meaningful connections at Fall Career and Graduate School Fair

    October 31, 2025

    ASU named among the world’s best universities for graduate employability

    October 31, 2025

    DOJ Scrubs Jan. 6 Attack From Court Record After Suspending Career Prosecutors

    October 30, 2025
  • Sports

    OKC Thunder exercises options on Nikola Topic, Cason Wallace

    October 31, 2025

    OKC Thunder guard Topic, 20, diagnosed with cancer

    October 31, 2025

    Sports Illustrated – Thunder Guard Nikola Topic…

    October 31, 2025

    Oklahoma City Thunder guard Nikola Topic diagnosed with testicular cancer

    October 30, 2025

    Raiders DE Maxx Crosby Weighs In on Sports’ Hottest Topic

    October 30, 2025
  • Climate

    PA Environment & Energy Articles & NewsClips By Topic

    October 26, 2025

    important environmental topics 2024| Statista

    October 21, 2025

    World BankDevelopment TopicsProvide sustainable food systems, water, and economies for healthy people and a healthy planet. Agriculture · Agribusiness and Value Chains · Climate-Smart….2 days ago

    October 20, 2025

    PA Environment & Energy Articles & NewsClips By Topic

    October 17, 2025

    World Bank Group and the Intergovernmental Negotiating Committee on Plastic Pollution Process

    October 14, 2025
  • Science
    1. Tech
    2. View All

    It is a hot topic as Grok and DeepSeek overwhelmed big tech AI models such as ChatGPT and Gemini in ..

    October 24, 2025

    Countdown to the Tech.eu Summit London 2025: Key Topics, Speakers, and Opportunities

    October 23, 2025

    The High-Tech Agenda of the German government

    October 20, 2025

    Texas Tech Universities Ban Teaching About Transgender and Other Gender Topics

    October 19, 2025

    Einstein’s Relativity And Hypothetical Particles Tested With Record-Breaking Black Hole Collisions

    October 31, 2025

    SpaceX Falcon Heavy launch of private Griffin moon lander delayed to 2026

    October 31, 2025

    Dozens of new species, including carnivorous ‘death ball’ sponge, discovered in Southern Ocean

    October 31, 2025

    Evidence for improved DNA repair in long-lived bowhead whale

    October 31, 2025
  • Culture

    Talking Music and Culture-Making with East Forest

    October 31, 2025

    Louisville Public MediaArts, Culture, Et Cetera: Happy Halloween!It's time for Arts, Culture, Et Cetera, where LPM News' Giselle Rhoden shares what's new in arts and culture around Louisville..7 hours ago

    October 31, 2025

    One UK town to win £3.5 million in new government competition

    October 31, 2025

    ‘Bloodbath’ at CBS News: Anti-Israel ‘Race and Culture’ Unit is Gutted, Digital Show That Said Motive for Kirk’s Killing ‘Elusive’ Is Axed

    October 31, 2025

    Take this week’s American Culture Quiz and test your knowledge of haunted hollows and more

    October 30, 2025
  • Health

    Thunder GM Sam Presti shares gut-wrenching Nikola Topic health news

    October 30, 2025

    Nikola Topic Diagnosed with Cancer: What We Know About the Oklahoma City Thunder Rookie’s Health Condition | US News

    October 30, 2025

    What happened to Nikola Topic? Oklahoma City Thunder guard reveals health scare

    October 30, 2025

    Breast Cancer Awareness Month 2025

    October 26, 2025

    Hampton: Community Encouraged To Attend November Los Alamos County Health Council Meeting

    October 24, 2025
  • Lifestyle
Contact
onlyfacts24
Home»Science»Choosing and Implementing Hugging Face Models | by Stephanie Kirmer | Nov, 2024
Science

Choosing and Implementing Hugging Face Models | by Stephanie Kirmer | Nov, 2024

November 3, 2024No Comments
Facebook Twitter Pinterest LinkedIn Tumblr Email
11pjaci7 Nq58azzsw73zaq.png
Share
Facebook Twitter LinkedIn Pinterest Email

Pulling pre-trained models out of the box for your use case

Stephanie Kirmer

Towards Data Science

Photo by Erda Estremera on Unsplash

I’ve been having a lot of fun in my daily work recently experimenting with models from the Hugging Face catalog, and I thought this might be a good time to share what I’ve learned and give readers some tips for how to apply these models with a minimum of stress.

My specific task recently has involved looking at blobs of unstructured text data (think memos, emails, free text comment fields, etc) and classifying them according to categories that are relevant to a business use case. There are a ton of ways you can do this, and I’ve been exploring as many as I can feasibly do, including simple stuff like pattern matching and lexicon search, but also expanding to using pre-built neural network models for a number of different functionalities, and I’ve been moderately pleased with the results.

I think the best strategy is to incorporate multiple techniques, in some form of ensembling, to get the best of the options. I don’t trust these models necessarily to get things right often enough (and definitely not consistently enough) to use them solo, but when combined with more basic techniques they can add to the signal.

For me, as I’ve mentioned, the task is just to take blobs of text, usually written by a human, with no consistent format or schema, and try to figure out what categories apply to that text. I’ve taken a few different approaches, outside of the analysis methods mentioned earlier, to do that, and these range from very low effort to somewhat more work on my part. These are three of the strategies that I’ve tested so far.

  • Ask the model to choose the category (zero-shot classification — I’ll use this as an example later on in this article)
  • Use a named entity recognition model to find key objects referenced in the text, and make classification based on that
  • Ask the model to summarize the text, then apply other techniques to make classification based on the summary

This is some of the most fun — looking through the Hugging Face catalog for models! At https://huggingface.co/models you can see a gigantic assortment of the models available, which have been added to the catalog by users. I have a few tips and pieces of advice for how to select wisely.

  • Look at the download and like numbers, and don’t choose something that has not been tried and tested by a decent number of other users. You can also check the Community tab on each model page to see if users are discussing challenges or reporting bugs.
  • Investigate who uploaded the model, if possible, and determine if you find them trustworthy. This person who trained or tuned the model may or may not know what they’re doing, and the quality of your results will depend on them!
  • Read the documentation closely, and skip models with little or no documentation. You’ll struggle to use them effectively anyway.
  • Use the filters on the side of the page to narrow down to models suited to your task. The volume of choices can be overwhelming, but they are well categorized to help you find what you need.
  • Most model cards offer a quick test you can run to see the model’s behavior, but keep in mind that this is just one example and it’s probably one that was chosen because the model’s good at that and finds this case pretty easy.

Once you’ve found a model you’d like to try, it’s easy to get going- click the “Use this Model” button on the top right of the Model Card page, and you’ll see the choices for how to implement. If you choose the Transformers option, you’ll get some instructions that look like this.

Screenshot taken by author

If a model you’ve selected is not supported by the Transformers library, there may be other techniques listed, like TF-Keras, scikit-learn, or more, but all should show instructions and sample code for easy use when you click that button.

In my experiments, all the models were supported by Transformers, so I had a mostly easy time getting them running, just by following these steps. If you find that you have questions, you can also look at the deeper documentation and see full API details for the Transformers library and the different classes it offers. I’ve definitely spent some time looking at these docs for specific classes when optimizing, but to get the basics up and running you shouldn’t really need to.

Ok, so you’ve picked out a model that you want to try. Do you already have data? If not, I have been using several publicly available datasets for this experimentation, mainly from Kaggle, and you can find lots of useful datasets there as well. In addition, Hugging Face also has a dataset catalog you can check out, but in my experience it’s not as easy to search or to understand the data contents over there (just not as much documentation).

Once you pick a dataset of unstructured text data, loading it to use in these models isn’t that difficult. Load your model and your tokenizer (from the docs provided on Hugging Face as noted above) and pass all this to the pipeline function from the transformers library. You’ll loop over your blobs of text in a list or pandas Series and pass them to the model function. This is essentially the same for whatever kind of task you’re doing, although for zero-shot classification you also need to provide a candidate label or list of labels, as I’ll show below.

So, let’s take a closer look at zero-shot classification. As I’ve noted above, this involves using a pretrained model to classify a text according to categories that it hasn’t been specifically trained on, in the hopes that it can use its learned semantic embeddings to measure similarities between the text and the label terms.

from transformers import AutoModelForSequenceClassification
from transformers import AutoTokenizer
from transformers import pipeline

nli_model = AutoModelForSequenceClassification.from_pretrained("facebook/bart-large-mnli", model_max_length=512)
tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-mnli")
classifier = pipeline("zero-shot-classification", device="cpu", model=nli_model, tokenizer=tokenizer)

label_list = ['News', 'Science', 'Art']

all_results = []
for text in list_of_texts:
prob = self.classifier(text, label_list, multi_label=True, use_fast=True)
results_dict = {x: y for x, y in zip(prob["labels"], prob["scores"])}
all_results.append(results_dict)

This will return you a list of dicts, and each of those dicts will contain keys for the possible labels, and the values are the probability of each label. You don’t have to use the pipeline as I’ve done here, but it makes multi-label zero shot a lot easier than manually writing that code, and it returns results that are easy to interpret and work with.

If you prefer to not use the pipeline, you can do something like this instead, but you’ll have to run it once for each label. Notice how the processing of the logits resulting from the model run needs to be specified so that you get human-interpretable output. Also, you still need to load the tokenizer and the model as described above.

def run_zero_shot_classifier(text, label):
hypothesis = f"This example is related to {label}."

x = tokenizer.encode(
text,
hypothesis,
return_tensors="pt",
truncation_strategy="only_first"
)

logits = nli_model(x.to("cpu"))[0]

entail_contradiction_logits = logits[:, [0, 2]]
probs = entail_contradiction_logits.softmax(dim=1)
prob_label_is_true = probs[:, 1]

return prob_label_is_true.item()

label_list = ['News', 'Science', 'Art']
all_results = []
for text in list_of_texts:
for label in label_list:
result = run_zero_shot_classifier(text, label)
all_results.append(result)

You probably have noticed that I haven’t talked about fine tuning the models myself for this project — that’s true. I may do this in future, but I’m limited by the fact that I have minimal labeled training data to work with at this time. I can use semisupervised techniques or bootstrap a labeled training set, but this whole experiment has been to see how far I can get with straight off-the-shelf models. I do have a few small labeled data samples, for use in testing the models’ performance, but that’s nowhere near the same volume of data I will need to tune the models.

If you do have good training data and would like to tune a base model, Hugging Face has some docs that can help. https://huggingface.co/docs/transformers/en/training

Performance has been an interesting problem, as I’ve run all my experiments on my local laptop so far. Naturally, using these models from Hugging Face will be much more compute intensive and slower than the basic strategies like regex and lexicon search, but it provides signal that can’t really be achieved any other way, so finding ways to optimize can be worthwhile. All these models are GPU enabled, and it’s very easy to push them to be run on GPU. (If you want to try it on GPU quickly, review the code I’ve shown above, and where you see “cpu” substitute in “cuda” if you have a GPU available in your programming environment.) Keep in mind that using GPUs from cloud providers is not cheap, however, so prioritize accordingly and decide if more speed is worth the price.

Most of the time, using the GPU is much more important for training (keep it in mind if you choose to fine tune) but less vital for inference. I’m not digging in to more details about optimization here, but you’ll want to consider parallelism as well if this is important to you- both data parallelism and actual training/compute parallelism.

We’ve run the model! Results are here. I have a few closing tips for how to review the output and actually apply it to business questions.

  • Don’t trust the model output blindly, but run rigorous tests and evaluate performance. Just because a transformer model does well on a certain text blob, or is able to correctly match text to a certain label regularly, doesn’t mean this is generalizable result. Use lots of different examples and different kinds of text to prove the performance is going to be sufficient.
  • If you feel confident in the model and want to use it in a production setting, track and log the model’s behavior. This is just good practice for any model in production, but you should keep the results it has produced alongside the inputs you gave it, so you can continually check up on it and make sure the performance doesn’t decline. This is more important for these kinds of deep learning models because we don’t have as much interpretability of why and how the model is coming up with its inferences. It’s dangerous to make too many assumptions about the inner workings of the model.

As I mentioned earlier, I like using these kinds of model output as part of a larger pool of techniques, combining them in ensemble strategies — that way I’m not only relying on one approach, but I do get the signal those inferences can provide.

I hope this overview is useful for those of you getting started with pre-trained models for text (or other mode) analysis — good luck!

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email

Related Posts

Einstein’s Relativity And Hypothetical Particles Tested With Record-Breaking Black Hole Collisions

October 31, 2025

SpaceX Falcon Heavy launch of private Griffin moon lander delayed to 2026

October 31, 2025

Dozens of new species, including carnivorous ‘death ball’ sponge, discovered in Southern Ocean

October 31, 2025

Evidence for improved DNA repair in long-lived bowhead whale

October 31, 2025
Add A Comment
Leave A Reply Cancel Reply

Latest Posts

Study shows UNL will cut teaching jobs while non-teaching salaries surge 32%

October 31, 2025

Ghosts of gains past for markets this Halloween

October 31, 2025

Einstein’s Relativity And Hypothetical Particles Tested With Record-Breaking Black Hole Collisions

October 31, 2025

Carrie Underwood says she is ‘truly blessed’ as she breaks record held by Shania Twain

October 31, 2025
News
  • Breaking News (5,055)
  • Business (313)
  • Career (4,289)
  • Climate (213)
  • Culture (4,256)
  • Education (4,473)
  • Finance (203)
  • Health (857)
  • Lifestyle (4,142)
  • Science (4,160)
  • Sports (318)
  • Tech (174)
  • Uncategorized (1)

Subscribe to Updates

Get the latest news from onlyfacts24.

Follow Us
  • Facebook
  • Instagram
  • TikTok

Subscribe to Updates

Get the latest news from ONlyfacts24.

News
  • Breaking News (5,055)
  • Business (313)
  • Career (4,289)
  • Climate (213)
  • Culture (4,256)
  • Education (4,473)
  • Finance (203)
  • Health (857)
  • Lifestyle (4,142)
  • Science (4,160)
  • Sports (318)
  • Tech (174)
  • Uncategorized (1)
Facebook Instagram TikTok
  • About us
  • Contact us
  • Disclaimer
  • Privacy Policy
  • Terms and services
© 2025 Designed by onlyfacts24

Type above and press Enter to search. Press Esc to cancel.