-
Notifications
You must be signed in to change notification settings - Fork 25
/
server.py
52 lines (39 loc) · 1.3 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
52
from flask import Flask, request
import secrets
import string
import predict
app = Flask(__name__)
enabled_api_keys = ["f24eded9-fcd1-4392-b214-01bad08fa69f"]
contexts = dict()
def generate_context_id():
return ''.join(secrets.choice(string.ascii_uppercase + string.digits) for _ in range(64))
@app.route('/getreply', methods=['GET'])
def getreply():
key = request.args.get('key')
if not key or key not in enabled_api_keys:
return {
"status": "401",
"error": "Missing or invalid API key"
}, 401
text = request.args.get('input')
if text:
print(f"Input: {text}")
context_id = request.args.get('context')
if not context_id:
context_id = generate_context_id()
if context_id not in contexts:
contexts[context_id] = list()
contexts[context_id].extend(text.split('\n'))
while len(contexts[context_id]) > predict.CONVERSATION_TURNS:
contexts[context_id].pop(0)
# elaborate response
answer, score = predict.predict(contexts[context_id])
print(f"Score: {score} Answer: {answer}")
contexts[context_id].append(answer)
return {
"context": context_id,
"output": str(answer),
"score": score
}
if __name__ == '__main__':
app.run("localhost", "2834", debug=True)