-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemotion_state_backend.py
67 lines (55 loc) · 1.71 KB
/
emotion_state_backend.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from flask import Flask, request, jsonify
from openai import OpenAI
import json
app = Flask(__name__)
client = OpenAI(api_key="<API_KEY>")
state = "NORMAL"
@app.route("/getEmotion", methods=['GET'])
def getEmotion():
resp = jsonify({
"emotion": state
})
return resp
@app.route('/setEmotion', methods = ['POST'])
def setEmotion():
data = request.json
data = json.loads(request.data)
app.logger.info(f"TRANSCRIPT: {data['transcript']}")
if "transcript" not in data:
# throw error
pass
resp = jsonify(success=True)
resp.headers.add("Access-Control-Allow-Origin", "*")
response = client.chat.completions.create(
model="gpt-4o-2024-08-06",
messages=[
{
"role": "system",
"content": "Given a text, you classify the emotion of the speaker into one of SHOCKED, THANKFUL, NORMAL, THINKING, DETERMINED. Only use the information give. Give only one of the above."
},
{
"role": "user",
"content": data["transcript"] # TEXT
}
]
)
global state
state = response.choices[0].message.content
if state not in ["SHOCKED", "THANKFUL", "NORMAL", "THINKING", "DETERMINED"]:
state = "NORMAL"
app.logger.info(f"NEW STATE: {state}")
resp = jsonify({
"emotion": state
})
return resp
@app.route('/demote', methods = ['GET'])
def demote():
global state
if state == 4: state = 3
elif state == 2: state = 1
elif state == 6: state = 5
resp = jsonify(success=True)
resp.headers.add("Access-Control-Allow-Origin", "*")
return resp
if __name__ == '__main__':
app.run(debug=True)