Docsity

AI Tutorial for Claude, Sintesi del corso di Machine learning

Toggle sidebar
bg1
■ AI TUTORIAL #1
Build a Simple Sentiment Analyzer
with Python & HuggingFace Transformers
What You'll Learn
Use a pre-trained model to classify text as positive, negative, or neutral — no model training required.
This tutorial shows you how to go from zero to a working classifier in under 10 lines of code.
Prerequisites
• Python 3.8 or later installed
• Basic familiarity with pip and the command line
• No machine learning experience needed!
Step 1 — Install the Library
Open your terminal and run the following command to install HuggingFace Transformers and PyTorch:
pip install transformers torch
Step 2 — Load the Pipeline
HuggingFace's pipeline abstraction lets you load a pre-trained model with a single line. The first run
downloads the model weights (~250 MB) — subsequent runs are instant.
from transformers import pipeline
sentiment = pipeline("sentiment-analysis")
Step 3 — Run Your First Prediction
Pass any string to the pipeline and inspect the result:
result = sentiment("I absolutely loved the product!")
print(result)
# Output:
# [{'label': 'POSITIVE', 'score': 0.9998}]
Step 4 — Batch Process a List
pf2

Anteprima parziale del testo

Scarica AI Tutorial for Claude e più Sintesi del corso in PDF di Machine learning solo su Docsity!

n AI TUTORIAL #

Build a Simple Sentiment Analyzer

with Python & HuggingFace Transformers

What You'll Learn

Use a pre-trained model to classify text as positive , negative , or neutral — no model training required.

This tutorial shows you how to go from zero to a working classifier in under 10 lines of code.

Prerequisites

  • Python 3.8 or later installed
  • Basic familiarity with pip and the command line
  • No machine learning experience needed!

Step 1 — Install the Library

Open your terminal and run the following command to install HuggingFace Transformers and PyTorch:

pip install transformers torch

Step 2 — Load the Pipeline

HuggingFace's pipeline abstraction lets you load a pre-trained model with a single line. The first run

downloads the model weights (~250 MB) — subsequent runs are instant.

from transformers import pipeline

sentiment = pipeline("sentiment-analysis")

Step 3 — Run Your First Prediction

Pass any string to the pipeline and inspect the result:

result = sentiment("I absolutely loved the product!") print(result)

Output:

[{'label': 'POSITIVE', 'score': 0.9998}]

Step 4 — Batch Process a List

You can pass a list of strings to process multiple reviews at once:

reviews = [ "Great quality, fast delivery!", "Terrible experience, never again.", "It was okay, nothing special." ]

results = sentiment(reviews) for r in results: print(r["label"], round(r["score"], 3))

Why This Is Useful

Drop this into a spreadsheet pipeline, Slack bot, or CRM automation to auto-tag customer feedback — no

labelling a single row of training data. Typical real-world applications include support ticket triage, product

review dashboards, and social media monitoring.

Pro tip: To use a multilingual model that supports Portuguese, Spanish, Italian, and German, replace the

pipeline argument with "nlptown/bert-base-multilingual-uncased-sentiment".

AI Tutorial Series · Generated April 2026