An educational Flask application from IBM's Developing AI Applications with Python and Flask course. The application analyzes English text, returns five emotion scores, and identifies the dominant emotion.
The default backend calls the course-hosted Watson NLP endpoint. A separate keyword heuristic is available for offline interface and test demonstrations; it must be selected explicitly and never silently replaces a failed network model.
- Flask interface and JSON endpoint
- Normalized scores for anger, disgust, fear, joy, and sadness
- Dominant-emotion selection
- Explicit Watson and offline-demo backend modes
- Clear
400responses for invalid input - Clear
503responses when the configured network service is unavailable - pytest coverage for every demo emotion, route behavior, invalid input, and network failure
- GitHub Actions continuous integration
Browser or API client
│
▼
Flask application (`server.py`)
│
▼
Emotion service (`emotion_detection.py`)
├── Watson course endpoint
└── explicit offline heuristic
emotion_detector calls the selected backend without silently changing models. emotion_predictor validates the response, normalizes the five required scores, and selects the highest-scoring emotion.
Requires Python 3.11 or newer.
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
python -m pip install -r requirements.txtStart the application with the course-hosted Watson endpoint:
python server.pyFor an explicit offline interface demonstration:
EMOTION_BACKEND=heuristic python server.pyOn Windows PowerShell:
$env:EMOTION_BACKEND = "heuristic"
python server.pyOpen http://127.0.0.1:5000.
Send text to:
GET /emotionDetector?textToAnalyze=I%20love%20this%20project
Example:
curl --get \
--data-urlencode "textToAnalyze=I love this project" \
http://127.0.0.1:5000/emotionDetectorSuccessful responses contain:
scores: anger, disgust, fear, joy, and sadnessdominant_emotion: the emotion with the highest score
The offline heuristic is deterministic test/demo behavior, not a trained NLP model.
pytestThe test suite verifies:
- All five explicit heuristic outcomes
- Network failures are not silently replaced
- Flask route behavior in demo mode
- Empty input returns
400
The same suite runs in GitHub Actions on pushes to main and pull requests.
server.py Flask entry point and HTTP routes
emotion_detection.py backend selection, validation, and prediction logic
test_emotion_detection.py service and route regression tests
templates/ browser interface
static/ interface assets
requirements.txt pinned runtime and test dependencies
The starter structure is based on IBM Skills Network's oaqjp-final-project-emb-ai course repository.
This repository is:
- A learning exercise
- Not an original emotion-classification model
- Not a production or high-availability service
- Dependent on a course-hosted endpoint when using the default backend
The repository retains its existing Apache-2.0 license.