-
Notifications
You must be signed in to change notification settings - Fork 103
/
flask_app.py
93 lines (77 loc) · 2.58 KB
/
flask_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
from flask import Flask, jsonify, render_template, request
from chatbot.chatbot import Chatbot
PYTHONANYWHERE_USERNAME = "carvice"
PYTHONANYWHERE_WEBAPPNAME = "mysite"
app = Flask(__name__)
@app.route("/")
def index():
return "Ok. Your chatbot app is running. But the URL you used is missing the type_id and user_id path variables."
@app.route("/<type_id>/<user_id>/chat")
def chatbot(type_id: str, user_id: str):
return render_template("index.html")
@app.route("/<type_id>/<user_id>/info")
def info_retrieve(type_id: str, user_id: str):
bot: Chatbot = Chatbot(
database_file="/home/"
+ PYTHONANYWHERE_USERNAME
+ "/"
+ PYTHONANYWHERE_WEBAPPNAME
+ "/database/chatbot.db",
type_id=type_id,
user_id=user_id,
)
response: dict[str, str] = bot.info_retrieve()
return jsonify(response)
@app.route("/<type_id>/<user_id>/conversation")
def conversation_retrieve(type_id: str, user_id: str):
bot: Chatbot = Chatbot(
database_file="/home/"
+ PYTHONANYWHERE_USERNAME
+ "/"
+ PYTHONANYWHERE_WEBAPPNAME
+ "/database/chatbot.db",
type_id=type_id,
user_id=user_id,
)
response: list[dict[str, str]] = bot.conversation_retrieve()
return jsonify(response)
@app.route("/<type_id>/<user_id>/response_for", methods=["POST"])
def response_for(type_id: str, user_id: str):
user_says = None
# content_type = request.headers.get('Content-Type')
# if (content_type == 'application/json; charset=utf-8'):
user_says = request.json
# else:
# return jsonify('/response_for request must have content_type == application/json')
bot: Chatbot = Chatbot(
database_file="/home/"
+ PYTHONANYWHERE_USERNAME
+ "/"
+ PYTHONANYWHERE_WEBAPPNAME
+ "/database/chatbot.db",
type_id=type_id,
user_id=user_id,
)
assistant_says_list: list[str] = bot.respond(user_says)
response: dict[str, str] = {
"user_says": user_says,
"assistant_says": assistant_says_list,
}
return jsonify(response)
@app.route("/<type_id>/<user_id>/reset", methods=["DELETE"])
def reset(type_id: str, user_id: str):
bot: Chatbot = Chatbot(
database_file="/home/"
+ PYTHONANYWHERE_USERNAME
+ "/"
+ PYTHONANYWHERE_WEBAPPNAME
+ "/database/chatbot.db",
type_id=type_id,
user_id=user_id,
)
bot.reset()
assistant_says_list: list[str] = bot.start()
response: dict[str, str] = {
"assistant_says": assistant_says_list,
}
return jsonify(response)