medibot / app.py
abidkh's picture
...
a188e11
raw
history blame
2.97 kB
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.")