...
Browse files- app.py +75 -15
- history.py +10 -9
app.py
CHANGED
@@ -1,38 +1,98 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
from groq_api import query_groq
|
3 |
from history import save_interaction, load_history
|
4 |
|
5 |
-
|
|
|
|
|
6 |
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
9 |
if "history" not in st.session_state:
|
10 |
-
st.session_state.history = load_history()
|
|
|
|
|
11 |
|
12 |
mode = st.radio("Select mode", ["Symptom Checker", "Health Q&A"])
|
13 |
|
|
|
|
|
|
|
|
|
|
|
14 |
if mode == "Symptom Checker":
|
15 |
symptoms = st.text_area("Enter your symptoms", placeholder="e.g., headache, fever, sore throat")
|
16 |
if st.button("Analyze Symptoms"):
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
response = query_groq(prompt)
|
19 |
st.success(response)
|
20 |
-
|
21 |
-
st.session_state.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
elif mode == "Health Q&A":
|
24 |
question = st.text_input("Ask your health question")
|
25 |
if st.button("Get Answer"):
|
26 |
-
|
|
|
|
|
|
|
|
|
27 |
response = query_groq(prompt)
|
28 |
st.success(response)
|
29 |
-
save_interaction("Health Q&A", question, response)
|
30 |
-
st.session_state.history.append(("Health Q&A", question, response))
|
31 |
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
with st.expander("π Show Previous Interactions"):
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import uuid
|
3 |
+
import os
|
4 |
+
from datetime import datetime
|
5 |
from groq_api import query_groq
|
6 |
from history import save_interaction, load_history
|
7 |
|
8 |
+
# Generate session-based cookie ID
|
9 |
+
if "user_id" not in st.session_state:
|
10 |
+
st.session_state.user_id = str(uuid.uuid4())
|
11 |
|
12 |
+
# Profile info (name, age, gender)
|
13 |
+
with st.sidebar:
|
14 |
+
st.header("π€ Your Info")
|
15 |
+
st.session_state.user_name = st.text_input("Name", st.session_state.get("user_name", ""))
|
16 |
+
st.session_state.user_age = st.number_input("Age", 10, 120, value=30)
|
17 |
+
st.session_state.user_gender = st.selectbox("Gender", ["Male", "Female", "Other"])
|
18 |
|
19 |
+
# Load history for this session
|
20 |
if "history" not in st.session_state:
|
21 |
+
st.session_state.history = load_history(st.session_state.user_id)
|
22 |
+
|
23 |
+
st.title("π©Ί AI Health Assistant")
|
24 |
|
25 |
mode = st.radio("Select mode", ["Symptom Checker", "Health Q&A"])
|
26 |
|
27 |
+
def extract_keywords(symptoms: str) -> str:
|
28 |
+
prompt = f"Extract the main symptoms from this text as comma-separated keywords: {symptoms}"
|
29 |
+
keywords = query_groq(prompt)
|
30 |
+
return keywords.strip().strip(".,").lower()
|
31 |
+
|
32 |
if mode == "Symptom Checker":
|
33 |
symptoms = st.text_area("Enter your symptoms", placeholder="e.g., headache, fever, sore throat")
|
34 |
if st.button("Analyze Symptoms"):
|
35 |
+
dt = datetime.now().strftime("%Y-%m-%d %H:%M")
|
36 |
+
keywords = extract_keywords(symptoms)
|
37 |
+
prompt = (
|
38 |
+
f"A user named {st.session_state.user_name} (Age: {st.session_state.user_age}, "
|
39 |
+
f"Gender: {st.session_state.user_gender}) reports: {symptoms}. "
|
40 |
+
"List possible causes and precautions."
|
41 |
+
)
|
42 |
response = query_groq(prompt)
|
43 |
st.success(response)
|
44 |
+
|
45 |
+
save_interaction(st.session_state.user_id, {
|
46 |
+
"mode": "Symptom Checker",
|
47 |
+
"datetime": dt,
|
48 |
+
"query": symptoms,
|
49 |
+
"keywords": keywords,
|
50 |
+
"response": response
|
51 |
+
})
|
52 |
+
st.session_state.history.append({
|
53 |
+
"mode": "Symptom Checker",
|
54 |
+
"datetime": dt,
|
55 |
+
"query": symptoms,
|
56 |
+
"keywords": keywords,
|
57 |
+
"response": response
|
58 |
+
})
|
59 |
|
60 |
elif mode == "Health Q&A":
|
61 |
question = st.text_input("Ask your health question")
|
62 |
if st.button("Get Answer"):
|
63 |
+
dt = datetime.now().strftime("%Y-%m-%d %H:%M")
|
64 |
+
prompt = (
|
65 |
+
f"User named {st.session_state.user_name} (Age: {st.session_state.user_age}, "
|
66 |
+
f"Gender: {st.session_state.user_gender}) asked: {question}"
|
67 |
+
)
|
68 |
response = query_groq(prompt)
|
69 |
st.success(response)
|
|
|
|
|
70 |
|
71 |
+
save_interaction(st.session_state.user_id, {
|
72 |
+
"mode": "Health Q&A",
|
73 |
+
"datetime": dt,
|
74 |
+
"query": question,
|
75 |
+
"keywords": "",
|
76 |
+
"response": response
|
77 |
+
})
|
78 |
+
st.session_state.history.append({
|
79 |
+
"mode": "Health Q&A",
|
80 |
+
"datetime": dt,
|
81 |
+
"query": question,
|
82 |
+
"keywords": "",
|
83 |
+
"response": response
|
84 |
+
})
|
85 |
+
|
86 |
+
# Display history
|
87 |
with st.expander("π Show Previous Interactions"):
|
88 |
+
if st.session_state.history:
|
89 |
+
for entry in st.session_state.history[::-1]:
|
90 |
+
if entry["mode"] == "Symptom Checker":
|
91 |
+
title = f"{entry['datetime']} | Symptoms: {entry['keywords']}"
|
92 |
+
else:
|
93 |
+
title = f"{entry['datetime']} | Question: {entry['query'][:50]}"
|
94 |
+
with st.expander(title):
|
95 |
+
st.markdown("**Diagnosis / Answer:**")
|
96 |
+
st.write(entry["response"])
|
97 |
+
else:
|
98 |
+
st.info("No history found.")
|
history.py
CHANGED
@@ -1,20 +1,21 @@
|
|
1 |
import json
|
2 |
import os
|
3 |
|
4 |
-
|
|
|
5 |
|
6 |
-
def save_interaction(
|
7 |
-
|
8 |
-
history
|
9 |
-
|
10 |
-
with open(HISTORY_FILE, "w") as f:
|
11 |
json.dump(history, f)
|
12 |
|
13 |
-
def load_history():
|
14 |
-
|
|
|
15 |
return []
|
16 |
try:
|
17 |
-
with open(
|
18 |
return json.load(f)
|
19 |
except Exception:
|
20 |
return []
|
|
|
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 []
|