File size: 2,974 Bytes
f11641a
5b4755e
a188e11
 
 
 
56b4d17
 
 
 
a188e11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56b4d17
 
a188e11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56b4d17
a188e11
56b4d17
a188e11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56b4d17
a188e11
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
import streamlit as st
from datetime import datetime
from db import init_connection
from user import register_user, login_user, get_user_info
from history import save_history, get_user_history
from groq_api import get_groq_response  # Simulated or to be implemented

st.set_page_config(page_title="Health Assistant", layout="centered")
conn = init_connection()

# Session state
if "authenticated" not in st.session_state:
    st.session_state.authenticated = False
if "user_id" not in st.session_state:
    st.session_state.user_id = ""

st.title("🩺 Health Assistant")

# -------------------------
# Authentication
# -------------------------
tab1, tab2 = st.tabs(["πŸ” Login", "πŸ†• Create Account"])

with tab1:
    st.subheader("Login")
    user_id = st.text_input("User ID")
    password = st.text_input("Password", type="password")
    if st.button("Login"):
        if login_user(conn, user_id, password):
            st.session_state.authenticated = True
            st.session_state.user_id = user_id
            st.success("Login successful!")
        else:
            st.error("Invalid credentials.")

with tab2:
    st.subheader("Create Account")
    new_user_id = st.text_input("Choose a User ID")
    name = st.text_input("Full Name")
    dob = st.date_input("Date of Birth", max_value=datetime.today())
    gender = st.selectbox("Gender", ["Male", "Female", "Other"])
    new_password = st.text_input("Set a Password", type="password")
    if st.button("Register"):
        success, msg = register_user(conn, new_user_id, name, dob, gender, new_password)
        if success:
            st.success(msg)
        else:
            st.error(msg)

# -------------------------
# Main App Logic
# -------------------------
if st.session_state.authenticated:
    user_id = st.session_state.user_id
    user = get_user_info(conn, user_id)
    age = (datetime.today().date() - user['dob']).days // 365 if user['dob'] else "N/A"
    
    st.markdown(f"**πŸ‘€ Name:** {user['name']}  \n**πŸŽ‚ Age:** {age}  \n**⚧ Gender:** {user['gender']}")

    st.divider()
    st.subheader("πŸ“ Symptom Checker")
    symptoms = st.text_area("Describe your symptoms")

    if st.button("Get Possible Causes"):
        if symptoms.strip():
            query = f"Symptoms: {symptoms}"
            response = get_groq_response(query)
            st.markdown("#### πŸ€– Assistant Response")
            st.write(response)
            save_history(conn, user_id, query, response, symptoms)
        else:
            st.warning("Please enter your symptoms.")

    st.divider()
    st.subheader("πŸ“œ Your History")

    history = get_user_history(conn, user_id)
    if history:
        for item in history:
            with st.expander(f"{item['created_at'].strftime('%Y-%m-%d %H:%M')} β€” {item['symptoms']}"):
                st.markdown(f"**Query:** {item['query']}")
                st.markdown(f"**Response:** {item['response']}")
    else:
        st.info("No history found.")