A Python Flask web application that detects emotions in text using the IBM Watson NLP Emotion Prediction API. Built for the IBM/Coursera Developing AI Applications with Python and Flask final project.
- Watson NLP emotion analysis (anger, disgust, fear, joy, sadness)
- Dominant emotion detection
- Flask web UI with textarea and Analyze button
- REST endpoint:
/emotionDetector?textToAnalyze=... - Error handling for invalid input and API failures
- Unit tests for five emotion categories
- PEP8-compliant, pylint-ready code
emotion_detection_app/
├── EmotionDetection/
│ ├── __init__.py
│ └── emotion_detection.py # Watson API integration
├── templates/
│ └── index.html # Web UI
├── static/
│ ├── mywebscript.js # RunSentimentAnalysis()
│ └── styles.css
├── server.py # Flask application
├── test_emotion_detection.py # unittest suite
├── requirements.txt
└── README.md
-
User enters text in
index.htmland clicks Analyze Emotion. -
JavaScript
RunSentimentAnalysis()calls/emotionDetector?textToAnalyze=<text>viafetch(). -
Flask route in
server.pycallsemotion_detector(text). -
emotion_detection.pysends arequests.post()to Watson NLP:https://sn-watson-emotion.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict -
JSON response is parsed; dominant emotion is the highest score.
-
Flask returns a formatted plain-text response to the browser.
- Python 3.8 or newer
- Internet access (Watson Skills Network endpoint)
cd emotion_detection_app
pip install -r requirements.txtpython server.py
Open: http://localhost:5000
curl "http://localhost:5000/emotionDetector?textToAnalyze=I%20am%20glad%20this%20happened"Sample response:
For the given statement, the system response is
'anger': 0.001,
'disgust': 0.002,
'fear': 0.003,
'joy': 0.95,
'sadness': 0.01.
The dominant emotion is joy.
Tests call the live Watson API. Ensure you have network connectivity.
python -m unittest test_emotion_detection.py -v
| Input | Expected dominant emotion |
|---|---|
| I am glad this happened | joy |
| I am really mad about this | anger |
| I feel disgusted just hearing about this | disgust |
| I am so sad about this | sadness |
| I am really afraid that this will happen | fear |
Run pylint for PEP8 compliance:
pylint EmotionDetection/emotion_detection.py pylint server.py pylint test_emotion_detection.py
Target score: 10/10
| Condition | Behavior |
|---|---|
| API status 400 | Returns all emotion fields as None |
| Empty or invalid text | Invalid text! Please try again. |
| Network failure | Null emotion result / invalid message |
- Python 3
- Flask
- requests
- IBM Watson NLP (Skills Network)
- unittest
- pylint
Educational project for Coursera/IBM certification.