-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
360 lines (302 loc) · 12.2 KB
/
app.py
File metadata and controls
360 lines (302 loc) · 12.2 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
348
349
350
351
352
353
354
355
356
357
358
359
360
import streamlit as st
import os
from PIL import Image
import tempfile
from datetime import datetime
from pathlib import Path
# Try to import the chatbot
from chatbot import DrugDosageChatbot
USING_ENHANCED = True
print("Using enhanced chatbot with independent Gemini interface")
# Set page config
st.set_page_config(
page_title="Drug Dosage Explanation",
page_icon="💊",
layout="wide",
initial_sidebar_state="expanded"
)
# Add pages directory
PAGES_DIR = Path(__file__).parent / "pages"
PAGES_DIR.mkdir(exist_ok=True)
# Custom CSS for better styling
st.markdown("""
<style>
.main {
max-width: 1000px;
padding: 2rem;
}
.stButton>button {
background-color: #4CAF50;
color: white;
border-radius: 5px;
padding: 0.5rem 1rem;
font-weight: bold;
}
.stButton>button:hover {
background-color: #45a049;
}
.stTextInput>div>div>input {
border: 1px solid #4CAF50;
border-radius: 5px;
}
.stSelectbox>div>div>div {
border: 1px solid #4CAF50;
border-radius: 5px;
}
.stNumberInput>div>div>input {
border: 1px solid #4CAF50;
border-radius: 5px;
}
.stFileUploader>div>div>button {
border: 1px solid #4CAF50;
color: #4CAF50;
}
.stMarkdown h1 {
color: #4CAF50;
}
.stMarkdown h2 {
color: #2E7D32;
}
.info-box {
background-color: #E8F5E9;
border-left: 5px solid #4CAF50;
padding: 1rem;
margin: 1rem 0;
border-radius: 0 5px 5px 0;
}
</style>
""", unsafe_allow_html=True)
def initialize_session_state():
"""Initialize all session state variables needed for the app."""
# Initialize chat-related states
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
if 'current_prediction' not in st.session_state:
st.session_state.current_prediction = None
if 'page' not in st.session_state:
st.session_state.page = "home"
if 'chatbot' not in st.session_state:
# Initialize the chatbot with hardcoded API key
gemini_api_key = "AIzaSyCxhduD3UiOU1gFSqwMonmW6ItTBcSMEIw" # Temporary hardcoded key for testing
st.session_state.chatbot = DrugDosageChatbot(gemini_api_key=gemini_api_key)
def add_message(question, answer):
"""Add a message to the chat history."""
st.session_state.chat_history.append({
"question": question,
"answer": answer,
"timestamp": datetime.now().strftime("%H:%M:%S")
})
def clear_chat_history():
"""Clear the chat history."""
st.session_state.chat_history = []
def show_chat_button():
"""Display a button to navigate to the chat page."""
if st.button("💬 Chat with Prescription Assistant", use_container_width=True, type="primary"):
# Store the current page to return to
st.session_state.previous_page = "home"
# Switch to chat page
st.session_state.page = "chat"
st.rerun()
def initialize_chatbot():
"""Initialize the chatbot with hardcoded API key temporarily."""
gemini_api_key = "AIzaSyCxhduD3UiOU1gFSqwMonmW6ItTBcSMEIw" # Temporary hardcoded key for testing
chatbot = DrugDosageChatbot(gemini_api_key=gemini_api_key)
if USING_ENHANCED:
st.sidebar.success("✅ Using enhanced chatbot with independent Gemini interface")
else:
st.sidebar.info("ℹ️ Using original chatbot implementation")
return chatbot
def display_sidebar():
"""Display the sidebar with app information and navigation."""
st.sidebar.title("💊 Navigation")
# Navigation menu
page = st.sidebar.radio(
"Go to",
["🏠 Home", "💬 Chat with Assistant"],
index=0 if st.session_state.get('page') == "home" else 1
)
# Update page state based on selection
if page == "🏠 Home" and st.session_state.page != "home":
st.session_state.page = "home"
st.rerun()
elif page == "💬 Chat with Assistant" and st.session_state.page != "chat":
st.session_state.page = "chat"
st.rerun()
st.sidebar.markdown("---")
st.sidebar.title("About")
st.sidebar.markdown(
"""
**Drug Dosage Explanation** helps you understand your medication dosage
based on your prescription and health information.
### How it works:
1. Upload a prescription image or enter details manually
2. The system processes the information
3. Get dosage recommendations with explanations
4. Chat with the assistant for any questions
### Supported Medications:
- Metformin (Diabetes)
- Amlodipine (Hypertension)
- Salbutamol (Asthma)
- Paracetamol (Fever)
- Amoxicillin (Infection)
*Note: This is for informational purposes only. Always consult with a healthcare professional.*
"""
)
def process_prescription_image(chatbot, uploaded_file):
"""Process an uploaded prescription image."""
with st.spinner("Processing your prescription..."):
# Save the uploaded file to a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as tmp_file:
tmp_file.write(uploaded_file.getvalue())
tmp_path = tmp_file.name
try:
# Process the image
prediction = chatbot.process_prescription_image(tmp_path)
# Display the prediction
display_prediction(prediction, chatbot)
except Exception as e:
st.error(f"Error processing the image: {str(e)}")
st.info("Please try again or enter the details manually.")
finally:
# Clean up the temporary file
try:
os.unlink(tmp_path)
except:
pass
def display_prediction(prediction, chatbot):
"""Display the prediction results in a user-friendly format."""
st.subheader("📋 Dosage Recommendation")
# Create columns for better layout
col1, col2 = st.columns(2)
with col1:
st.metric("Condition", prediction.disease.title())
st.metric("Medication", prediction.medicine.title())
with col2:
st.metric("Recommended Dosage", f"{prediction.predicted_dosage:.0f}mg")
st.metric("Frequency", f"{prediction.frequency_per_day} time(s) per day")
# Display model confidence and prediction details
if hasattr(prediction, 'confidence'):
confidence = prediction.confidence
st.progress(confidence, text=f"Model Confidence: {confidence*100:.1f}%")
# Display explanation in an expandable section
with st.expander("ℹ️ How was this dosage determined?"):
st.markdown("""
The recommended dosage is based on:
- Your specific condition and medication
- Standard dosing guidelines
- Your provided health information (if any)
**Explanation:**
{}
""".format(prediction.explanation))
# Display safety notes
st.markdown("### ⚠️ Important Safety Information")
st.warning(prediction.safety_notes)
# Store the current prediction in session state for chat
st.session_state.current_prediction = prediction
# Show chat button
show_chat_button()
# Disclaimer
st.markdown("---")
st.markdown("""
*This information is provided for educational purposes only and is not
intended as medical advice. Always consult with a healthcare professional
before starting or changing any medication regimen.*
""")
def show_home_page():
"""Show the home page with prescription upload and manual entry options."""
st.title("💊 Drug Dosage Explanation Chatbot")
st.markdown("Upload a prescription image or enter details below to get dosage information.")
# Initialize the chatbot
chatbot = st.session_state.chatbot
# Create tabs for different input methods
tab1, tab2 = st.tabs(["📷 Upload Prescription", "✍️ Enter Details Manually"])
with tab1:
st.subheader("Upload Your Prescription")
uploaded_file = st.file_uploader(
"Choose an image of your prescription",
type=["jpg", "jpeg", "png"],
help="Upload a clear photo of your prescription"
)
if uploaded_file is not None:
# Display the uploaded image
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Prescription", use_container_width=True)
# Process the image when the user clicks the button
if st.button("Analyze Prescription", key="analyze_btn"):
process_prescription_image(chatbot, uploaded_file)
with tab2:
st.subheader("Enter Prescription Details")
# Create a form for manual input
with st.form("prescription_form"):
col1, col2 = st.columns(2)
with col1:
# Disease selection
disease = st.selectbox(
"Medical Condition",
["", "Diabetes", "Hypertension", "Asthma", "Fever", "Infection"],
help="Select your medical condition"
)
# Age input
age = st.number_input(
"Age (years)",
min_value=1,
max_value=120,
value=None,
step=1,
help="Your age in years"
)
with col2:
# Medicine input (auto-filled based on disease)
medicine_map = {
"Diabetes": "Metformin",
"Hypertension": "Amlodipine",
"Asthma": "Salbutamol",
"Fever": "Paracetamol",
"Infection": "Amoxicillin"
}
medicine = st.text_input(
"Medication",
value=medicine_map.get(disease, ""),
)
weight = st.number_input("Weight (kg)", min_value=10, max_value=300, value=70)
frequency = st.number_input("Frequency per day", min_value=1, max_value=4, value=1)
submitted = st.form_submit_button("Get Dosage Recommendation")
if submitted:
if not disease or not medicine:
st.error("Please select both disease and medicine.")
else:
try:
# Create input data dictionary
input_data = {
'disease': disease.lower(),
'medicine': medicine.lower(),
'age': age,
'weight': weight,
'frequency_per_day': frequency
}
# Make prediction
prediction = st.session_state.chatbot.predict_dosage(input_data)
st.session_state.current_prediction = prediction
# Display the prediction
display_prediction(prediction, st.session_state.chatbot)
# Show chat button after successful prediction
show_chat_button()
except Exception as e:
st.error(f"Error: {str(e)}")
st.rerun()
def main():
"""Main function to run the Streamlit app."""
# Initialize session state
initialize_session_state()
# Display the sidebar
display_sidebar()
# Show the appropriate page based on state
if st.session_state.page == "chat":
# Import and run the chat page
from pages.chat import main as chat_main
chat_main()
else:
# Show home page
show_home_page()
if __name__ == "__main__":
main()