Project

Run a pretrained model, and check its claim

You build a project that downloads a sentiment model and its test sentences from the Hugging Face Hub, checks the accuracy the model's page claims, and tries the model on reviews you wrote yourself.

About 3 hours. You need your own computer, with uv; about 1 GB of disk for the packages, the model and the sentences.

You build it in the starter, one step at a time. The finished project is there to read when you are stuck.

Terminal
git clone https://github.com/FaydenAI/python-for-ai.git
cd python-for-ai/project/starter
uv sync

Get the starterRead the finished project

  1. Part 1

    Read the model's card

    Before you download a model, read what its page says: what it does, its licence, and how good its makers say it is. You read it from the Hub's API, the way the course read GPT-2's entry.

    UsesNotebooks and environmentsYour first lines of PythonDicts and functionsLists, loops and ifCalling an API

    1. Run the testsRun it
    2. The task, the licence and the downloadsYour turn
    3. What the card claimsYour turn
    4. Ask the HubRun it

    When you have run it

    The card gives two accuracies. Which one tells you how the model does on sentences it has never seen?

  2. Part 2

    Ask the model

    Now download the model, and ask it about a sentence. A model reads numbers, not words, so its tokenizer turns the words into numbers first, as the course's `encode` did. The model's answer comes back as a tensor.

    UsesDicts and functionsClasses, just enoughPyTorch tensors and their shapesLists, loops and if

    1. Texts of different lengthsYour turn
    2. The model's answerYour turn
    3. Three sentencesRun it

    When you have run it

    What does the model answer for "the cat ran", a sentence with no feeling in it?

  3. Part 3

    Check the claim

    Now the check itself. Run the model on the 872 sentences of the validation split, count how often it is right, and set that beside the card's 0.9106. Then look at where it goes wrong, and how sure it was there.

    UsesNumPy arraysVectorization and broadcastingTables with pandasPlotting to see dataThe scikit-learn patternFrom a notebook to a script

    1. In batchesYour turn
    2. How often it is rightYour turn
    3. A baselineYour turn
    4. The surest mistakesYour turn
    5. How sure it was when it was wrongYour turn
    6. The checkRun it

    When you have run it

    In your run of main.py, how many of the wrong answers had a chance over 0.9?

  4. Part 4

    Your own reviews

    This part is yours. Write reviews of your own, of a film, a game, a meal or anything you have an opinion on, and see how the model does on sentences that are not from its test. There is no starter code: `mine.py` and `my_reviews.csv` only say what goes in them.

    UsesTables with pandasFrom a notebook to a scriptGit and GitHub

    1. Your reviewsOn your own
    2. A script for your fileOn your own
    3. Fool the modelOn your own
    4. Save it with GitRun it

    When you have run it

    "I expected to hate it, and I did not." is a positive review. What does the model answer? Add it to your file to find out.

Run on Python 3.13, PyTorch 2.15.0, transformers 5.24.1, datasets 5.2.0, pandas 3.1.1, scikit-learn 1.9.1. Checked on .