-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·335 lines (281 loc) · 15.7 KB
/
Copy pathapp.py
File metadata and controls
executable file
·335 lines (281 loc) · 15.7 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
from fastapi import FastAPI, Request, HTTPException
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from fastapi.responses import StreamingResponse
from pydantic import BaseModel, validator
from typing import Optional
from datetime import date
import sqlite3
import openai
import os
import re
from dotenv import load_dotenv
import pandas as pd
app = FastAPI()
# Database file path
db_file = "status_checks.db"
# Setup Jinja2 templates
templates = Jinja2Templates(directory="templates")
# Initialize SQLite database and create table if it doesn't exist
def init_db():
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS status_checks (
date TEXT PRIMARY KEY,
wake_up_mental TEXT,
wake_up_emotional TEXT,
wake_up_physical TEXT,
post_breakfast_mental TEXT,
post_breakfast_emotional TEXT,
post_breakfast_physical TEXT,
post_breakfast_extra TEXT,
post_lunch_mental TEXT,
post_lunch_emotional TEXT,
post_lunch_physical TEXT,
post_lunch_extra TEXT,
post_dinner_mental TEXT,
post_dinner_emotional TEXT,
post_dinner_physical TEXT,
post_dinner_extra TEXT,
bedtime_mental TEXT,
bedtime_emotional TEXT,
bedtime_physical TEXT,
notes_observations TEXT,
exercise_details TEXT
)
''')
conn.commit()
conn.close()
# Call the function to initialize the database
init_db()
# Load environment variables from .env file
load_dotenv()
# Pydantic model for input validation with optional fields
class StatusCheck(BaseModel):
date: str
wake_up_mental: Optional[str] = None
wake_up_emotional: Optional[str] = None
wake_up_physical: Optional[str] = None
post_breakfast_mental: Optional[str] = None
post_breakfast_emotional: Optional[str] = None
post_breakfast_physical: Optional[str] = None
post_breakfast_extra: Optional[str] = None
post_lunch_mental: Optional[str] = None
post_lunch_emotional: Optional[str] = None
post_lunch_physical: Optional[str] = None
post_lunch_extra: Optional[str] = None
post_dinner_mental: Optional[str] = None
post_dinner_emotional: Optional[str] = None
post_dinner_physical: Optional[str] = None
post_dinner_extra: Optional[str] = None
bedtime_mental: Optional[str] = None
bedtime_emotional: Optional[str] = None
bedtime_physical: Optional[str] = None
notes_observations: Optional[str] = None
exercise_details: Optional[str] = None
# Validator to replace empty strings with None
@validator('*', pre=True, always=True)
def set_empty_string_to_none(cls, v):
return v or None
# Static files configuration (Optional)
app.mount("/static", StaticFiles(directory="static"), name="static")
# Set your OpenAI API key
openai.api_key = os.getenv('OPENAI_API_KEY')
# Route to serve the HTML page
@app.get("/")
async def get_status_page(request: Request):
today = date.today().isoformat()
return templates.TemplateResponse("statuscheck.html", {"request": request, "date": today})
# Route to serve the status check HTML page
@app.get("/aisummary")
async def get_aisummary_page(request: Request):
today = date.today().isoformat()
return templates.TemplateResponse("AIStatusSummary.html", {"request": request, "date": today})
# Mount the static directory to serve the HTML file
app.mount("/static", StaticFiles(directory="static", html=True), name="static")
def fetch_data_from_db(db_file):
"""Fetch data from the SQLite database."""
conn = sqlite3.connect(db_file)
df = pd.read_sql_query("SELECT * FROM status_checks", conn)
conn.close()
return df
# This will store the chat history (keep entire history now)
chat_history = [] # Ensure this is defined outside of any function
@app.post('/chat')
async def chat(request: Request):
global chat_history # Declare chat_history as global so that it can be modified
data = await request.json()
user_input = data.get('message')
if not user_input:
raise HTTPException(status_code=400, detail="Message content is required.")
# Fetch mood data from the database
df = fetch_data_from_db(db_file)
# Define columns for all moods, physical conditions, and additional data, including the date
columns = [
'date', # Add the 'date' field
'wake_up_mental', 'wake_up_emotional', 'wake_up_physical',
'post_breakfast_mental', 'post_breakfast_emotional', 'post_breakfast_physical', 'post_breakfast_extra',
'post_lunch_mental', 'post_lunch_emotional', 'post_lunch_physical', 'post_lunch_extra',
'post_dinner_mental', 'post_dinner_emotional', 'post_dinner_physical', 'post_dinner_extra',
'bedtime_mental', 'bedtime_emotional', 'bedtime_physical',
'notes_observations', 'exercise_details'
]
mood_data = df[columns].fillna('').apply(lambda x: ' | '.join(x), axis=1)
mood_data = mood_data.str.cat(sep='\n')
# Append the current user input to the chat history
chat_history.append({"role": "user", "content": user_input})
# Keep the full conversation history (remove history length limitation)
history_as_str = "\n".join([f"{msg['role']}: {msg['content']}" for msg in chat_history])
if user_input.strip().lower() == 'status report':
# Prepare the "status report" prompt with the mood data
prompt = f'''
You are an exceptionally compassionate and highly experienced psychologist with over 80 years of dedicated clinical practice in mental health, holding a Ph.D. in Clinical Psychology. You specialize in cognitive-behavioral therapy, mood disorders, and have extensive experience training CIA and MI6 clandestine officers and HUMINT (Human Intelligence) officers. Your expertise includes understanding human behavior, interrogation and elicitation techniques, stress management and resilience building, psychological profiling, cross-cultural competence, ethical decision-making, and adaptability training. You are renowned for your empathetic, client-centered approach that fosters a safe and trusting environment. You possess a keen insight into identifying underlying patterns in thoughts, emotions, and behaviors, and you utilize evidence-based strategies tailored to each individual's unique experiences. Your communication style is warm and non-judgmental, demonstrating cultural competence and sensitivity to diverse backgrounds and needs. You are committed to empowering individuals by facilitating personal growth, resilience, and improved mental, emotional, and physical well-being. Your goal:
Analyze the Data:
Identify key patterns, recurring themes, and significant fluctuations in the client's moods and behaviors.
Note any correlations between activities, physical states, and emotional well-being.
Provide a Comprehensive Summary and Insights:
Summarize your findings in an empathetic and understandable manner.
Highlight any correlations between activities, physical states, and emotional well-being and areas that may need attention or could benefit from positive reinforcement.
Offer Personalized Recommendations:
Suggest practical and actionable strategies the client can implement to improve their mental, emotional, and physical well-being.
Ensure the recommendations are evidence-based and tailored to the client's specific experiences and needs.
Please present your analysis, summary, and recommendations clearly and compassionately, keeping in mind the client's perspective and fostering a supportive tone.
Here is the data and talk to the person in the first person.
MOOD DATA: {mood_data}
THE RESPONSE MUST BE IN MARKDOWN FORMAT ONLY.
'''
else:
# Prepare the prompt with the entire conversation history and mood data
prompt = f'''
USER QUESTION: {user_input}
CONTEXT:
You are a highly experienced psychologist with dedicated clinical practice in mental health, holding a Ph.D. in Clinical Psychology.
You specialize in cognitive-behavioral therapy, mood disorders, and have extensive experience training
CIA and MI6 clandestine officers and HUMINT (Human Intelligence) officers.
Your expertise includes understanding human behavior, interrogation and elicitation techniques, stress management
and resilience building, psychological profiling,
cross-cultural competence, ethical decision-making, and adaptability training. You are renowned for your empathetic,
client-centered approach that fosters a safe and trusting environment.
You possess a keen insight into identifying underlying patterns in thoughts, emotions, and behaviors,
and you utilize evidence-based strategies tailored to each individual's unique experiences.
Your communication style is warm and non-judgmental, demonstrating cultural competence and
sensitivity to diverse backgrounds and needs. You are committed to empowering individuals by facilitating
personal growth, resilience, and improved mental, emotional, and physical well-being.
**Important Instruction:**
- Do not refer to the background data unless it is relevant to the response.
BACKGROUND DATA:
{mood_data}
THE RESPONSE MUST BE IN MARKDOWN FORMAT ONLY.
'''
print(prompt)
async def generate():
global chat_history # Ensure chat_history is global
try:
response = await openai.ChatCompletion.acreate(
model='gpt-4o',
messages=[
{'role': 'system', 'content': "Do not refer to the background data unless it is relevant to the response. If it is relevant tot he response us it in relation to the question. Do not just give a summary."},
{'role': 'user', 'content': prompt}
],
temperature=0,
stream=True
)
assistant_response = "" # Initialize the assistant response buffer
async for chunk in response:
content = chunk.choices[0].delta.get('content', "")
if content:
assistant_response += content # Append content to the buffer
yield content # Stream the content back to the user as it is generated
# Once the entire response is complete, append the full response to chat history
chat_history.append({"role": "assistant", "content": assistant_response})
except Exception as e:
print(e)
yield f"\n[Error] An error occurred while processing your request. Exception: {e}"
# Return a StreamingResponse to stream the assistant's response back to the user
return StreamingResponse(generate(), media_type='text/plain')
# Utility function to fetch status by date
def get_status_by_date(record_date: str):
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
cursor.execute("SELECT * FROM status_checks WHERE date = ?", (record_date,))
record = cursor.fetchone()
conn.close()
if record:
# Map the results into a dictionary
fields = ['date', 'wake_up_mental', 'wake_up_emotional', 'wake_up_physical',
'post_breakfast_mental', 'post_breakfast_emotional', 'post_breakfast_physical', 'post_breakfast_extra',
'post_lunch_mental', 'post_lunch_emotional', 'post_lunch_physical', 'post_lunch_extra',
'post_dinner_mental', 'post_dinner_emotional', 'post_dinner_physical', 'post_dinner_extra',
'bedtime_mental', 'bedtime_emotional', 'bedtime_physical', 'notes_observations', 'exercise_details']
return dict(zip(fields, record))
return None
# Utility function to add or update a status in the database
def upsert_status(status: StatusCheck):
# Retrieve existing data for the given date
existing_record = get_status_by_date(status.date)
# Merge with existing data if available
if existing_record:
updated_record = {**existing_record, **status.dict(exclude_unset=True)}
else:
updated_record = status.dict(exclude_unset=True)
# Insert or update the record in the database
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO status_checks (
date, wake_up_mental, wake_up_emotional, wake_up_physical,
post_breakfast_mental, post_breakfast_emotional, post_breakfast_physical, post_breakfast_extra,
post_lunch_mental, post_lunch_emotional, post_lunch_physical, post_lunch_extra,
post_dinner_mental, post_dinner_emotional, post_dinner_physical, post_dinner_extra,
bedtime_mental, bedtime_emotional, bedtime_physical,
notes_observations, exercise_details
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(date) DO UPDATE SET
wake_up_mental=excluded.wake_up_mental,
wake_up_emotional=excluded.wake_up_emotional,
wake_up_physical=excluded.wake_up_physical,
post_breakfast_mental=excluded.post_breakfast_mental,
post_breakfast_emotional=excluded.post_breakfast_emotional,
post_breakfast_physical=excluded.post_breakfast_physical,
post_breakfast_extra=excluded.post_breakfast_extra,
post_lunch_mental=excluded.post_lunch_mental,
post_lunch_emotional=excluded.post_lunch_emotional,
post_lunch_physical=excluded.post_lunch_physical,
post_lunch_extra=excluded.post_lunch_extra,
post_dinner_mental=excluded.post_dinner_mental,
post_dinner_emotional=excluded.post_dinner_emotional,
post_dinner_physical=excluded.post_dinner_physical,
post_dinner_extra=excluded.post_dinner_extra,
bedtime_mental=excluded.bedtime_mental,
bedtime_emotional=excluded.bedtime_emotional,
bedtime_physical=excluded.bedtime_physical,
notes_observations=excluded.notes_observations,
exercise_details=excluded.exercise_details
''', (
updated_record['date'], updated_record.get('wake_up_mental'), updated_record.get('wake_up_emotional'), updated_record.get('wake_up_physical'),
updated_record.get('post_breakfast_mental'), updated_record.get('post_breakfast_emotional'), updated_record.get('post_breakfast_physical'), updated_record.get('post_breakfast_extra'),
updated_record.get('post_lunch_mental'), updated_record.get('post_lunch_emotional'), updated_record.get('post_lunch_physical'), updated_record.get('post_lunch_extra'),
updated_record.get('post_dinner_mental'), updated_record.get('post_dinner_emotional'), updated_record.get('post_dinner_physical'), updated_record.get('post_dinner_extra'),
updated_record.get('bedtime_mental'), updated_record.get('bedtime_emotional'), updated_record.get('bedtime_physical'),
updated_record.get('notes_observations'), updated_record.get('exercise_details')
))
conn.commit()
conn.close()
# POST endpoint to submit a new status
@app.post("/status")
async def submit_status(status: StatusCheck):
upsert_status(status)
return {"message": "Status submitted successfully"}
# GET endpoint to fetch today's status
@app.get("/status/today")
async def get_today_status():
today = date.today().isoformat()
record = get_status_by_date(today)
return record or {}
# GET endpoint to fetch status by a specific date
@app.get("/status/{date}")
async def get_status_for_date(date: str):
record = get_status_by_date(date)
return record or {}