-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
347 lines (287 loc) · 7.44 KB
/
Copy pathapp.py
File metadata and controls
347 lines (287 loc) · 7.44 KB
1
2
3
4
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import os
import streamlit as st
from dotenv import load_dotenv
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.prompts import ChatPromptTemplate
# ----------------------------
# LOAD ENV
# ----------------------------
load_dotenv()
api_key = os.getenv("GOOGLE_API_KEY")
# Streamlit Cloud Secrets fallback
if not api_key:
api_key = st.secrets["GOOGLE_API_KEY"]
# ----------------------------
# PAGE CONFIG
# ----------------------------
st.set_page_config(
page_title="MediBot AI Assistant",
page_icon="🏥",
layout="wide",
initial_sidebar_state="collapsed"
)
# ----------------------------
# CUSTOM CSS
# ----------------------------
st.markdown(
"""
<style>
#MainMenu {
visibility: hidden;
}
footer {
visibility: hidden;
}
header {
visibility: hidden;
}
.stApp {
background:
radial-gradient(circle at top left, rgba(56,189,248,0.12), transparent 25%),
radial-gradient(circle at bottom right, rgba(99,102,241,0.10), transparent 25%),
linear-gradient(135deg, #020617, #0f172a, #111827);
color: white;
}
.hero {
padding: 3rem;
border-radius: 30px;
background: rgba(255,255,255,0.05);
backdrop-filter: blur(18px);
border: 1px solid rgba(255,255,255,0.08);
margin-bottom: 2rem;
box-shadow: 0 10px 40px rgba(0,0,0,0.35);
}
.hero-title {
font-size: 4rem;
font-weight: 700;
color: white;
margin-bottom: 0.5rem;
}
.gradient-text {
background: linear-gradient(90deg,#38bdf8,#60a5fa,#818cf8);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.hero-subtitle {
color: #cbd5e1;
font-size: 1.1rem;
}
.feature-card {
background: rgba(255,255,255,0.05);
border: 1px solid rgba(255,255,255,0.08);
padding: 1rem;
border-radius: 20px;
text-align: center;
transition: 0.3s;
}
.feature-card:hover {
transform: translateY(-4px);
border: 1px solid #38bdf8;
}
.stChatMessage {
background: rgba(255,255,255,0.05);
border-radius: 18px;
padding: 14px;
border: 1px solid rgba(255,255,255,0.06);
}
.stChatMessage p,
.stChatMessage div,
.stChatMessage span,
.stChatMessage strong,
.stChatMessage li {
color: white !important;
}
.stChatInputContainer {
background: rgba(255,255,255,0.05) !important;
border-radius: 20px !important;
border: 1px solid rgba(255,255,255,0.08) !important;
}
.stButton > button {
background-color: #1e293b !important;
color: white !important;
border: 1px solid rgba(255,255,255,0.1) !important;
border-radius: 12px !important;
}
.stButton > button:hover {
border: 1px solid #38bdf8 !important;
color: #38bdf8 !important;
}
.block-container {
padding-top: 2rem;
max-width: 1200px;
}
.badge {
display: inline-block;
padding: 8px 16px;
border-radius: 999px;
background: rgba(56,189,248,0.15);
color: #38bdf8;
font-size: 0.9rem;
margin-bottom: 1rem;
}
.footer {
text-align: center;
color: #94a3b8;
padding-top: 2rem;
}
</style>
""",
unsafe_allow_html=True
)
# ----------------------------
# API CHECK
# ----------------------------
if not api_key:
st.error(
"GOOGLE_API_KEY not found. Add it in .env or Streamlit Secrets."
)
st.stop()
# ----------------------------
# GEMINI MODEL
# ----------------------------
llm = ChatGoogleGenerativeAI(
model="gemini-2.5-flash",
google_api_key=api_key,
temperature=0.3
)
# ----------------------------
# PROMPT
# ----------------------------
prompt = ChatPromptTemplate.from_template(
"""
You are MediBot, a professional AI hospital assistant.
Responsibilities:
- Answer hospital-related questions
- Help patients understand symptoms
- Suggest suitable medical departments
- Provide appointment guidance
- Give healthcare assistance
- Recommend emergency care when needed
Rules:
- Never provide unsafe medical advice
- Recommend professional medical consultation for serious issues
- Keep answers concise, accurate, and supportive
- Respond professionally
User Question:
{question}
Answer:
"""
)
# ----------------------------
# CHAIN
# ----------------------------
chain = prompt | llm
# ----------------------------
# HERO SECTION
# ----------------------------
st.markdown(
"""
<div class="hero">
<div class="badge">AI-Powered Healthcare Assistant</div>
<div class="hero-title">
MediBot <span class="gradient-text">AI</span>
</div>
<div class="hero-subtitle">
Next-generation AI healthcare assistant for hospitals, clinics, patient support, healthcare guidance, appointment assistance, and medical information.
</div>
</div>
""",
unsafe_allow_html=True
)
# ----------------------------
# PROFESSIONAL INFO SECTION
# ----------------------------
st.markdown(
"""
<div style="
background: rgba(255,255,255,0.04);
border: 1px solid rgba(255,255,255,0.08);
padding: 1.2rem;
border-radius: 18px;
margin-bottom: 1.5rem;
">
<h3 style="margin-top:0;color:white;">AI Healthcare Assistant</h3>
<p style="color:#cbd5e1;margin-bottom:0;">
Ask healthcare, hospital, symptom, appointment, or emergency-related questions and receive AI-powered assistance instantly.
</p>
</div>
""",
unsafe_allow_html=True
)
# ----------------------------
# SESSION STATE
# ----------------------------
if "messages" not in st.session_state:
st.session_state.messages = []
# ----------------------------
# DISPLAY CHAT HISTORY
# ----------------------------
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# ----------------------------
# CHAT INPUT
# ----------------------------
user_input = st.chat_input(
"Ask any hospital or healthcare-related question..."
)
# quick_prompt removed to avoid undefined variable issues
# ----------------------------
# AI CHAT
# ----------------------------
if user_input:
st.session_state.messages.append(
{
"role": "user",
"content": user_input
}
)
with st.chat_message("user"):
st.markdown(user_input)
with st.chat_message("assistant"):
with st.spinner("Analyzing your query..."):
try:
response = chain.invoke(
{
"question": user_input
}
)
ai_response = response.content
st.markdown(ai_response)
st.session_state.messages.append(
{
"role": "assistant",
"content": ai_response
}
)
except Exception as e:
st.error(f"Error: {str(e)}")
# ----------------------------
# SIDEBAR INFO
# ----------------------------
with st.sidebar:
st.header("🏥 MediBot")
st.markdown("""
### Features
- AI Healthcare Assistant
- Symptom Guidance
- Medical Department Suggestions
- Appointment Help
- Emergency Support
- Patient Assistance
""")
st.markdown("---")
st.info(
"This AI assistant provides general healthcare guidance only and does not replace professional medical consultation."
)
# ----------------------------
# FOOTER
# ----------------------------
st.markdown(
"""
<div class="footer">
Powered by Streamlit • LangChain • Gemini 2.5 Flash
</div>
""",
unsafe_allow_html=True
)