-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
102 lines (73 loc) · 2.67 KB
/
app.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
import logging
from db import db
from image import *
from openai import OpenAI
from emotion import emotion
from datetime import datetime
from dotenv import load_dotenv
from flask import Flask, request, jsonify
load_dotenv()
app = Flask(__name__)
client = OpenAI(api_key=os.getenv("OPENAI_KEY"))
dbconn = db()
logging.basicConfig(
format="%(asctime)s %(levelname)s:%(message)s",
level=logging.INFO,
datefmt="%m/%d/%Y %I:%M:%S %p",
)
def getCompletion(messages, model="gpt-3.5-turbo"):
response = client.chat.completions.create(
model=model, messages=messages, temperature=0.9
)
return response.choices[0].message.content
def readyGPT(emotion, diary):
messages = [
{
"role": "system",
"content": "너는 사람들의 감정을 듣고, 그 감정에 대한 분석과 해결책을 제공하는 챗봇이야. 모든 입출력은 한국어로 이루어져야 해. 입력은 현재의 감정과 일기가 주어질 거야. 출력은 50자 내외로 해줘.",
},
{"role": "user", "content": f"감정: {emotion} 일기: {diary}"},
]
return getCompletion(messages)
@app.route("/api/emotion", methods=["POST"])
def getEmotion():
data = request.get_json()
face = base64ToImage(data["face"])
emotionInfo = emotion.getPrediction(face)
logging.info(emotionInfo)
return jsonify({"emotion": emotionInfo[0]["label"]})
@app.route("/api/get/diaries", methods=["POST"])
def getDiary():
data = request.get_json()
logging.info(data)
try:
user = data["user"]
except:
return jsonify({"diaries": []})
diaries = dbconn.getAll(user)
return jsonify({"diaries": diaries})
@app.route("/api/save/diary", methods=["POST"])
def saveDiary():
data = request.get_json()
logging.info(data)
user = data.get("user", 0)
date = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
emotion = data["emotion"]
diary = data["diary"]
solution = readyGPT(emotion, diary).split("\n\n")[0]
# solution = "솔루션!"
id = dbconn.insert(user, date, emotion, diary, solution)
logging.info(f"{id} - {user} - {date} - {emotion} - {diary} - {solution}")
return jsonify({"solution": solution})
@app.route("/api/save/image", methods=["POST"])
def saveImage():
data = request.get_json()
logging.info(data)
id = data["id"]
image = data["image"]
dbconn.updateImage(id, image)
return jsonify({"id": id})
if __name__ == "__main__":
# print(readyGPT("난 오늘 앱잼이라는 해커톤에 참여했는데, 너무 힘들었지만, 최우수상이라는 큰 상을 타게 되어서 매우 기뻐."))
app.run(host="0.0.0.0", port=3000)