Spaces:
Runtime error
Runtime error
natexcvi
commited on
Commit
•
3fc62de
1
Parent(s):
ff37931
Add logic for bot handling
Browse files- main.py +52 -22
- requirements.txt +2 -1
main.py
CHANGED
@@ -1,9 +1,10 @@
|
|
1 |
import os
|
2 |
import random
|
3 |
import statistics
|
4 |
-
from typing import Dict, Optional
|
5 |
|
6 |
from fastapi import BackgroundTasks, Body, FastAPI, Response, UploadFile, status
|
|
|
7 |
|
8 |
from schema import *
|
9 |
from telegram_bot import checkup_messages, send_message_to_user
|
@@ -21,10 +22,10 @@ class DB:
|
|
21 |
end="2021-12-31",
|
22 |
items=[
|
23 |
CarePlanItem(
|
24 |
-
name="
|
25 |
-
description="
|
26 |
-
quantity=
|
27 |
-
units="
|
28 |
frequency="daily",
|
29 |
start="2021-01-01",
|
30 |
adherence=0.9,
|
@@ -51,32 +52,34 @@ class DB:
|
|
51 |
"Suggest lighter activities",
|
52 |
],
|
53 |
),
|
54 |
-
PatientIssue(
|
55 |
-
title="Patient is not taking metformin",
|
56 |
-
reason="Reports feeling nauseous",
|
57 |
-
suggested_actions=[
|
58 |
-
"Discuss reason with physician",
|
59 |
-
"Suggest taking metformin with food",
|
60 |
-
],
|
61 |
-
),
|
62 |
]
|
63 |
}
|
64 |
self.adherence_history = {
|
65 |
"123": AdherenceHistory(
|
66 |
history=[
|
67 |
AdherenceHistoryRecord(date="2021-01-01", adherence=0.9),
|
68 |
-
AdherenceHistoryRecord(date="2021-01
|
69 |
-
AdherenceHistoryRecord(date="2021-01
|
70 |
-
AdherenceHistoryRecord(date="2021-01
|
71 |
-
AdherenceHistoryRecord(date="2021-01
|
72 |
-
AdherenceHistoryRecord(date="2021-01
|
73 |
-
AdherenceHistoryRecord(date="2021-01
|
74 |
-
AdherenceHistoryRecord(date="2021-01
|
75 |
-
AdherenceHistoryRecord(date="2021-01
|
76 |
-
AdherenceHistoryRecord(date="2021-01
|
77 |
]
|
78 |
)
|
79 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
|
82 |
db = DB()
|
@@ -132,4 +135,31 @@ async def get_patient_issues(patient_id: str) -> list[PatientIssue] | Response:
|
|
132 |
@app.post("/log-message-response")
|
133 |
async def log_message_response(msg: dict = Body(...)):
|
134 |
print(msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
return Response(status_code=status.HTTP_202_ACCEPTED)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import random
|
3 |
import statistics
|
4 |
+
from typing import Any, Dict, Optional
|
5 |
|
6 |
from fastapi import BackgroundTasks, Body, FastAPI, Response, UploadFile, status
|
7 |
+
from jsonpath import JSONPath
|
8 |
|
9 |
from schema import *
|
10 |
from telegram_bot import checkup_messages, send_message_to_user
|
|
|
22 |
end="2021-12-31",
|
23 |
items=[
|
24 |
CarePlanItem(
|
25 |
+
name="Glyburide",
|
26 |
+
description="Glyburide to treat diabetes",
|
27 |
+
quantity=2.5,
|
28 |
+
units="mg",
|
29 |
frequency="daily",
|
30 |
start="2021-01-01",
|
31 |
adherence=0.9,
|
|
|
52 |
"Suggest lighter activities",
|
53 |
],
|
54 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
]
|
56 |
}
|
57 |
self.adherence_history = {
|
58 |
"123": AdherenceHistory(
|
59 |
history=[
|
60 |
AdherenceHistoryRecord(date="2021-01-01", adherence=0.9),
|
61 |
+
AdherenceHistoryRecord(date="2021-02-01", adherence=0.3),
|
62 |
+
AdherenceHistoryRecord(date="2021-03-01", adherence=0.5),
|
63 |
+
AdherenceHistoryRecord(date="2021-04-01", adherence=0.79),
|
64 |
+
AdherenceHistoryRecord(date="2021-05-01", adherence=0.7),
|
65 |
+
AdherenceHistoryRecord(date="2021-06-01", adherence=0.8),
|
66 |
+
AdherenceHistoryRecord(date="2021-07-01", adherence=0.75),
|
67 |
+
AdherenceHistoryRecord(date="2021-08-01", adherence=0.78),
|
68 |
+
AdherenceHistoryRecord(date="2021-09-01", adherence=0.80),
|
69 |
+
AdherenceHistoryRecord(date="2021-10-01", adherence=0.85),
|
70 |
]
|
71 |
)
|
72 |
}
|
73 |
+
self.conversation_state = {
|
74 |
+
"1773171761": {
|
75 |
+
"current_question": 0,
|
76 |
+
"patient_id": "123",
|
77 |
+
"action": "",
|
78 |
+
}
|
79 |
+
}
|
80 |
+
|
81 |
+
def reset(self):
|
82 |
+
self.__init__()
|
83 |
|
84 |
|
85 |
db = DB()
|
|
|
135 |
@app.post("/log-message-response")
|
136 |
async def log_message_response(msg: dict = Body(...)):
|
137 |
print(msg)
|
138 |
+
user_id = JSONPath("$..from.id").parse(msg)
|
139 |
+
if user_id not in db.conversation_state:
|
140 |
+
return Response(status_code=status.HTTP_404_NOT_FOUND)
|
141 |
+
state = db.conversation_state[user_id]
|
142 |
+
patient_id = state["patient_id"]
|
143 |
+
response = JSONPath("$..queryResult.queryText").parse(msg)
|
144 |
+
action = JSONPath("$..queryResult.action").parse(msg)
|
145 |
+
state["action"] = action
|
146 |
+
state["current_question"] += 1
|
147 |
+
if state["action"] == "good-flow.good-flow-no.good-flow-no-no-2":
|
148 |
+
db.care_plans[patient_id].items[0].adherence -= 0.02
|
149 |
+
db.issues[patient_id].append(
|
150 |
+
PatientIssue(
|
151 |
+
title="Patient is not taking glyburide",
|
152 |
+
reason=response,
|
153 |
+
suggested_actions=[
|
154 |
+
"Discuss side effects with physician",
|
155 |
+
"Suggest taking glyburide with food",
|
156 |
+
],
|
157 |
+
)
|
158 |
+
)
|
159 |
return Response(status_code=status.HTTP_202_ACCEPTED)
|
160 |
+
|
161 |
+
|
162 |
+
@app.delete("/reset-db")
|
163 |
+
async def reset_db():
|
164 |
+
db.reset()
|
165 |
+
return Response(status_code=status.HTTP_200_OK)
|
requirements.txt
CHANGED
@@ -2,4 +2,5 @@ fastapi==0.74.*
|
|
2 |
requests==2.27.*
|
3 |
uvicorn[standard]==0.17.*
|
4 |
python-multipart
|
5 |
-
python-telegram-bot
|
|
|
|
2 |
requests==2.27.*
|
3 |
uvicorn[standard]==0.17.*
|
4 |
python-multipart
|
5 |
+
python-telegram-bot
|
6 |
+
jsonpath-python
|