This repository has been archived by the owner on Sep 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
51 lines (37 loc) · 1.74 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
47
48
49
50
51
"""Server for Web Deployment"""
from flask import Flask, render_template, request
from emotion_detection.emotion_detection import emotion_detector
app = Flask("Emotion Detection")
@app.route('/')
def render_homepage():
""" Render homepage """
return render_template("index.html")
@app.route('/emotionDetector', methods=["GET"])
def emotion_analysis() -> str:
""" Analyze text and return emotion detection (analysis result) """
text_to_analyze = request.args["textToAnalyze"]
analysis_result = emotion_detector(text_to_analyze)
# Required output format:
# For the given statement, the system response is '<emotion1>': <score1>,
# '<emotion2>': <score2>, '<emotionN>': <scoreN>. The dominant emotion is <emotion>.
#
# Required output format when dominant_emotion == None:
# Invalid text! Please try again.
if analysis_result["dominant_emotion"] is None:
response = "Invalid text! Please try again!"
else:
response = "For the given statement, the system response is"
for key, value in analysis_result.items():
# dominant emotion is mentioned separately as its own sentence.
if key != "dominant_emotion":
response += f" '{key}': {value},"
# Replace last comma with point. If index is -1, no commas found.
last_comma_index = response.rfind(",")
if last_comma_index != -1:
response = response[:last_comma_index] + '.' + response[last_comma_index + 1:]
response += f" The dominant emotion is {analysis_result['dominant_emotion']}."
return response
# To make sure that the server only runs when the script is executed
# directly (not imported as a module)
if __name__ == "__main__":
app.run(debug = True)