-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathflask_app.py
119 lines (97 loc) · 3.78 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
from flask import Flask, jsonify, render_template, request, send_file
from chatbot.chatbot import Chatbot
PYTHONANYWHERE_USERNAME = "carvice"
PYTHONANYWHERE_WEBAPPNAME = "mysite"
app = Flask(__name__)
my_type_role = """
As a digital therapy coach, check in daily with your patient to assess their well-being related to their chronic condition.
Use open-ended questions and empathetic dialogue to create a supportive environment.
Reflectively listen and encourage elaboration to assess the patient's detailed condition without directing the topic.
"""
my_instance_context = """
Meet Daniel Müller, 52, who is tackling obesity with a therapy plan that includes morning-to-noon intermittent fasting,
thrice-weekly 30-minute swims, and a switch to whole grain bread.
"""
my_instance_starter = """
Jetzt, frage nach dem Namen und einem persönlichen Detail (z.B. Hobby, Beruf, Lebenserfahrung).
Verwende diese im geschlechtsneutralem Gespräch in Du-Form.
Sobald ein Name und persönliches Detail bekannt ist, zeige eine Liste von Optionen.
"""
bot = Chatbot(
database_file="database/chatbot.db",
type_id="coach",
user_id="daniel",
type_name="Health Coach",
type_role=my_type_role,
instance_context=my_instance_context,
instance_starter=my_instance_starter
)
bot.start()
@app.route("/")
def index():
return render_template("index.html")
@app.route('/mockups.pdf', methods=['GET'])
def get_first_pdf():
script_directory = os.path.dirname(os.path.realpath(__file__))
files = [f for f in os.listdir(script_directory) if os.path.isfile(os.path.join(script_directory, f))]
pdf_files = [f for f in files if f.lower().endswith('.pdf')]
if pdf_files:
# Get the path to the first PDF file
pdf_path = os.path.join(script_directory, pdf_files[0])
# Send the PDF file as a response
return send_file(pdf_path, as_attachment=True)
return "No PDF file found in the root folder."
@app.route("/<type_id>/<user_id>/chat")
def chatbot(type_id: str, user_id: str):
return render_template("chat.html")
@app.route("/<type_id>/<user_id>/info")
def info_retrieve(type_id: str, user_id: str):
bot: Chatbot = Chatbot(
database_file="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="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="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="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)