abidkh commited on
Commit
56b4d17
·
1 Parent(s): 93054e0

Switch to db.

Browse files
Files changed (7) hide show
  1. app.py +50 -74
  2. db.py +22 -0
  3. groq_api.py +0 -21
  4. history.py +0 -21
  5. prompts.py +0 -12
  6. requirements.txt +4 -2
  7. user_auth.py +28 -0
app.py CHANGED
@@ -1,77 +1,53 @@
1
  import streamlit as st
 
 
2
  from datetime import datetime
3
- from streamlit_cookies_manager import EncryptedCookieManager
4
- from uuid import uuid4
5
- import hashlib
6
-
7
- # Ensure secret is set
8
- cookies = EncryptedCookieManager(prefix="health_app_", password="your_cookie_secret_here")
9
- if not cookies.ready():
10
- st.stop()
11
-
12
- # Generate or retrieve session ID
13
- user_id = cookies.get("user_id")
14
- if not user_id:
15
- user_id = hashlib.sha256(str(uuid4()).encode()).hexdigest()
16
- cookies["user_id"] = user_id
17
-
18
- # Retrieve saved details
19
- user_name = cookies.get("user_name")
20
- user_gender = cookies.get("user_gender")
21
- user_age = cookies.get("user_age")
22
-
23
- st.title("🩺 AI Health Assistant")
24
-
25
- # User Info Form
26
- with st.expander("👤 User Information", expanded=not all([user_name, user_gender, user_age])):
27
- name = st.text_input("Name", value=user_name or "")
28
- gender = st.selectbox("Gender", ["Male", "Female", "Other"], index=["Male", "Female", "Other"].index(user_gender) if user_gender else 0)
29
-
30
- # Safe age default
31
- default_age = int(user_age) if user_age and user_age.isdigit() and 1 <= int(user_age) <= 120 else 25
32
- age = st.number_input("Age", min_value=1, max_value=120, value=default_age)
33
-
34
- if st.button("Save Info"):
35
- cookies["user_name"] = name
36
- cookies["user_gender"] = gender
37
- cookies["user_age"] = str(age)
38
- st.success("User info saved!")
39
-
40
- # Session storage for history
41
- if "history" not in st.session_state:
42
- st.session_state.history = []
43
-
44
- st.markdown("---")
45
- st.subheader("📝 Describe your symptoms:")
46
-
47
- user_input = st.text_area("Enter symptoms", height=100)
48
-
49
- if st.button("Diagnose"):
50
- if not user_input.strip():
51
- st.warning("Please enter some symptoms.")
52
  else:
