← Back to Portfolio
NLP Fine-Tuned Transformer Hugging Face Gradio Demo

Food Not Food
Classifier

A lightweight NLP model that reads any sentence and determines whether it's talking about food or not — returning a label and a confidence score. Simple premise. Real ML pipeline.

"I love pizza on Friday nights"

Food 0.999

"The stock market dropped today"

Not Food 0.998

Type

Binary Text Classification

Model

DistilBERT (fine-tuned)

Accuracy

100%

F1 Score

1.0

Eval Loss

0.0087

Parameters

~67 Million

Architecture

The Stack

A clean end-to-end pipeline from pre-trained foundation model through fine-tuning to hosted, interactive deployment.

01

DistilBERT (base, uncased)

pre-trained foundation

02

Fine-tuning

Hugging Face Trainer API

03

Sequence Classification Head

Food / Not Food

04

Hosted Model

Hugging Face Hub

05

Gradio Demo

Hugging Face Spaces

Model Choice

Why DistilBERT?

DistilBERT is a distilled (compressed) version of BERT — retaining ~97% of its language understanding at 40% fewer parameters and 60% faster inference.

97%

Language understanding retained vs. BERT

40%

Fewer parameters than BERT

60%

Faster inference speed

  • — Rapid prototyping and learning without sacrificing language understanding
  • — Classification tasks that don't require the full depth of BERT
  • — Deployment in resource-constrained environments
Base model distilbert/distilbert-base-uncased
Training

Training Process

Rather than training from scratch, this project uses transfer learning — taking a model already pre-trained on massive text corpora (Wikipedia, BookCorpus) and adapting it to a specific binary classification task.

Hyperparameters

Parameter Value
Learning Rate 2e-05
Optimizer AdamW (fused) — β=(0.9, 0.999), ε=1e-08
LR Scheduler Linear decay
Train Batch Size 32
Eval Batch Size 32
Epochs 10
Seed 42

Training Results

Epoch Train Loss Val Loss Accuracy F1
1 0.3420 0.2034 1.0 1.0
2 0.2167 0.1099 1.0 1.0
3 0.0960 0.0430 1.0 1.0
5 0.0246 0.0154 1.0 1.0
8 0.0124 0.0093 1.0 1.0
10 ★ 0.0112 0.0087 1.0 1.0

The model reached perfect accuracy on the validation set by epoch 1 and continued refining its confidence through all 10 epochs, with training and validation loss steadily converging toward near-zero.

Demo

The Demo

Built and deployed as a Gradio app on Hugging Face Spaces — no backend required, no server to manage.

How it works

  1. 01 User types any sentence into the input field
  2. 02 The app routes the text through the hosted model via the Hugging Face Inference API
  3. 03 The model returns two labels — Food and Not Food — each with a probability score
  4. 04 Results render instantly in the UI

Live on Hugging Face Spaces

No setup required. Open the demo, type a sentence, and see the classifier run in real time.

Open Live Demo →
Usage

Using the Model

With a Pipeline — Easiest

Python
from transformers import pipeline

classifier = pipeline(
    "text-classification",
    model="DayWalker2226/learn_hf_food_not_food_text_classifier-distilbert-base-uncased"
)

result = classifier("I had tacos for dinner last night")
# → [{'label': 'Food', 'score': 0.999}]

Loading the Model Directly

Python
from transformers import AutoTokenizer, AutoModelForSequenceClassification

tokenizer = AutoTokenizer.from_pretrained(
    "DayWalker2226/learn_hf_food_not_food_text_classifier-distilbert-base-uncased"
)
model = AutoModelForSequenceClassification.from_pretrained(
    "DayWalker2226/learn_hf_food_not_food_text_classifier-distilbert-base-uncased"
)
Reflection

What I Learned

01

End-to-end ML workflow

On Hugging Face: from dataset → tokenization → fine-tuning → model hub → live demo. The full pipeline in one project.

02

Transfer learning in practice

How pre-trained weights dramatically reduce the data and compute needed for task-specific performance.

03

Hugging Face Trainer API

Managing hyperparameters, evaluation loops, and logging with minimal boilerplate.

04

Gradio + Spaces

Spinning up a shareable, interactive ML demo without writing a backend — zero deployment overhead.

05

Model cards and versioning

Publishing reproducible, documented model artifacts that others can build on.