Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,147 +1,86 @@
|
|
1 |
from langchain_google_genai import GoogleGenerativeAI
|
2 |
import streamlit as st
|
3 |
-
import re
|
4 |
-
from datetime import datetime
|
5 |
|
6 |
-
# ---
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
"
|
22 |
-
"
|
23 |
-
"
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
"
|
34 |
-
|
35 |
-
return not any(phrase in text.lower() for phrase in blacklist)
|
36 |
|
37 |
# --- Streamlit App ---
|
38 |
def main():
|
39 |
st.set_page_config(
|
40 |
-
page_title="DoctorX
|
41 |
page_icon="🩺",
|
42 |
-
layout="
|
43 |
-
initial_sidebar_state="
|
44 |
)
|
45 |
|
46 |
-
# Initialize session state
|
47 |
if "history" not in st.session_state:
|
48 |
st.session_state.history = []
|
49 |
-
|
50 |
-
#
|
51 |
with st.sidebar:
|
52 |
-
st.header("
|
53 |
-
for key, value in
|
54 |
st.subheader(key)
|
55 |
st.caption(value)
|
56 |
st.divider()
|
57 |
-
st.caption("⚠️ This is not a substitute for emergency medical care")
|
58 |
|
59 |
-
#
|
60 |
-
st.title("🩺 DoctorX
|
61 |
-
st.caption("AI-powered preliminary health guidance - Always consult a human doctor for final diagnosis")
|
62 |
|
63 |
# Display chat history
|
64 |
for role, message in st.session_state.history:
|
65 |
with st.chat_message(role):
|
66 |
st.markdown(message)
|
67 |
|
68 |
-
# User input
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
with col2:
|
78 |
-
if st.form_submit_button("Send", use_container_width=True):
|
79 |
-
user_input = sanitize_input(user_input.strip())
|
80 |
-
|
81 |
-
if user_input.lower() in ["emergency", "911", "112"]:
|
82 |
-
st.session_state.history.append(("user", user_input))
|
83 |
-
st.session_state.history.append(("bot", "🚨 Please use the emergency contacts in the sidebar immediately!"))
|
84 |
-
st.rerun()
|
85 |
-
|
86 |
-
elif user_input:
|
87 |
-
process_user_input(user_input)
|
88 |
-
|
89 |
-
# Quick symptom buttons
|
90 |
-
common_symptoms = ["Fever", "Headache", "Cough", "Rash", "Nausea"]
|
91 |
-
cols = st.columns(len(common_symptoms))
|
92 |
-
for col, symptom in zip(cols, common_symptoms):
|
93 |
-
if col.button(symptom):
|
94 |
-
process_user_input(f"I'm experiencing {symptom.lower()}")
|
95 |
|
96 |
-
# Quick response buttons
|
97 |
common_queries = ["What are the symptoms of epilepsy?", "How to manage stress?"]
|
98 |
cols = st.columns(len(common_queries))
|
99 |
for col, query in zip(cols, common_queries):
|
100 |
if col.button(query):
|
101 |
-
|
102 |
-
|
103 |
-
def process_user_input(user_input):
|
104 |
-
"""Handle user input and generate AI response"""
|
105 |
-
llm = GoogleGenerativeAI(google_api_key=st.secrets["GOOGLE_API_KEY"], **MODEL_CONFIG)
|
106 |
-
|
107 |
-
# Build medical context
|
108 |
-
system_prompt = f"""
|
109 |
-
You are DoctorX, an AI medical assistant. Follow these rules STRICTLY:
|
110 |
-
1. Never diagnose - suggest POSSIBLE conditions
|
111 |
-
2. Always recommend consulting a human doctor
|
112 |
-
3. Prioritize patient safety over all else
|
113 |
-
4. Maintain professional, empathetic tone
|
114 |
-
5. Disclose your AI nature clearly
|
115 |
-
6. For emergencies, direct to sidebar contacts
|
116 |
-
|
117 |
-
Current Date: {datetime.now().strftime("%Y-%m-%d")}
|
118 |
-
User Input: {user_input}
|
119 |
-
"""
|
120 |
-
|
121 |
-
try:
|
122 |
-
with st.chat_message("user"):
|
123 |
-
st.markdown(user_input)
|
124 |
-
|
125 |
-
with st.chat_message("bot"):
|
126 |
-
with st.spinner("🔍 Analyzing symptoms..."):
|
127 |
-
response = llm.invoke(system_prompt)
|
128 |
-
|
129 |
-
if not validate_medical_response(response):
|
130 |
-
raise ValueError("Response validation failed")
|
131 |
-
|
132 |
-
# Format medical information
|
133 |
-
response = response.replace("**Possible Condition:**", "\n🩺 **Possible Condition:**")
|
134 |
-
response = response.replace("**Recommendation:**", "\n📋 **Recommendation:**")
|
135 |
-
|
136 |
-
st.markdown(response)
|
137 |
-
st.caption("ℹ️ AI-generated suggestion - Verify with healthcare professional")
|
138 |
-
|
139 |
-
st.session_state.history.append(("user", user_input))
|
140 |
st.session_state.history.append(("bot", response))
|
141 |
-
|
142 |
-
except Exception as e:
|
143 |
-
st.error(f"⚠️ System Error: {str(e)}")
|
144 |
-
st.session_state.history.append(("bot", "🚨 Error processing request. Please try again later."))
|
145 |
|
146 |
if __name__ == "__main__":
|
147 |
main()
|
|
|
1 |
from langchain_google_genai import GoogleGenerativeAI
|
2 |
import streamlit as st
|
|
|
|
|
3 |
|
4 |
+
# --- LLM Initialization ---
|
5 |
+
llm = GoogleGenerativeAI(
|
6 |
+
model="gemini-1.5-flash",
|
7 |
+
google_api_key=GEMINI_API_KEY
|
8 |
+
temperature= 0.7,
|
9 |
+
top_p= 0.85,
|
10 |
+
max_tokens= 1024,
|
11 |
+
stop_sequence = ["take your own life", "kill yourself", "hate you"]
|
12 |
+
)
|
13 |
+
|
14 |
+
# --- instruction ---
|
15 |
+
instructions = "Answer the user's question politely and provide accurate information."
|
16 |
+
|
17 |
+
# --- Context & Input Data ---
|
18 |
+
context =(
|
19 |
+
"You are DoctorX, an AI medical assistant. Follow these rules STRICTLY:"
|
20 |
+
"1. Never diagnose - suggest POSSIBLE conditions"
|
21 |
+
"2. Always recommend consulting a human doctor"
|
22 |
+
"3. Prioritize patient safety over all else"
|
23 |
+
"4. Maintain professional, empathetic tone"
|
24 |
+
"5. Disclose your AI nature clearly"
|
25 |
+
"6. For emergencies, direct to sidebar contacts"
|
26 |
+
)
|
27 |
+
|
28 |
+
input_data = (
|
29 |
+
"Hospital Address:John Smith Hospital, 123 Health St."
|
30 |
+
"Ambulance:911 (US) / 112 (EU)"
|
31 |
+
"24/7 Support:+1-800-123-4567"
|
32 |
+
)
|
|
|
33 |
|
34 |
# --- Streamlit App ---
|
35 |
def main():
|
36 |
st.set_page_config(
|
37 |
+
page_title="DoctorX",
|
38 |
page_icon="🩺",
|
39 |
+
layout="wide",
|
40 |
+
initial_sidebar_state="expanded"
|
41 |
)
|
42 |
|
43 |
+
# Initialize session state for chat history
|
44 |
if "history" not in st.session_state:
|
45 |
st.session_state.history = []
|
46 |
+
|
47 |
+
# Sidebar for emergency contacts
|
48 |
with st.sidebar:
|
49 |
+
st.header("🚨 Emergency Contacts")
|
50 |
+
for key, value in input_data.items():
|
51 |
st.subheader(key)
|
52 |
st.caption(value)
|
53 |
st.divider()
|
54 |
+
st.caption("⚠️ This is not a substitute for emergency medical care.")
|
55 |
|
56 |
+
# Main interface
|
57 |
+
st.title("🩺 DoctorX")
|
58 |
+
st.caption("AI-powered preliminary health guidance - Always consult a human doctor for final diagnosis.")
|
59 |
|
60 |
# Display chat history
|
61 |
for role, message in st.session_state.history:
|
62 |
with st.chat_message(role):
|
63 |
st.markdown(message)
|
64 |
|
65 |
+
# User input
|
66 |
+
user_query = st.text_input("Describe your symptoms or ask a question:", placeholder="e.g., I have a headache")
|
67 |
+
if user_query:
|
68 |
+
st.session_state.history.append(("user", user_query))
|
69 |
+
with st.chat_message("bot"):
|
70 |
+
with st.spinner("Analyzing your input..."):
|
71 |
+
response = llm.invoke(user_query)
|
72 |
+
st.markdown(response)
|
73 |
+
st.session_state.history.append(("bot", response))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
# Quick response buttons
|
76 |
common_queries = ["What are the symptoms of epilepsy?", "How to manage stress?"]
|
77 |
cols = st.columns(len(common_queries))
|
78 |
for col, query in zip(cols, common_queries):
|
79 |
if col.button(query):
|
80 |
+
st.session_state.history.append(("user", query))
|
81 |
+
response = llm.invoke(query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
st.session_state.history.append(("bot", response))
|
83 |
+
st.markdown(response)
|
|
|
|
|
|
|
84 |
|
85 |
if __name__ == "__main__":
|
86 |
main()
|