Build a simple chatbot using Python and NLP techniques.
1. Introduction to AI
What is AI?
2. Machine Learning Basics
How Machine Learning Works
3. AI Applications
AI in the Real World
10. Reinforcement Learning
Reinforcement Learning Basics
Syntax
AI
from transformers import pipeline
import random
def chatbot(message):
classifier = pipeline("zero-shot-classification")
result = classifier(message, ["greeting", "question", "goodbye"])
return responses[result["labels"][0]]
Simple Chatbot
PYTHON
import random responses = { "greeting": ["Hello!", "Hi there!", "Hey!"], "goodbye": ["Bye!", "See you!", "Goodbye!"], "thanks": ["You're welcome!", "Happy to help!"], "default": ["Tell me more!", "Interesting!", "I see."] } def get_intent(text): text = text.lower() if any(w in text for w in ["hi", "hello", "hey"]): return "greeting" if any(w in text for w in ["bye", "goodbye"]): return "goodbye" if any(w in text for w in ["thanks", "thank"]): return "thanks" return "default" def chatbot(text): intent = get_intent(text) return random.choice(responses[intent]) print(chatbot("Hello!")) print(chatbot("Thanks for help")) print(chatbot("Goodbye"))
Up next
Image Classifier
Lesson 16 of 17 · AI Projects