-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
321 lines (265 loc) · 11.7 KB
/
app.py
File metadata and controls
321 lines (265 loc) · 11.7 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
import os
import pandas as pd
import joblib
import numpy as np
from flask import Flask, render_template, request, jsonify
from sklearn.preprocessing import StandardScaler
app = Flask(__name__)
# Configuration
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# Fixed paths - using absolute paths
DATA_PATH = '/Users/sachidhoka/Desktop/balanced_drug_food_interactions.csv'
MODEL_PATH = '/Users/sachidhoka/Desktop/College/ASEP_2(Drug-Food)/dfi project/models/best_drug_food_interaction_model.pkl'
# Load dataset and model
print("Loading dataset...")
try:
df = pd.read_csv(DATA_PATH)
print(f"✅ Dataset loaded: {len(df)} records")
print(f"Columns: {df.columns.tolist()}")
except Exception as e:
print(f"❌ Error loading dataset: {e}")
df = pd.DataFrame()
print("Loading model...")
try:
model_package = joblib.load(MODEL_PATH)
model = model_package['model']
scaler = model_package.get('scaler', StandardScaler())
feature_info = model_package.get('feature_info', {})
drug_categories = model_package.get('drug_categories', {})
food_categories = model_package.get('food_categories', {})
high_risk_interactions = model_package.get('high_risk_interactions', {})
print("✅ Model loaded successfully")
except Exception as e:
print(f"❌ Error loading model: {e}")
model = None
scaler = StandardScaler()
feature_info = {}
drug_categories = {}
food_categories = {}
high_risk_interactions = {}
def categorize_entity(entity, categories):
"""Categorize drug or food based on predefined categories"""
entity_lower = str(entity).lower()
best_match = 'other'
max_matches = 0
for category, items in categories.items():
matches = sum(1 for item in items if item in entity_lower)
if matches > max_matches:
max_matches = matches
best_match = category
return best_match
def get_interaction_details(drug_cat, food_cat):
"""Get interaction mechanism and risk level"""
for (d_cat, f_cat), details in high_risk_interactions.items():
if d_cat == drug_cat and f_cat == food_cat:
return details['mechanism'], details['risk']
return 'unknown', 'LOW'
def get_clinical_explanation(mechanism, risk_level):
"""Provide patient-friendly explanation"""
explanations = {
'cyp3a4_inhibition': "This food can block liver enzymes that break down your medication, potentially causing it to build up to dangerous levels in your body.",
'vitamin_k_competition': "This food contains vitamin K which can reduce the effectiveness of blood-thinning medications.",
'calcium_chelation': "Calcium in this food can bind to your medication and prevent your body from absorbing it properly.",
'absorption_interference': "This food may slow down or reduce how well your body absorbs the medication.",
'cns_depression': "Combining these can increase drowsiness and impair coordination and judgment.",
'hypoglycemia_risk': "This combination may cause dangerously low blood sugar levels.",
'arrhythmia_risk': "This combination may affect your heart rhythm.",
'gi_bleeding_risk': "This combination may increase the risk of stomach bleeding.",
'bp_elevation': "This combination may reduce the effectiveness of blood pressure medication.",
'fluid_retention': "This combination may cause increased fluid retention and swelling.",
'unknown': "The specific interaction mechanism is not fully understood."
}
recommendations = {
'HIGH': "🚨 AVOID this combination. Consult your healthcare provider immediately before taking these together.",
'MODERATE': "⚠️ Use with CAUTION. Consider spacing them 2-4 hours apart and monitor for side effects.",
'LOW': "✅ Generally safe, but follow standard precautions and monitor for any unusual symptoms."
}
return {
'explanation': explanations.get(mechanism, explanations['unknown']),
'recommendation': recommendations.get(risk_level, recommendations['LOW'])
}
def predict_interaction(drug, food):
"""Predict drug-food interaction"""
if model is None or df.empty:
return {'error': 'Model or data not available'}
# Check for known interaction first
known_interaction = df[
(df['drug'].str.lower() == drug.lower()) &
(df['food'].str.lower() == food.lower())
]
if not known_interaction.empty:
interaction = known_interaction.iloc[0]
mechanism = interaction.get('mechanism', 'unknown')
risk_level = interaction.get('risk_level', 'LOW')
clinical_info = get_clinical_explanation(mechanism, risk_level)
return {
'drug': drug.title(),
'food': food.title(),
'interaction_predicted': bool(interaction.get('interaction', 0)),
'probability': 1.0 if interaction.get('interaction', 0) else 0.0,
'drug_category': interaction.get('drug_category', 'other'),
'food_category': interaction.get('food_category', 'other'),
'mechanism': mechanism,
'risk_level': risk_level,
'explanation': clinical_info['explanation'],
'recommendation': clinical_info['recommendation'],
'source': 'known_database'
}
# If not in database, use model prediction
try:
new_df = pd.DataFrame({
'drug': [drug.lower().strip()],
'food': [food.lower().strip()],
'interaction': [0]
})
# Categorize
new_df['drug_category'] = new_df['drug'].apply(lambda x: categorize_entity(x, drug_categories))
new_df['food_category'] = new_df['food'].apply(lambda x: categorize_entity(x, food_categories))
# Get interaction details
interaction_details = new_df.apply(
lambda x: get_interaction_details(x['drug_category'], x['food_category']),
axis=1
)
new_df['mechanism'] = [details[0] for details in interaction_details]
new_df['risk_level'] = [details[1] for details in interaction_details]
# Create features (simplified for compatibility)
drug_dummies = pd.get_dummies(new_df['drug_category'], prefix='drug')
food_dummies = pd.get_dummies(new_df['food_category'], prefix='food')
mechanism_dummies = pd.get_dummies(new_df['mechanism'], prefix='mechanism')
risk_dummies = pd.get_dummies(new_df['risk_level'], prefix='risk')
# Combine features
X_new = pd.concat([drug_dummies, food_dummies, mechanism_dummies, risk_dummies], axis=1)
# Ensure all required features are present
if feature_info.get('feature_names'):
missing_cols = set(feature_info['feature_names']) - set(X_new.columns)
for col in missing_cols:
X_new[col] = 0
X_new = X_new[feature_info['feature_names']]
# Scale features
X_new_scaled = scaler.transform(X_new)
# Make prediction
prediction = model.predict(X_new_scaled)[0]
probability = model.predict_proba(X_new_scaled)[0, 1]
mechanism = new_df['mechanism'].iloc[0]
risk_level = new_df['risk_level'].iloc[0]
clinical_info = get_clinical_explanation(mechanism, risk_level)
return {
'drug': drug.title(),
'food': food.title(),
'interaction_predicted': bool(prediction),
'probability': float(probability),
'drug_category': new_df['drug_category'].iloc[0],
'food_category': new_df['food_category'].iloc[0],
'mechanism': mechanism,
'risk_level': risk_level,
'explanation': clinical_info['explanation'],
'recommendation': clinical_info['recommendation'],
'source': 'model_prediction'
}
except Exception as e:
print(f"Prediction error: {e}")
return {'error': f'Prediction failed: {str(e)}'}
def find_similar_interactions(drug, food, limit=3):
"""Find similar interactions in database"""
if df.empty:
return []
# Get drug and food categories
drug_cat = categorize_entity(drug, drug_categories)
food_cat = categorize_entity(food, food_categories)
# Find similar interactions
similar = df[
((df['drug_category'] == drug_cat) | (df['food_category'] == food_cat)) &
(df['interaction'] == 1)
].head(limit)
return similar[['drug', 'food', 'risk_level']].to_dict('records')
@app.route('/')
def index():
"""Main page"""
return render_template('index.html')
@app.route('/api/status')
def api_status():
"""Check API status"""
return jsonify({
'status': 'online',
'dataset_loaded': not df.empty,
'model_loaded': model is not None,
'total_records': len(df) if not df.empty else 0
})
@app.route('/api/drugs')
def get_drugs():
"""Get list of drugs with optional search"""
if df.empty:
return jsonify({'drugs': []})
search = request.args.get('q', '').lower()
limit = int(request.args.get('limit', 20))
drugs = df['drug'].unique()
if search:
drugs = [d for d in drugs if search in str(d).lower()]
drugs = sorted([str(d) for d in drugs if pd.notna(d)])[:limit]
return jsonify({'drugs': drugs})
@app.route('/api/foods')
def get_foods():
"""Get list of foods with optional search"""
if df.empty:
return jsonify({'foods': []})
search = request.args.get('q', '').lower()
limit = int(request.args.get('limit', 20))
foods = df['food'].unique()
if search:
foods = [f for f in foods if search in str(f).lower()]
foods = sorted([str(f) for f in foods if pd.notna(f)])[:limit]
return jsonify({'foods': foods})
@app.route('/api/predict', methods=['POST'])
def api_predict():
"""Predict drug-food interaction"""
try:
data = request.get_json()
drug = data.get('drug', '').strip()
food = data.get('food', '').strip()
if not drug or not food:
return jsonify({
'success': False,
'error': 'Both drug and food must be provided'
}), 400
# Get prediction
result = predict_interaction(drug, food)
if 'error' in result:
return jsonify({
'success': False,
'error': result['error']
}), 500
# Get similar interactions
similar = find_similar_interactions(drug, food)
result['similar_interactions'] = similar
return jsonify({
'success': True,
'data': result
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
}), 500
@app.route('/api/stats')
def get_stats():
"""Get dataset statistics"""
if df.empty:
return jsonify({'error': 'Dataset not loaded'})
stats = {
'total_interactions': len(df),
'unique_drugs': df['drug'].nunique() if 'drug' in df.columns else 0,
'unique_foods': df['food'].nunique() if 'food' in df.columns else 0,
'risk_distribution': {}
}
if 'risk_level' in df.columns:
stats['risk_distribution'] = df['risk_level'].value_counts().to_dict()
return jsonify(stats)
if __name__ == '__main__':
print("\n" + "="*60)
print("🚀 Drug-Food Interaction Predictor")
print("="*60)
print(f"📊 Dataset: {len(df)} records loaded" if not df.empty else "❌ Dataset not loaded")
print(f"🤖 Model: {'Loaded' if model else 'Not loaded'}")
print(f"🌐 Starting server on http://localhost:5001")
print("="*60 + "\n")
app.run(debug=True, port=5001, host='0.0.0.0')