-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.py
84 lines (67 loc) · 2.62 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
import os
import logging
import traceback
import openai
from flask import Flask, request, jsonify
from dotenv import load_dotenv
from helpers.inference_chatgpt import generate_response_chatgpt, generate_response_chatgpt_no_drift
load_dotenv()
app = Flask(__name__)
openai.api_key = os.environ.get('OPENAI_KEY')
app.debug = True
@app.route("/chat_openai", methods=['POST'])
def get_bot_response_chatgpt():
if request.method == 'POST':
output_text = []
try:
user_text = request.get_json()["text"]
output_text = generate_response_chatgpt(user_text)
return jsonify({"error": None,
"message": str(output_text),
"status": 200}), 200
except Exception as error:
return jsonify({"error": True,
"message": "Can't to process the input questions",
"status": 400}), 400
else:
return jsonify(success=False), 405
session = {"session_id": ""}
interview_history = []
@app.route("/chat_openai_no_drift", methods=['POST'])
def get_bot_response_chatgpt_no_drift():
if request.method == 'POST':
output_text = []
try:
user_text = request.get_json()["text"]
session_id = request.get_json()["session_id"]
print(session_id)
print(session["session_id"])
if session_id == session["session_id"]:
output_text = generate_response_chatgpt_no_drift(user_text, interview_history)
interview = {
"question": user_text,
"answer": output_text
}
interview_history.append(interview)
else:
session["session_id"] = session_id
interview_history2 = []
output_text = generate_response_chatgpt_no_drift(user_text, interview_history2)
interview = {
"question": user_text,
"answer": output_text
}
interview_history2.append(interview)
return jsonify({"message": str(output_text),
"error": None,
"status": 200}), 200
except Exception as error:
logging.error([error])
return jsonify({"error": True,
"message": "Can't to process the input questions",
"status": 400}), 400
else:
return jsonify({"error": "Not Allowed",
"status": 405}), 405
if __name__ == "__main__":
app.run(host="0.0.0.0")