-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
33 lines (26 loc) · 1.06 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
"""
Flask application for emotion detection.
"""
from flask import Flask, request, jsonify
from EmotionDetection.emotion_detection import emotion_detector
app = Flask(__name__)
@app.route('/emotionDetector', methods=['POST'])
def detect_emotion():
"""
Endpoint to detect emotions in the provided text.
"""
data = request.get_json()
text_to_analyze = data.get('text')
if not text_to_analyze:
return jsonify({"error": "Invalid text! Please try again."}), 400
result = emotion_detector(text_to_analyze)
if result['dominant_emotion'] is None:
return jsonify({"response": "Invalid text! Please try again."})
response_text = (
f"For the given statement, the system response is 'anger': {result['anger']}, "
f"'disgust': {result['disgust']}, 'fear': {result['fear']}, 'joy': {result['joy']} "
f"and 'sadness': {result['sadness']}. The dominant emotion is {result['dominant_emotion']}."
)
return jsonify({"response": response_text})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)