natexcvi commited on
Commit
1150b5c
1 Parent(s): 575d27c

Initial commit

Browse files
Files changed (5) hide show
  1. Dockerfile +17 -0
  2. main.py +106 -0
  3. requirements.txt +5 -0
  4. schema.py +28 -0
  5. telegram_bot.py +27 -0
Dockerfile ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN apt-get update && apt-get install -y \
8
+ ffmpeg \
9
+ && rm -rf /var/lib/apt/lists/*
10
+
11
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
12
+ RUN --mount=type=secret,id=TELEGRAM_BOT_API,mode=0444,required=true
13
+ RUN --mount=type=secret,id=TELEGRAM_USER,mode=0444,required=true
14
+
15
+ COPY . .
16
+
17
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
main.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
10
+
11
+ app = FastAPI()
12
+ tasks: Dict[str, Optional[Dict]] = {}
13
+
14
+
15
+ class DB:
16
+ def __init__(self):
17
+ self.care_plans = {
18
+ "123": CarePlan(
19
+ purpose="Diabetes",
20
+ start="2021-01-01",
21
+ end="2021-12-31",
22
+ items=[
23
+ CarePlanItem(
24
+ name="Insulin",
25
+ description="Insulin to treat diabetes",
26
+ quantity=1.0,
27
+ units="ml",
28
+ frequency="daily",
29
+ start="2021-01-01",
30
+ adherence=0.9,
31
+ ),
32
+ CarePlanItem(
33
+ name="Metformin",
34
+ description="Metformin to treat diabetes",
35
+ quantity=1.0,
36
+ units="tablet",
37
+ frequency="daily",
38
+ start="2021-01-01",
39
+ adherence=0.8,
40
+ ),
41
+ ],
42
+ )
43
+ }
44
+ self.issues = {
45
+ "123": [
46
+ PatientIssue(
47
+ title="Patient does not engage in physical activity",
48
+ reason="Reports feeling tired and fatigued",
49
+ suggested_actions=[
50
+ "Discuss reason with physician",
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
+
65
+
66
+ db = DB()
67
+
68
+
69
+ @app.post("/echo")
70
+ async def echo(msg: str):
71
+ return msg
72
+
73
+
74
+ @app.post("/trigger-medication-check")
75
+ async def trigger_medication_check(background_tasks: BackgroundTasks):
76
+ background_tasks.add_task(
77
+ send_message_to_user,
78
+ chat_id=os.getenv("TELEGRAM_USER_ID", ""),
79
+ message=random.choice(checkup_messages),
80
+ )
81
+ return Response(status_code=status.HTTP_202_ACCEPTED)
82
+
83
+
84
+ @app.get("/{patient_id}/care_plan", response_model=CarePlan)
85
+ async def get_patient_care_plan(patient_id: str) -> CarePlan | Response:
86
+ if patient_id not in db.care_plans:
87
+ return Response(status_code=status.HTTP_404_NOT_FOUND)
88
+ return db.care_plans[patient_id]
89
+
90
+
91
+ @app.get("/{patient_id}/adherence_status", response_model=AdherenceStatus)
92
+ async def get_patient_adherence_status(patient_id: str) -> AdherenceStatus | Response:
93
+ if patient_id not in db.care_plans:
94
+ return Response(status_code=status.HTTP_404_NOT_FOUND)
95
+ return AdherenceStatus(
96
+ adherence=statistics.mean(
97
+ [item.adherence for item in db.care_plans[patient_id].items]
98
+ )
99
+ )
100
+
101
+
102
+ @app.get("/{patient_id}/issues", response_model=list[PatientIssue])
103
+ async def get_patient_issues(patient_id: str) -> list[PatientIssue] | Response:
104
+ if patient_id not in db.issues:
105
+ return Response(status_code=status.HTTP_404_NOT_FOUND)
106
+ return db.issues[patient_id]
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi==0.74.*
2
+ requests==2.27.*
3
+ uvicorn[standard]==0.17.*
4
+ python-multipart
5
+ python-telegram-bot
schema.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel
2
+
3
+
4
+ class CarePlanItem(BaseModel):
5
+ name: str
6
+ description: str
7
+ quantity: float
8
+ units: str
9
+ frequency: str
10
+ start: str
11
+ adherence: float | None = None
12
+
13
+
14
+ class CarePlan(BaseModel):
15
+ purpose: str
16
+ start: str
17
+ end: str
18
+ items: list[CarePlanItem]
19
+
20
+
21
+ class PatientIssue(BaseModel):
22
+ title: str
23
+ reason: str
24
+ suggested_actions: list[str]
25
+
26
+
27
+ class AdherenceStatus(BaseModel):
28
+ adherence: float
telegram_bot.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import os
3
+ import random
4
+
5
+ import telegram
6
+ from dotenv import load_dotenv
7
+
8
+ load_dotenv()
9
+
10
+
11
+ async def send_message_to_user(chat_id: str, message: str):
12
+ bot = telegram.Bot(token=os.getenv("TELEGRAM_BOT_TOKEN", ""))
13
+ await bot.send_message(chat_id=chat_id, text=message)
14
+
15
+
16
+ checkup_messages = [
17
+ "Hey there, how are you feeling today?",
18
+ "Hello, how are you doing today?",
19
+ "Hi, how are you feeling today?",
20
+ "Hey, what's up?",
21
+ "Hi, how are you doing?",
22
+ ]
23
+
24
+
25
+ if __name__ == "__main__":
26
+ msg = random.choice(checkup_messages)
27
+ asyncio.run(send_message_to_user(chat_id="1773171761", message=msg))