from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
spam = [
'Free money! Click here now!',
"You've won a prize! Claim it today!",
'Limited offer: Buy now and save 90%!',
'URGENT: Your account has been compromised',
'Make $5000 a week working from home',
'Congratulations! You are the winner!',
'Get rich quick with this simple trick',
'Special discount for you only today',
]
not_spam = [
'Hey, are you coming to the meeting tomorrow?',
'Can you pick up some groceries on your way home?',
'The project report is due on Friday',
'Happy birthday! Hope you have a great day',
"Let's catch up over coffee this week",
'Thanks for your help with the homework',
'The match starts at 7pm tonight',
'See you at school tomorrow',
]
texts = spam + not_spam
labels = ['Spam'] * len(spam) + ['Not Spam'] * len(not_spam)
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(texts)
model = MultinomialNB()
model.fit(X, labels)
print('✓ Spam detector trained on', len(texts), 'examples')AI in Action
You’ve just learned about three types of AI. Now let’s see them working — right here in the browser.
Run each section to see Supervised ML, Computer Vision, and Generative AI in action.
What You’ll Explore
- Part 1 — Supervised ML: A model trained to detect spam messages
- Part 2 — Computer Vision: A model that identifies objects through your webcam
- Part 3 — Generative AI: An AI that answers your questions in different styles
Part 1: Supervised ML — Spam Detector
Spam detectors are one of the most common uses of supervised ML. Engineers collected thousands of emails labelled Spam or Not Spam, then trained a model to learn the difference.
The model below was trained on 16 example messages. Run the cell to train it.
The model is trained. Type any message below and run the cell to see what it predicts.
MESSAGE = "You've won a free iPhone!" #@param {type:"string"}
X_new = vectorizer.transform([MESSAGE])
prediction = model.predict(X_new)[0]
proba = model.predict_proba(X_new)[0]
confidence = max(proba)
emoji = '🚨' if prediction == 'Spam' else '✅'
print(f'{emoji} {prediction} (confidence: {confidence:.0%})')Challenge: Can you write a spam message that fools the detector into thinking it’s not spam? Or a normal message it incorrectly labels as spam?
What kinds of words do you think the model learned to associate with spam?
Part 2: Computer Vision — What Can the Camera See?
The model in the next cell can detect 80 different types of objects — from people and laptops to chairs, cups, and phones. It was trained on millions of labelled images.
Click Allow when your browser asks for webcam access, then point the camera around the room. Press Stop (■) when you’re done.
import cv
import graphics
import time
canvas = graphics.canvas()
camera = cv.start_camera(canvas)
detector = cv.start_object_detector(camera)
try:
while True:
detections = detector.get_detections()
canvas.draw_bounding_boxes(detections)
time.sleep(0.033)
finally:
detector.stop()
camera.stop()
print('Camera stopped.')Challenge: Find an object the model labels incorrectly, or something it completely fails to detect. What does that tell you about how the model was trained?
What was the most surprising thing the model detected — or failed to detect?
Part 3: Generative AI — Ask an AI
Unlike the spam detector, this AI doesn’t just predict a label — it generates a response. Type any topic, choose a style, and run the cell.
import openai
import os
TOPIC = "How do solar panels work?" #@param {type:"string"}
STYLE = "simple" #@param ["simple", "detailed", "funny"]
client = openai.OpenAI(
base_url='https://openrouter.ai/api/v1',
api_key=os.environ['OPENROUTER_API_KEY']
)
response = client.chat.completions.create(
model='openai/gpt-oss-120b:free',
messages=[
{'role': 'system', 'content': f'You are a helpful teacher. Explain things in a {STYLE} way. Keep your answer to 3-4 sentences.'},
{'role': 'user', 'content': TOPIC},
]
)
print(response.choices[0].message.content)Challenge: Ask it something factual and check if the answer is correct. Can you find something it gets wrong or makes up?
How did changing the style change the response? Which style was most useful — and why?
Check for Understanding
{ “question_type”: “multiple_choice”, “question”: “Which type of AI would you use to predict tomorrow’s temperature from today’s weather data?”, “options”: [ { “key”: “a”, “text”: “Computer Vision” }, { “key”: “b”, “text”: “Supervised ML (regression)” }, { “key”: “c”, “text”: “Generative AI” }, { “key”: “d”, “text”: “Object detection” } ], “answer”: “b”, “submitted_answer”: “” }
{ “question_type”: “true_false”, “question”: “A computer vision model understands what it sees the same way a human does.”, “answer”: “False”, “submitted_answer”: “” }
{ “question_type”: “multiple_choice”, “question”: “What is the main difference between supervised ML and generative AI?”, “options”: [ { “key”: “a”, “text”: “Supervised ML uses cameras; generative AI does not” }, { “key”: “b”, “text”: “Supervised ML predicts from fixed categories; generative AI creates new content” }, { “key”: “c”, “text”: “Generative AI is always more accurate” }, { “key”: “d”, “text”: “Supervised ML only works with numbers” } ], “answer”: “b”, “submitted_answer”: “” }