Spaces:
Running
Running
Sayon Bhattacharyya commited on
Commit ·
6738aa5
1
Parent(s): d2fbe0b
commit
Browse files- src/database.py +82 -82
- src/streamlit_app.py +1 -1
src/database.py
CHANGED
|
@@ -1,96 +1,96 @@
|
|
| 1 |
-
# database.py
|
| 2 |
-
import sqlite3
|
| 3 |
-
import json
|
| 4 |
-
from datetime import datetime
|
| 5 |
-
from typing import Optional, Dict
|
| 6 |
|
| 7 |
-
DB_PATH = "users.db"
|
| 8 |
|
| 9 |
-
# Ensure DB and table are created on import
|
| 10 |
-
def init_db():
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
|
| 29 |
-
def save_user_data(session_alias: str, user_data: Dict):
|
| 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 |
-
def get_user_data(session_alias: str) -> Optional[Dict]:
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
|
| 81 |
|
| 82 |
-
def create_user(session_alias: str, name: str):
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
|
| 94 |
|
| 95 |
-
# Always call on app start
|
| 96 |
-
init_db()
|
|
|
|
| 1 |
+
# # database.py
|
| 2 |
+
# import sqlite3
|
| 3 |
+
# import json
|
| 4 |
+
# from datetime import datetime
|
| 5 |
+
# from typing import Optional, Dict
|
| 6 |
|
| 7 |
+
# DB_PATH = "users.db"
|
| 8 |
|
| 9 |
+
# # Ensure DB and table are created on import
|
| 10 |
+
# def init_db():
|
| 11 |
+
# conn = sqlite3.connect(DB_PATH)
|
| 12 |
+
# cursor = conn.cursor()
|
| 13 |
+
# cursor.execute("""
|
| 14 |
+
# CREATE TABLE IF NOT EXISTS users (
|
| 15 |
+
# session_alias TEXT PRIMARY KEY,
|
| 16 |
+
# name TEXT,
|
| 17 |
+
# created_at TEXT,
|
| 18 |
+
# profile TEXT,
|
| 19 |
+
# chat_history TEXT,
|
| 20 |
+
# workout_plan TEXT,
|
| 21 |
+
# nutrition_plan TEXT,
|
| 22 |
+
# last_updated TEXT
|
| 23 |
+
# )
|
| 24 |
+
# """)
|
| 25 |
+
# conn.commit()
|
| 26 |
+
# conn.close()
|
| 27 |
|
| 28 |
|
| 29 |
+
# def save_user_data(session_alias: str, user_data: Dict):
|
| 30 |
+
# conn = sqlite3.connect(DB_PATH)
|
| 31 |
+
# cursor = conn.cursor()
|
| 32 |
|
| 33 |
+
# cursor.execute("""
|
| 34 |
+
# INSERT INTO users (
|
| 35 |
+
# session_alias, name, created_at, profile, chat_history,
|
| 36 |
+
# workout_plan, nutrition_plan, last_updated
|
| 37 |
+
# ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
| 38 |
+
# ON CONFLICT(session_alias) DO UPDATE SET
|
| 39 |
+
# name=excluded.name,
|
| 40 |
+
# profile=excluded.profile,
|
| 41 |
+
# chat_history=excluded.chat_history,
|
| 42 |
+
# workout_plan=excluded.workout_plan,
|
| 43 |
+
# nutrition_plan=excluded.nutrition_plan,
|
| 44 |
+
# last_updated=excluded.last_updated
|
| 45 |
+
# """, (
|
| 46 |
+
# session_alias,
|
| 47 |
+
# user_data.get("name", ""),
|
| 48 |
+
# user_data.get("created_at", datetime.now().isoformat()),
|
| 49 |
+
# json.dumps(user_data.get("profile", {})),
|
| 50 |
+
# json.dumps(user_data.get("chat_history", [])),
|
| 51 |
+
# json.dumps(user_data.get("workout_plan", {})),
|
| 52 |
+
# json.dumps(user_data.get("nutrition_plan", {})),
|
| 53 |
+
# datetime.now().isoformat()
|
| 54 |
+
# ))
|
| 55 |
|
| 56 |
+
# conn.commit()
|
| 57 |
+
# conn.close()
|
| 58 |
|
| 59 |
|
| 60 |
+
# def get_user_data(session_alias: str) -> Optional[Dict]:
|
| 61 |
+
# conn = sqlite3.connect(DB_PATH)
|
| 62 |
+
# cursor = conn.cursor()
|
| 63 |
+
# cursor.execute("SELECT * FROM users WHERE session_alias = ?", (session_alias,))
|
| 64 |
+
# row = cursor.fetchone()
|
| 65 |
+
# conn.close()
|
| 66 |
|
| 67 |
+
# if not row:
|
| 68 |
+
# return None
|
| 69 |
|
| 70 |
+
# return {
|
| 71 |
+
# "session_alias": row[0],
|
| 72 |
+
# "name": row[1],
|
| 73 |
+
# "created_at": row[2],
|
| 74 |
+
# "profile": json.loads(row[3] or "{}"),
|
| 75 |
+
# "chat_history": json.loads(row[4] or "[]"),
|
| 76 |
+
# "workout_plan": json.loads(row[5] or "{}"),
|
| 77 |
+
# "nutrition_plan": json.loads(row[6] or "{}"),
|
| 78 |
+
# "last_updated": row[7]
|
| 79 |
+
# }
|
| 80 |
|
| 81 |
|
| 82 |
+
# def create_user(session_alias: str, name: str):
|
| 83 |
+
# # Only called when new user logs in
|
| 84 |
+
# user_data = {
|
| 85 |
+
# "name": name,
|
| 86 |
+
# "created_at": datetime.now().isoformat(),
|
| 87 |
+
# "profile": {},
|
| 88 |
+
# "chat_history": [],
|
| 89 |
+
# "workout_plan": {},
|
| 90 |
+
# "nutrition_plan": {}
|
| 91 |
+
# }
|
| 92 |
+
# save_user_data(session_alias, user_data)
|
| 93 |
|
| 94 |
|
| 95 |
+
# # Always call on app start
|
| 96 |
+
# init_db()
|
src/streamlit_app.py
CHANGED
|
@@ -6,7 +6,7 @@ import pandas as pd
|
|
| 6 |
import hashlib
|
| 7 |
import os
|
| 8 |
from service import generate_ai_response, create_user_profile_prompt, create_workout_type_prompt, create_nutrition_type_prompt, create_conversation_chat_prompt
|
| 9 |
-
from database import create_user, get_user_data, save_user_data
|
| 10 |
|
| 11 |
# Configure page
|
| 12 |
st.set_page_config(
|
|
|
|
| 6 |
import hashlib
|
| 7 |
import os
|
| 8 |
from service import generate_ai_response, create_user_profile_prompt, create_workout_type_prompt, create_nutrition_type_prompt, create_conversation_chat_prompt
|
| 9 |
+
# from database import create_user, get_user_data, save_user_data
|
| 10 |
|
| 11 |
# Configure page
|
| 12 |
st.set_page_config(
|