-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
46 lines (38 loc) · 1.51 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
Emotion Detection Server
This script defines a Flask-based server for performing emotion detection on user-provided text.
Author(Learner): [NoorAldin]
"""
from flask import Flask, render_template, request
from EmotionDetection.emotion_detection import emotion_detector
from EmotionDetection.emotion_detection import emotion_predictor
app = Flask("Emotion Detection")
def run_emotion_detection():
"""
Main function to run the Emotion Detection application.
"""
app.run(host="0.0.0.0", port=5000)
@app.route("/emotionDetector")
def sent_detector():
"""
Analyze the user-provided text for emotions and return the result.
"""
text_to_detect = request.args.get('textToAnalyze')
response = emotion_detector(text_to_detect)
formated_response = emotion_predictor(response)
if formated_response['dominant_emotion'] is None:
return "Invalid text! Please try again."
return (
f"For the given statement, the system response is 'anger': {formated_response['anger']} "
f"'disgust': {formated_response['disgust']}, 'fear': {formated_response['fear']}, "
f"'joy': {formated_response['joy']} and 'sadness': {formated_response['sadness']}. "
f"The dominant emotion is {formated_response['dominant_emotion']}."
)
@app.route("/")
def render_index_page():
''' This function initiates the rendering of the main application
page over the Flask channel
'''
return render_template('index.html')
if __name__ == "__main__":
run_emotion_detection()