Skip to content

Done all the necessary Changes. #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions EmotionDetection/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .emotion_detection import emotion_detector
48 changes: 48 additions & 0 deletions EmotionDetection/emotion_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import requests
import json

def emotion_detector(text_to_analyze):
url = "https://sn-watson-emotion.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict"
headers = {"grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock"}
data = { "raw_document": { "text": text_to_analyze } }

response = requests.post(url, json=data, headers=headers)

if response.status_code == 400:
return {
'anger': None,
'disgust': None,
'fear': None,
'joy': None,
'sadness': None,
'dominant_emotion': None
}

result = json.loads(response.text)

emotions = result['emotionPredictions'][0]['emotion']
anger = emotions['anger']
disgust = emotions['disgust']
fear = emotions['fear']
joy = emotions['joy']
sadness = emotions['sadness']

# Get the dominant emotion
emotion_scores = {
'anger': anger,
'disgust': disgust,
'fear': fear,
'joy': joy,
'sadness': sadness
}

dominant_emotion = max(emotion_scores, key=emotion_scores.get)

return {
'anger': anger,
'disgust': disgust,
'fear': fear,
'joy': joy,
'sadness': sadness,
'dominant_emotion': dominant_emotion
}
232 changes: 231 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,231 @@
# Repository for final project
# 🧠 AI-Based Emotion Detection Web Application

A complete AI-powered web application built with Flask and IBM Watson NLP to detect **emotions** from customer feedback text. This project includes **data analysis**, **testing**, **error handling**, **static code analysis**, and **web deployment**.

---

## 📁 Project Structure

```
final_project/
├── EmotionDetection/
│ ├── __init__.py
│ └── emotion_detection.py
├── static/
│ └── mywebscript.js
├── templates/
│ └── index.html
├── server.py
├── test_emotion_detection.py
├── README.md
└── [screenshots...]
```

---

## 🧩 Tasks Overview & Code

### ✅ Task 1: Fork & Clone

```bash
git clone https://github.com/YOUR_USERNAME/oaqjp-final-project-emb-ai.git final_project
cd final_project
```

---

### ✅ Task 2: Create Emotion Detection Module

**File:** `emotion_detection.py`

```python
import requests
import json

def emotion_detector(text_to_analyze):
url = "https://sn-watson-emotion.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict"
headers = {"grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock"}
data = { "raw_document": { "text": text_to_analyze } }
response = requests.post(url, headers=headers, json=data)
if response.status_code == 400:
return {
'anger': None, 'disgust': None, 'fear': None,
'joy': None, 'sadness': None, 'dominant_emotion': None
}
response_json = json.loads(response.text)
emotions = response_json['emotionPredictions'][0]['emotion']
dominant_emotion = max(emotions, key=emotions.get)
return {
'anger': emotions['anger'],
'disgust': emotions['disgust'],
'fear': emotions['fear'],
'joy': emotions['joy'],
'sadness': emotions['sadness'],
'dominant_emotion': dominant_emotion
}
```

---

### ✅ Task 3: Format Output

Already included above in `emotion_detector()` function.

---

### ✅ Task 4: Package as Module

**Folder:** `EmotionDetection/`
**File:** `__init__.py`

```python
from .emotion_detection import emotion_detector
```

---

### ✅ Task 5: Unit Testing

**File:** `test_emotion_detection.py`

```python
import unittest
from EmotionDetection.emotion_detection import emotion_detector

class TestEmotionDetection(unittest.TestCase):
def test_joy(self):
self.assertEqual(emotion_detector("I am glad this happened")['dominant_emotion'], "joy")
def test_anger(self):
self.assertEqual(emotion_detector("I am really mad about this")['dominant_emotion'], "anger")
def test_disgust(self):
self.assertEqual(emotion_detector("I feel disgusted just hearing about this")['dominant_emotion'], "disgust")
def test_sadness(self):
self.assertEqual(emotion_detector("I am so sad about this")['dominant_emotion'], "sadness")
def test_fear(self):
self.assertEqual(emotion_detector("I am really afraid that this will happen")['dominant_emotion'], "fear")

if __name__ == '__main__':
unittest.main()
```

Run the tests:

```bash
python3 test_emotion_detection.py
```

---

### ✅ Task 6: Web Deployment with Flask

**File:** `server.py`

