...
Browse files- app.py +73 -42
- db.py +0 -17
- diagnosis.py +17 -0
- history.py +18 -0
- prompts.py +2 -0
- qroq_api.py +12 -0
- user.py +32 -0
- user_auth.py +0 -28
app.py
CHANGED
@@ -1,53 +1,84 @@
|
|
1 |
import streamlit as st
|
2 |
-
from db import init_connection, get_history_for_user, save_history
|
3 |
-
from user_auth import login_user, register_user, get_user_info
|
4 |
from datetime import datetime
|
5 |
-
import
|
|
|
|
|
|
|
6 |
|
7 |
st.set_page_config(page_title="Health Assistant", layout="centered")
|
8 |
conn = init_connection()
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
st.
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
user_id = st.text_input("User ID")
|
15 |
password = st.text_input("Password", type="password")
|
16 |
-
if
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
st.
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
|
|
31 |
# Main App Logic
|
32 |
-
|
33 |
-
|
34 |
-
st.
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
else:
|
51 |
-
|
52 |
-
with st.expander(f"{h['timestamp']} | Symptoms: {h['symptoms']}"):
|
53 |
-
st.write(h['result'])
|
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
from datetime import datetime
|
3 |
+
from db import init_connection
|
4 |
+
from user import register_user, login_user, get_user_info
|
5 |
+
from history import save_history, get_user_history
|
6 |
+
from groq_api import get_groq_response # Simulated or to be implemented
|
7 |
|
8 |
st.set_page_config(page_title="Health Assistant", layout="centered")
|
9 |
conn = init_connection()
|
10 |
|
11 |
+
# Session state
|
12 |
+
if "authenticated" not in st.session_state:
|
13 |
+
st.session_state.authenticated = False
|
14 |
+
if "user_id" not in st.session_state:
|
15 |
+
st.session_state.user_id = ""
|
16 |
+
|
17 |
+
st.title("🩺 Health Assistant")
|
18 |
+
|
19 |
+
# -------------------------
|
20 |
+
# Authentication
|
21 |
+
# -------------------------
|
22 |
+
tab1, tab2 = st.tabs(["🔐 Login", "🆕 Create Account"])
|
23 |
+
|
24 |
+
with tab1:
|
25 |
+
st.subheader("Login")
|
26 |
user_id = st.text_input("User ID")
|
27 |
password = st.text_input("Password", type="password")
|
28 |
+
if st.button("Login"):
|
29 |
+
if login_user(conn, user_id, password):
|
30 |
+
st.session_state.authenticated = True
|
31 |
+
st.session_state.user_id = user_id
|
32 |
+
st.success("Login successful!")
|
33 |
+
else:
|
34 |
+
st.error("Invalid credentials.")
|
35 |
+
|
36 |
+
with tab2:
|
37 |
+
st.subheader("Create Account")
|
38 |
+
new_user_id = st.text_input("Choose a User ID")
|
39 |
+
name = st.text_input("Full Name")
|
40 |
+
dob = st.date_input("Date of Birth", max_value=datetime.today())
|
41 |
+
gender = st.selectbox("Gender", ["Male", "Female", "Other"])
|
42 |
+
new_password = st.text_input("Set a Password", type="password")
|
43 |
+
if st.button("Register"):
|
44 |
+
success, msg = register_user(conn, new_user_id, name, dob, gender, new_password)
|
45 |
+
if success:
|
46 |
+
st.success(msg)
|
47 |
+
else:
|
48 |
+
st.error(msg)
|
49 |
|
50 |
+
# -------------------------
|
51 |
# Main App Logic
|
52 |
+
# -------------------------
|
53 |
+
if st.session_state.authenticated:
|
54 |
+
user_id = st.session_state.user_id
|
55 |
+
user = get_user_info(conn, user_id)
|
56 |
+
age = (datetime.today().date() - user['dob']).days // 365 if user['dob'] else "N/A"
|
57 |
+
|
58 |
+
st.markdown(f"**👤 Name:** {user['name']} \n**🎂 Age:** {age} \n**⚧ Gender:** {user['gender']}")
|
59 |
+
|
60 |
+
st.divider()
|
61 |
+
st.subheader("📝 Symptom Checker")
|
62 |
+
symptoms = st.text_area("Describe your symptoms")
|
63 |
+
|
64 |
+
if st.button("Get Possible Causes"):
|
65 |
+
if symptoms.strip():
|
66 |
+
query = f"Symptoms: {symptoms}"
|
67 |
+
response = get_groq_response(query)
|
68 |
+
st.markdown("#### 🤖 Assistant Response")
|
69 |
+
st.write(response)
|
70 |
+
save_history(conn, user_id, query, response, symptoms)
|
71 |
+
else:
|
72 |
+
st.warning("Please enter your symptoms.")
|
73 |
+
|
74 |
+
st.divider()
|
75 |
+
st.subheader("📜 Your History")
|
76 |
+
|
77 |
+
history = get_user_history(conn, user_id)
|
78 |
+
if history:
|
79 |
+
for item in history:
|
80 |
+
with st.expander(f"{item['created_at'].strftime('%Y-%m-%d %H:%M')} — {item['symptoms']}"):
|
81 |
+
st.markdown(f"**Query:** {item['query']}")
|
82 |
+
st.markdown(f"**Response:** {item['response']}")
|
83 |
else:
|
84 |
+
st.info("No history found.")
|
|
|
|
db.py
CHANGED
@@ -3,20 +3,3 @@ import os
|
|
3 |
|
4 |
def init_connection():
|
5 |
return psycopg2.connect(os.environ["DB_CONNECT"])
|
6 |
-
|
7 |
-
def save_history(conn, user_id, timestamp, symptoms, result):
|
8 |
-
with conn.cursor() as cur:
|
9 |
-
cur.execute("""
|
10 |
-
INSERT INTO mb_history (user_id, timestamp, symptoms, result)
|
11 |
-
VALUES (%s, %s, %s, %s)
|
12 |
-
""", (user_id, timestamp, symptoms, result))
|
13 |
-
conn.commit()
|
14 |
-
|
15 |
-
def get_history_for_user(conn, user_id):
|
16 |
-
with conn.cursor() as cur:
|
17 |
-
cur.execute("""
|
18 |
-
SELECT timestamp, symptoms, result FROM mb_history
|
19 |
-
WHERE user_id = %s ORDER BY timestamp DESC
|
20 |
-
""", (user_id,))
|
21 |
-
rows = cur.fetchall()
|
22 |
-
return [{"timestamp": r[0], "symptoms": r[1], "result": r[2]} for r in rows]
|
|
|
3 |
|
4 |
def init_connection():
|
5 |
return psycopg2.connect(os.environ["DB_CONNECT"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
diagnosis.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from db import get_connection
|
2 |
+
|
3 |
+
def save_history(user_id, symptoms, diagnosis):
|
4 |
+
with get_connection() as conn, conn.cursor() as cur:
|
5 |
+
cur.execute("INSERT INTO mb_history (user_id, symptoms, diagnosis) VALUES (%s, %s, %s)",
|
6 |
+
(user_id, symptoms, diagnosis))
|
7 |
+
conn.commit()
|
8 |
+
|
9 |
+
def get_history(user_id):
|
10 |
+
with get_connection() as conn, conn.cursor() as cur:
|
11 |
+
cur.execute("""
|
12 |
+
SELECT timestamp, symptoms, diagnosis
|
13 |
+
FROM mb_history
|
14 |
+
WHERE user_id = %s
|
15 |
+
ORDER BY timestamp DESC
|
16 |
+
""", (user_id,))
|
17 |
+
return cur.fetchall()
|
history.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def save_history(conn, user_id, query, response, symptoms):
|
2 |
+
cur = conn.cursor()
|
3 |
+
cur.execute("""
|
4 |
+
INSERT INTO mb_history (user_id, query, response, symptoms)
|
5 |
+
VALUES (%s, %s, %s, %s)
|
6 |
+
""", (user_id, query, response, symptoms))
|
7 |
+
conn.commit()
|
8 |
+
|
9 |
+
def get_user_history(conn, user_id):
|
10 |
+
cur = conn.cursor()
|
11 |
+
cur.execute("""
|
12 |
+
SELECT query, response, symptoms, created_at
|
13 |
+
FROM mb_history
|
14 |
+
WHERE user_id = %s
|
15 |
+
ORDER BY created_at DESC
|
16 |
+
""", (user_id,))
|
17 |
+
rows = cur.fetchall()
|
18 |
+
return [{"query": r[0], "response": r[1], "symptoms": r[2], "created_at": r[3]} for r in rows]
|
prompts.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
def health_prompt(symptoms):
|
2 |
+
return f"What are the possible causes and precautions for the symptoms: {symptoms}?"
|
qroq_api.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from groq import Groq
|
3 |
+
|
4 |
+
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
5 |
+
|
6 |
+
def get_diagnosis(prompt):
|
7 |
+
response = client.chat.completions.create(
|
8 |
+
model="mixtral-8x7b-32768",
|
9 |
+
messages=[{"role": "user", "content": prompt}],
|
10 |
+
temperature=0.7,
|
11 |
+
)
|
12 |
+
return response.choices[0].message.content.strip()
|
user.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import bcrypt
|
2 |
+
|
3 |
+
def register_user(conn, user_id, name, dob, gender, password):
|
4 |
+
cur = conn.cursor()
|
5 |
+
try:
|
6 |
+
cur.execute("SELECT user_id FROM mb_users WHERE user_id = %s", (user_id,))
|
7 |
+
if cur.fetchone():
|
8 |
+
return False, "User ID already exists."
|
9 |
+
|
10 |
+
hashed_pw = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
|
11 |
+
cur.execute("""
|
12 |
+
INSERT INTO mb_users (user_id, name, dob, gender, password)
|
13 |
+
VALUES (%s, %s, %s, %s, %s)
|
14 |
+
""", (user_id, name, dob, gender, hashed_pw))
|
15 |
+
conn.commit()
|
16 |
+
return True, "Account created successfully."
|
17 |
+
except Exception as e:
|
18 |
+
return False, str(e)
|
19 |
+
|
20 |
+
def login_user(conn, user_id, password):
|
21 |
+
cur = conn.cursor()
|
22 |
+
cur.execute("SELECT password FROM mb_users WHERE user_id = %s", (user_id,))
|
23 |
+
result = cur.fetchone()
|
24 |
+
if result and bcrypt.checkpw(password.encode(), result[0].encode()):
|
25 |
+
return True
|
26 |
+
return False
|
27 |
+
|
28 |
+
def get_user_info(conn, user_id):
|
29 |
+
cur = conn.cursor()
|
30 |
+
cur.execute("SELECT name, dob, gender FROM mb_users WHERE user_id = %s", (user_id,))
|
31 |
+
result = cur.fetchone()
|
32 |
+
return {"name": result[0], "dob": result[1], "gender": result[2]} if result else {}
|
user_auth.py
DELETED
@@ -1,28 +0,0 @@
|
|
1 |
-
import hashlib
|
2 |
-
|
3 |
-
def hash_password(password):
|
4 |
-
return hashlib.sha256(password.encode()).hexdigest()
|
5 |
-
|
6 |
-
def login_user(conn, user_id, password):
|
7 |
-
with conn.cursor() as cur:
|
8 |
-
cur.execute("SELECT password FROM mb_users WHERE user_id = %s", (user_id,))
|
9 |
-
row = cur.fetchone()
|
10 |
-
return row and row[0] == hash_password(password)
|
11 |
-
|
12 |
-
def register_user(conn, user_id, password, name, age, sex):
|
13 |
-
with conn.cursor() as cur:
|
14 |
-
cur.execute("SELECT user_id FROM mb_users WHERE user_id = %s", (user_id,))
|
15 |
-
if cur.fetchone():
|
16 |
-
return False, "User ID already exists."
|
17 |
-
cur.execute("""
|
18 |
-
INSERT INTO mb_users (user_id, password, name, age, sex)
|
19 |
-
VALUES (%s, %s, %s, %s, %s)
|
20 |
-
""", (user_id, hash_password(password), name, age, sex))
|
21 |
-
conn.commit()
|
22 |
-
return True, "Registered successfully."
|
23 |
-
|
24 |
-
def get_user_info(conn, user_id):
|
25 |
-
with conn.cursor() as cur:
|
26 |
-
cur.execute("SELECT name, age, sex FROM mb_users WHERE user_id = %s", (user_id,))
|
27 |
-
row = cur.fetchone()
|
28 |
-
return {"name": row[0], "age": row[1], "sex": row[2]} if row else {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|