53
- now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
54
- # Simulated diagnosis result
55
- diagnosis = f"Based on symptoms, possible cause: 'Common cold' or 'Flu'. Please consult a doctor for confirmation."
56
-
57
- # Extract keywords (simple placeholder logic)
58
- keywords = ", ".join(user_input.lower().replace('.', '').replace(',', '').split()[:5])
59
-
60
- st.session_state.history.append({
61
- "timestamp": now,
62
- "symptoms": keywords,
63
- "diagnosis": diagnosis
64
- })
65
-
66
- st.success(" Diagnosis complete!")
67
- st.write(diagnosis)
68
-
69
- st.markdown("---")
70
- st.subheader("📚 Previous History")
71
-
72
- if not st.session_state.history:
73
- st.info("No previous history available.")
74
- else:
75
- for idx, entry in reversed(list(enumerate(st.session_state.history))):
76
- with st.expander(f"{entry['timestamp']} | Symptoms: {entry['symptoms']}"):
77
- st.write(entry["diagnosis"])
 
 
 
 
 
 
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 os
6
+
7
+ st.set_page_config(page_title="Health Assistant", layout="centered")
8
+ conn = init_connection()
9
+
10
+ # Sidebar - Login/Register
11
+ with st.sidebar:
12
+ st.title("🔐 User Login")
13
+ action = st.radio("Choose action:", ["Login", "Register"])
14
+ user_id = st.text_input("User ID")
15
+ password = st.text_input("Password", type="password")
16
+ if action == "Register":
17
+ name = st.text_input("Full Name")
18
+ age = st.number_input("Age", min_value=1, max_value=120, step=1)
19
+ sex = st.selectbox("Sex", ["Male", "Female", "Other"])
20
+ if st.button("Register"):
21
+ success, msg = register_user(conn, user_id, password, name, age, sex)
22
+ st.info(msg)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  else:
24
+ if st.button("Login"):
25
+ if login_user(conn, user_id, password):
26
+ st.session_state.user_id = user_id
27
+ st.success("Login successful")
28
+ else:
29
+ st.error("Invalid credentials")
30
+
31
+ # Main App Logic
32
+ if "user_id" in st.session_state:
33
+ user_info = get_user_info(conn, st.session_state.user_id)
34
+ st.markdown(f"### 👤 Welcome, {user_info['name']} ({user_info['sex']}, {user_info['age']} yrs)")
35
+
36
+ st.header("💬 Symptom Checker")
37
+ query = st.text_area("Describe your symptoms")
38
+ if st.button("Analyze Symptoms"):
39
+ if query.strip():
40
+ keywords = [word.strip(".,") for word in query.split() if len(word) > 4]
41
+ result = f"Possible causes for symptoms like: {', '.join(keywords[:5])}..."
42
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
43
+ save_history(conn, st.session_state.user_id, timestamp, ", ".join(keywords[:5]), result)
44
+ st.success(result)
45
+
46
+ st.markdown("### 📜 Previous History")
47
+ history = get_history_for_user(conn, st.session_state.user_id)
48
+ if not history:
49
+ st.info("No previous history found.")
50
+ else:
51
+ for h in history:
52
+ with st.expander(f"{h['timestamp']} | Symptoms: {h['symptoms']}"):
53
+ st.write(h['result'])
db.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import psycopg2
2
+ 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]
groq_api.py DELETED
@@ -1,21 +0,0 @@
1
- import os
2
- import requests
3
-
4
- GROQ_API_KEY = os.getenv("GROQ_API_KEY")
5
- MODEL = "llama3-70b-8192" # or "mixtral-8x7b-32768"
6
-
7
- def query_groq(prompt: str) -> str:
8
- url = "https://api.groq.com/openai/v1/chat/completions"
9
- headers = {
10
- "Authorization": f"Bearer {GROQ_API_KEY}",
11
- "Content-Type": "application/json"
12
- }
13
- payload = {
14
- "model": MODEL,
15
- "messages": [{"role": "user", "content": prompt}],
16
- "temperature": 0.6
17
- }
18
-
19
- res = requests.post(url, headers=headers, json=payload)
20
- res.raise_for_status()
21
- return res.json()["choices"][0]["message"]["content"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
history.py DELETED
@@ -1,21 +0,0 @@
1
- import json
2
- import os
3
-
4
- def get_user_history_file(user_id: str) -> str:
5
- return f"user_history_{user_id}.json"
6
-
7
- def save_interaction(user_id: str, entry: dict):
8
- history = load_history(user_id)
9
- history.append(entry)
10
- with open(get_user_history_file(user_id), "w") as f:
11
- json.dump(history, f)
12
-
13
- def load_history(user_id: str):
14
- file = get_user_history_file(user_id)
15
- if not os.path.exists(file):
16
- return []
17
- try:
18
- with open(file, "r") as f:
19
- return json.load(f)
20
- except Exception:
21
- return []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
prompts.py DELETED
@@ -1,12 +0,0 @@
1
- SYMPTOM_PROMPT_TEMPLATE = """
2
- You are an experienced medical assistant. A patient reports the following symptoms: {symptoms}.
3
- Please list possible causes and suggest general precautions.
4
- Avoid giving any direct diagnosis. Keep your answer informative, clear, and responsible.
5
- """
6
-
7
- QUESTION_PROMPT_TEMPLATE = """
8
- You are an AI medical assistant trained in clinical and health knowledge.
9
- Answer the following health-related question in a responsible, factual, and non-diagnostic manner.
10
-
11
- Question: {question}
12
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,4 +1,6 @@
1
  streamlit
2
- requests
3
- streamlit-cookies-manager
 
 
4
  groq
 
1
  streamlit
2
+ psycopg2-binary
3
+ sqlalchemy
4
+ bcrypt
5
+ python-dotenv
6
  groq
user_auth.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 {}