```python
from flask import Flask, request, render_template
from EmotionDetection.emotion_detection import emotion_detector

app = Flask(__name__)

@app.route('/emotionDetector')
def emotion_detector_route():
text = request.args.get('textToAnalyze')
result = emotion_detector(text)
if result['dominant_emotion'] is None:
return "Invalid text! Please try again!"
response = (f"For the given statement, the system response is "
f"'anger': {result['anger']}, 'disgust': {result['disgust']}, "
f"'fear': {result['fear']}, 'joy': {result['joy']} and "
f"'sadness': {result['sadness']}. The dominant emotion is "
f"{result['dominant_emotion']}.")
return response

if __name__ == '__main__':
app.run(debug=True)
```

Run the app:

```bash
python3 server.py
```

Access at: [http://localhost:5000](http://localhost:5000)

---

### ✅ Task 7: Error Handling

Handled in `emotion_detector()` (via `status_code == 400`) and in `server.py` (`dominant_emotion == None`).

---

### ✅ Task 8: Static Code Analysis

Run Pylint:

```bash
pylint server.py
```

Ensure functions have proper **docstrings** to get a 10/10 score.

---

## 💡 Sample Input/Output

**Input Text:** "I love my life"

**Formatted Output:**

```json
{
"anger": 0.0062,
"disgust": 0.0025,
"fear": 0.0092,
"joy": 0.9680,
"sadness": 0.0497,
"dominant_emotion": "joy"
}
```

**Response Message:**
"For the given statement, the system response is 'anger': 0.0062, 'disgust': 0.0025, 'fear': 0.0092, 'joy': 0.9680 and 'sadness': 0.0497. The dominant emotion is joy."

---

## 📸 Screenshots

All screenshots from each task (e.g., `1_folder_structure.png`, `2a_emotion_detection.png`, `6b_deployment_test.png`) are included in the root project directory.

---

## 🧪 Technologies Used

- Python 3.11
- Flask (Web framework)
- Watson NLP API (Emotion Detection)
- Requests (HTTP client)
- unittest (Testing)
- pylint (Static Code Analysis)

---

## 👨‍💻 Author

**Souvik Mandal**
IBM AI Engineering Final Project
GitHub: [Souvik Mandal](https://github.com/AP19110010485)

---

## 🏁 Status

✅ All 8 tasks completed
✅ Fully functional and tested
✅ 10/10 code quality

🎉 Thank you for visiting the project!
Binary file added Screenshots/1_folder_structure.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/2a_emotion_detection.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/2b_application_creation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/3a_output_formatting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/3b_formatted_output_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/4a_packaging.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/4b_packaging_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/5a_unit_testing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/5b_unit_testing_result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/6a_server.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/6b_deployment_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/7a_error_handling_function.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/7b_error_handling_server.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/7c_error_handling_interface.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/8a_server_modified.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/8b_static_code_analysis.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Flask==2.3.2
requests==2.31.0
pylint==3.0.2
33 changes: 33 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Flask server for Emotion Detection App."""

from flask import Flask, request, render_template
from EmotionDetection.emotion_detection import emotion_detector

app = Flask(__name__)

@app.route("/")
def render_index_page():
"""Renders the main page of the application."""
return render_template("index.html")

@app.route("/emotionDetector")
def analyze_emotion():
"""Handles emotion analysis request and returns formatted response."""
text_to_analyze = request.args.get("textToAnalyze")
response = emotion_detector(text_to_analyze)

if response["dominant_emotion"] is None:
return "Invalid text! Please try again!"

return (
f"For the given statement, the system response is "
f"'anger': {response['anger']}, "
f"'disgust': {response['disgust']}, "
f"'fear': {response['fear']}, "
f"'joy': {response['joy']} and "
f"'sadness': {response['sadness']}. "
f"The dominant emotion is {response['dominant_emotion']}."
)

if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
26 changes: 26 additions & 0 deletions test_emotion_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import unittest
from EmotionDetection import emotion_detector

class TestEmotionDetector(unittest.TestCase):
def test_joy(self):
result = emotion_detector("I am glad this happened")
self.assertEqual(result['dominant_emotion'], 'joy')

def test_anger(self):
result = emotion_detector("I am really mad about this")
self.assertEqual(result['dominant_emotion'], 'anger')

def test_disgust(self):
result = emotion_detector("I feel disgusted just hearing about this")
self.assertEqual(result['dominant_emotion'], 'disgust')

def test_sadness(self):
result = emotion_detector("I am so sad about this")
self.assertEqual(result['dominant_emotion'], 'sadness')

def test_fear(self):
result = emotion_detector("I am really afraid that this will happen")
self.assertEqual(result['dominant_emotion'], 'fear')

if __name__ == '__main__':
unittest.main()