Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,9 @@ from datetime import datetime
|
|
| 7 |
from groq import Groq
|
| 8 |
import pdfplumber
|
| 9 |
import plotly.graph_objects as go
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# --- 1. CORE SYSTEM CONFIG ---
|
| 12 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
|
@@ -38,7 +41,6 @@ st.markdown("""
|
|
| 38 |
background-color: #1e3a8a !important;
|
| 39 |
color: white !important;
|
| 40 |
border: 1px solid #3b82f6 !important;
|
| 41 |
-
box-shadow: none !important;
|
| 42 |
}
|
| 43 |
|
| 44 |
/* User Message: Beautiful Blue Gradient */
|
|
@@ -77,6 +79,11 @@ def send_call_request(caller, receiver, room):
|
|
| 77 |
new_req = pd.DataFrame([{"Caller": caller, "Receiver": receiver, "RoomID": room, "Status": "Active"}])
|
| 78 |
pd.concat([df, new_req]).to_csv(CALL_SIGNAL_DB, index=False)
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
# Session Initialization
|
| 81 |
if "logged_in" not in st.session_state: st.session_state.logged_in = False
|
| 82 |
if "msgs" not in st.session_state: st.session_state.msgs = []
|
|
@@ -111,29 +118,28 @@ if not st.session_state.logged_in:
|
|
| 111 |
st.warning("User already exists.")
|
| 112 |
else:
|
| 113 |
pd.concat([df, pd.DataFrame([{"username": nu, "password": hash_pass(np), "role": nr}])]).to_csv(USER_DB, index=False)
|
| 114 |
-
st.success("Account Registered!
|
| 115 |
st.stop()
|
| 116 |
|
| 117 |
# --- 5. SIDEBAR NAVIGATION ---
|
| 118 |
with st.sidebar:
|
| 119 |
st.markdown(f"### π€ {st.session_state.username} ({st.session_state.role})")
|
| 120 |
-
if st.button("ποΈ Clear Chat"):
|
| 121 |
st.session_state.msgs = []
|
| 122 |
st.session_state.active_doc = None
|
| 123 |
st.rerun()
|
| 124 |
if st.button("πͺ Logout"): st.session_state.logged_in = False; st.rerun()
|
| 125 |
st.divider()
|
| 126 |
if st.session_state.role == "Patient":
|
| 127 |
-
nav = st.radio("Menu", ["π¬ AI Chat", "π§ͺ Health Tools", "π Video Consult", "π Call History"])
|
| 128 |
else:
|
| 129 |
nav = st.radio("Menu", ["π₯οΈ Consultation Desk", "π Call Logs"])
|
| 130 |
|
| 131 |
# --- 6. AI CHAT MODULE ---
|
| 132 |
if nav == "π¬ AI Chat":
|
| 133 |
st.markdown("### π¬ Clinical Intelligence Assistant")
|
| 134 |
-
|
| 135 |
if st.session_state.active_doc:
|
| 136 |
-
st.info("π Medical document
|
| 137 |
st.download_button("π Download PDF Summary", data=st.session_state.active_doc, file_name="medical_summary.txt") #
|
| 138 |
|
| 139 |
for m in st.session_state.msgs:
|
|
@@ -161,13 +167,7 @@ if nav == "π¬ AI Chat":
|
|
| 161 |
|
| 162 |
if final_q:
|
| 163 |
st.session_state.msgs.append({"role": "user", "content": final_q})
|
| 164 |
-
sys_p =
|
| 165 |
-
"You are the IntelliCare Medical Assistant. "
|
| 166 |
-
"1. Greet the user professionally as a medical assistant. "
|
| 167 |
-
"2. Strictly answer ONLY medical/health queries. "
|
| 168 |
-
"3. Refuse essays, code, or non-medical topics. "
|
| 169 |
-
"4. Prioritize PDF analysis if context is present."
|
| 170 |
-
)
|
| 171 |
ans = Groq(api_key=GROQ_API_KEY).chat.completions.create(model="llama-3.3-70b-versatile", messages=[{"role": "system", "content": sys_p}, {"role": "system", "content": f"PDF_CONTEXT: {st.session_state.active_doc}"}] + st.session_state.msgs)
|
| 172 |
st.session_state.msgs.append({"role": "assistant", "content": ans.choices[0].message.content})
|
| 173 |
st.rerun()
|
|
@@ -196,7 +196,19 @@ elif nav == "π§ͺ Health Tools":
|
|
| 196 |
fig = go.Figure(data=go.Scatter(x=t, y=ecg, line=dict(color='#ef4444')))
|
| 197 |
st.plotly_chart(fig, use_container_width=True)
|
| 198 |
|
| 199 |
-
# --- 8.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
elif nav == "π Video Consult":
|
| 201 |
st.markdown("### π Specialist Call Request")
|
| 202 |
db = load_db(USER_DB, ["username", "role"])
|
|
@@ -217,7 +229,14 @@ elif nav == "π₯οΈ Consultation Desk":
|
|
| 217 |
my_calls = signals[signals['Receiver'] == st.session_state.username]
|
| 218 |
if not my_calls.empty:
|
| 219 |
st.info(f"π Incoming request from **{my_calls.iloc[0]['Caller']}**")
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
else: st.write("Waiting for requests...")
|
| 222 |
if "d_room" in st.session_state:
|
| 223 |
st.components.v1.html(f'<iframe src="https://meet.jit.si/{st.session_state.d_room}" width="100%" height="600px"></iframe>', height=650)
|
|
|
|
| 7 |
from groq import Groq
|
| 8 |
import pdfplumber
|
| 9 |
import plotly.graph_objects as go
|
| 10 |
+
import folium
|
| 11 |
+
from streamlit_folium import st_folium
|
| 12 |
+
from streamlit_geolocation import streamlit_geolocation
|
| 13 |
|
| 14 |
# --- 1. CORE SYSTEM CONFIG ---
|
| 15 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
|
|
|
| 41 |
background-color: #1e3a8a !important;
|
| 42 |
color: white !important;
|
| 43 |
border: 1px solid #3b82f6 !important;
|
|
|
|
| 44 |
}
|
| 45 |
|
| 46 |
/* User Message: Beautiful Blue Gradient */
|
|
|
|
| 79 |
new_req = pd.DataFrame([{"Caller": caller, "Receiver": receiver, "RoomID": room, "Status": "Active"}])
|
| 80 |
pd.concat([df, new_req]).to_csv(CALL_SIGNAL_DB, index=False)
|
| 81 |
|
| 82 |
+
def clear_call_request(receiver):
|
| 83 |
+
df = load_db(CALL_SIGNAL_DB, ["Caller", "Receiver", "RoomID", "Status"])
|
| 84 |
+
df = df[df['Receiver'] != receiver]
|
| 85 |
+
df.to_csv(CALL_SIGNAL_DB, index=False)
|
| 86 |
+
|
| 87 |
# Session Initialization
|
| 88 |
if "logged_in" not in st.session_state: st.session_state.logged_in = False
|
| 89 |
if "msgs" not in st.session_state: st.session_state.msgs = []
|
|
|
|
| 118 |
st.warning("User already exists.")
|
| 119 |
else:
|
| 120 |
pd.concat([df, pd.DataFrame([{"username": nu, "password": hash_pass(np), "role": nr}])]).to_csv(USER_DB, index=False)
|
| 121 |
+
st.success("Account Registered! Login now.") #
|
| 122 |
st.stop()
|
| 123 |
|
| 124 |
# --- 5. SIDEBAR NAVIGATION ---
|
| 125 |
with st.sidebar:
|
| 126 |
st.markdown(f"### π€ {st.session_state.username} ({st.session_state.role})")
|
| 127 |
+
if st.button("ποΈ Clear Chat"):
|
| 128 |
st.session_state.msgs = []
|
| 129 |
st.session_state.active_doc = None
|
| 130 |
st.rerun()
|
| 131 |
if st.button("πͺ Logout"): st.session_state.logged_in = False; st.rerun()
|
| 132 |
st.divider()
|
| 133 |
if st.session_state.role == "Patient":
|
| 134 |
+
nav = st.radio("Menu", ["π¬ AI Chat", "π§ͺ Health Tools", "π Nearby Clinics", "π Video Consult", "π Call History"])
|
| 135 |
else:
|
| 136 |
nav = st.radio("Menu", ["π₯οΈ Consultation Desk", "π Call Logs"])
|
| 137 |
|
| 138 |
# --- 6. AI CHAT MODULE ---
|
| 139 |
if nav == "π¬ AI Chat":
|
| 140 |
st.markdown("### π¬ Clinical Intelligence Assistant")
|
|
|
|
| 141 |
if st.session_state.active_doc:
|
| 142 |
+
st.info("π Medical document active. Analysis mode engaged.")
|
| 143 |
st.download_button("π Download PDF Summary", data=st.session_state.active_doc, file_name="medical_summary.txt") #
|
| 144 |
|
| 145 |
for m in st.session_state.msgs:
|
|
|
|
| 167 |
|
| 168 |
if final_q:
|
| 169 |
st.session_state.msgs.append({"role": "user", "content": final_q})
|
| 170 |
+
sys_p = "Strict Medical Assistant. Refuse essays/code. Analyze PDFs specifically."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
ans = Groq(api_key=GROQ_API_KEY).chat.completions.create(model="llama-3.3-70b-versatile", messages=[{"role": "system", "content": sys_p}, {"role": "system", "content": f"PDF_CONTEXT: {st.session_state.active_doc}"}] + st.session_state.msgs)
|
| 172 |
st.session_state.msgs.append({"role": "assistant", "content": ans.choices[0].message.content})
|
| 173 |
st.rerun()
|
|
|
|
| 196 |
fig = go.Figure(data=go.Scatter(x=t, y=ecg, line=dict(color='#ef4444')))
|
| 197 |
st.plotly_chart(fig, use_container_width=True)
|
| 198 |
|
| 199 |
+
# --- 8. MAP FEATURE (NEARBY CLINICS) ---
|
| 200 |
+
elif nav == "π Nearby Clinics":
|
| 201 |
+
st.markdown("### π Specialist Hospital Locator")
|
| 202 |
+
loc = streamlit_geolocation()
|
| 203 |
+
if loc.get("latitude"):
|
| 204 |
+
# Display map based on current geolocation
|
| 205 |
+
m = folium.Map(location=[loc["latitude"], loc["longitude"]], zoom_start=14)
|
| 206 |
+
folium.Marker([loc["latitude"], loc["longitude"]], popup="You are here", icon=folium.Icon(color='red')).add_to(m)
|
| 207 |
+
st_folium(m, width=1000, height=500)
|
| 208 |
+
else:
|
| 209 |
+
st.info("Allow location access to find nearby medical facilities.")
|
| 210 |
+
|
| 211 |
+
# --- 9. CALLS & HISTORY ---
|
| 212 |
elif nav == "π Video Consult":
|
| 213 |
st.markdown("### π Specialist Call Request")
|
| 214 |
db = load_db(USER_DB, ["username", "role"])
|
|
|
|
| 229 |
my_calls = signals[signals['Receiver'] == st.session_state.username]
|
| 230 |
if not my_calls.empty:
|
| 231 |
st.info(f"π Incoming request from **{my_calls.iloc[0]['Caller']}**")
|
| 232 |
+
c1, c2 = st.columns(2)
|
| 233 |
+
with c1:
|
| 234 |
+
if st.button("β
Accept & Join"): st.session_state.d_room = my_calls.iloc[0]['RoomID']
|
| 235 |
+
with c2:
|
| 236 |
+
if st.button("β Decline Call"): #
|
| 237 |
+
clear_call_request(st.session_state.username)
|
| 238 |
+
st.warning("Call declined.")
|
| 239 |
+
st.rerun()
|
| 240 |
else: st.write("Waiting for requests...")
|
| 241 |
if "d_room" in st.session_state:
|
| 242 |
st.components.v1.html(f'<iframe src="https://meet.jit.si/{st.session_state.d_room}" width="100%" height="600px"></iframe>', height=650)
|