Huggingface Transformers
✕Hugging Face Concepts
- Leading platform for
NLP,Image,Audiomodels. - Provides pre-trained models for various tasks that can be used in applications.
- Supports tasks like text classification, generation, summarization, question answering, etc.
- Models can be fine-tuned on custom datasets for specific use cases.
- Sign up at Hugging Face to access models and resources.
Walking Through Website and Model Hub
- Explore the Hugging Face Model Hub via Top bar.
- Walk through task sections to see relevant models.
- Search for models based on task and language.
- Click on a model to see details, usage instructions, and example code.
- Install the transformers library:
pip install transformers==4.57.6 huggingface-hub==0.36.2
Using Models with High-Level Pipelines
- Hugging Face provides high-level pipelines for common NLP tasks.
- Pipelines abstract away model loading and preprocessing steps.
from transformers import pipeline text_classifier = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english") response = text_classifier("I love using Hugging Face models!") print(response)
Example: Text Classification Pipeline
Summarization Pipeline Example
from transformers import pipeline summarizer = pipeline("summarization", model="nyamuda/extractive-summarization") article = """The Hugging Face Transformers library provides a wide range of pre-trained models for natural language processing tasks. It supports tasks such as text classification, generation, summarization, and qa.""" summary = summarizer(article, max_length=50, min_length=25) print(summary[0]['summary_text'])
Sample Code:
Question Answering Pipeline Example
from transformers import pipeline qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad") context = "content_of_the_policy_file" question = "How many volunteer days are offered annually?" response = qa_pipeline(question=question, context=context) print(response)
Sample Code:
Practice QuestionsNot started
Text Classification with Hugging Face
Question 1 of 3
- Write a function
is_spam(review_text)that uses text-classification pipeline with a model likedistilbert-base-uncased-finetuned-sst-2-english. If the score isabove 0.9and the label isNEGATIVE, return FLAG FOR REVIEW - Write a function
get_topic(news_snippet)that uses a zero-shot-classification pipeline. The function should take a snippet and a list of labels (e.g., ["politics", "economy", "sports"]) and return the label with the highest confidence score. - Write a function
detect_anger(text)using an emotion-specific model (likej-hartmann/emotion-english-distilrobert-base). If the "anger" score is the highest among all emotions, return True
- Write a function
Summarization with Hugging Face
Question 2 of 3
- Write a function
create_bullet_summary(long_text)that uses the summarization pipeline. Set the max_length to 50 and min_length to 10. The function should return the summary string stripped of any leading or trailing whitespace. - Write a function
summarize_csv_column(df, column_name)that takes a pandas DataFrame, iterates through the first 5 rows of a specific text column, and returns a Python list containing the summarized version of each row. - Write a function
dynamic_summarize(text)that checks the length of the input. If the text is more than 500 words, use the pipeline to compress it; if it is less than 500 words, return the original text as-is.
- Write a function
Question Answering with Hugging Face
Question 3 of 3
- Write a function
get_answer(context_text, question)that uses the "question-answering" pipeline. Test it by passing a paragraph about a company's fiscal year and asking, "What was the total revenue?" Return only the answer string from the output dictionary. - Write a function
verified_answer(context, question)that performs question-answering. If the score returned by the model is less than 0.5, the function should return "I am not confident in the answer" instead of the model's text. - Create a function
find_date(report_text)that specifically asks the model "On what date was this document signed?" and returns the start and end indices (start and end keys) of where that answer was found in the original text.
- Write a function
