Skip to content

Navigation Menu

Sign in
Sign up

Repository files navigation

Emotion Detection Application

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.

Features

  • 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

Project Structure

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

API Flow

  1. User enters text in index.html and clicks Analyze Emotion.

  2. JavaScript RunSentimentAnalysis() calls /emotionDetector?textToAnalyze=<text> via fetch().

  3. Flask route in server.py calls emotion_detector(text).

  4. emotion_detection.py sends a requests.post() to Watson NLP:

    https://sn-watson-emotion.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict
    
  5. JSON response is parsed; dominant emotion is the highest score.

  6. Flask returns a formatted plain-text response to the browser.

Installation

Prerequisites

  • Python 3.8 or newer
  • Internet access (Watson Skills Network endpoint)

Setup

cd emotion_detection_app
pip install -r requirements.txt

Running the Flask App

python server.py

Open: http://localhost:5000

Example API Request

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.

Running Tests

Tests call the live Watson API. Ensure you have network connectivity.

python -m unittest test_emotion_detection.py -v

Test Cases

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

Static Code Analysis

Run pylint for PEP8 compliance:

pylint EmotionDetection/emotion_detection.py
pylint server.py
pylint test_emotion_detection.py

Target score: 10/10

Error Handling

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

Technologies

  • Python 3
  • Flask
  • requests
  • IBM Watson NLP (Skills Network)
  • unittest
  • pylint

License

Educational project for Coursera/IBM certification.

About

AI-powered Emotion Detection Web Application built with Python, Flask, and IBM Watson NLP. The project analyzes text, detects emotions such as joy, anger, fear, disgust, and sadness, identifies the dominant emotion, includes unit testing, error handling, application packaging, and static code analysis.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

AltStyle によって変換されたページ (->オリジナル) /