medibot / app.py
abidkh's picture
Switch to db.
56b4d17
raw
history blame
2.21 kB
import streamlit as st
from db import init_connection, get_history_for_user, save_history
from user_auth import login_user, register_user, get_user_info
from datetime import datetime
import os
st.set_page_config(page_title="Health Assistant", layout="centered")
conn = init_connection()
# Sidebar - Login/Register
with st.sidebar:
st.title("πŸ” User Login")
action = st.radio("Choose action:", ["Login", "Register"])
user_id = st.text_input("User ID")
password = st.text_input("Password", type="password")
if action == "Register":
name = st.text_input("Full Name")
age = st.number_input("Age", min_value=1, max_value=120, step=1)
sex = st.selectbox("Sex", ["Male", "Female", "Other"])
if st.button("Register"):
success, msg = register_user(conn, user_id, password, name, age, sex)
st.info(msg)
else:
if st.button("Login"):
if login_user(conn, user_id, password):
st.session_state.user_id = user_id
st.success("Login successful")
else:
st.error("Invalid credentials")
# Main App Logic
if "user_id" in st.session_state:
user_info = get_user_info(conn, st.session_state.user_id)
st.markdown(f"### πŸ‘€ Welcome, {user_info['name']} ({user_info['sex']}, {user_info['age']} yrs)")
st.header("πŸ’¬ Symptom Checker")
query = st.text_area("Describe your symptoms")
if st.button("Analyze Symptoms"):
if query.strip():
keywords = [word.strip(".,") for word in query.split() if len(word) > 4]
result = f"Possible causes for symptoms like: {', '.join(keywords[:5])}..."
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
save_history(conn, st.session_state.user_id, timestamp, ", ".join(keywords[:5]), result)
st.success(result)
st.markdown("### πŸ“œ Previous History")
history = get_history_for_user(conn, st.session_state.user_id)
if not history:
st.info("No previous history found.")
else:
for h in history:
with st.expander(f"{h['timestamp']} | Symptoms: {h['symptoms']}"):
st.write(h['result'])