Fredaaaaaa commited on
Commit
7515fab
Β·
verified Β·
1 Parent(s): 0eb7515

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -122
app.py CHANGED
@@ -1,70 +1,65 @@
1
  import gradio as gr
2
  import pandas as pd
3
  import os
 
4
 
5
  # Load your labeled dataset with exact column names
6
  def load_drug_interaction_dataset():
7
  """Load your labeled drug interaction dataset with exact column names"""
8
  try:
9
- # Your exact dataset filename
10
  dataset_path = 'merged_cleaned_dataset.csv'
11
-
12
  print(f"Looking for dataset: {dataset_path}")
13
  print("Files in directory:", os.listdir('.'))
14
 
15
  if not os.path.exists(dataset_path):
16
  print(f"Dataset file {dataset_path} not found!")
17
- return create_fallback_database()
18
 
19
- # Load the dataset with your exact column names
20
  print(f"Loading dataset from: {dataset_path}")
21
  df = pd.read_csv(dataset_path)
22
  print(f"Dataset columns: {df.columns.tolist()}")
23
  print(f"Dataset shape: {df.shape}")
24
- print(f"First few rows:\n{df.head()}")
25
 
26
- # Create interaction dictionary using your exact column names
27
  interaction_db = {}
28
  count = 0
29
 
30
- for _, row in df.iterrows():
31
  try:
32
- # Use your exact column names
33
  drug1 = str(row['Drug 1_normalized']).lower().strip()
34
  drug2 = str(row['Drug 2_normalized']).lower().strip()
35
  severity = str(row['severity']).strip()
36
 
37
- # Skip empty entries or invalid data
38
  if (not all([drug1, drug2, severity]) or
39
  drug1 == 'nan' or drug2 == 'nan' or
40
  severity == 'nan' or severity.lower() == 'none'):
41
  continue
42
 
43
- # Clean up severity labels
44
  severity = severity.capitalize()
45
  if severity == 'No interaction':
46
  severity = 'No Interaction'
47
 
48
- # Add both orders to the dictionary
49
  interaction_db[(drug1, drug2)] = severity
50
- interaction_db[(drug2, drug1)] = severity # Add reverse order
51
  count += 1
 
 
52
 
53
  except Exception as e:
54
- print(f"Error processing row {_}: {e}")
55
  continue
56
 
57
  print(f"βœ… Successfully loaded {count} drug interactions from dataset")
58
  print(f"Sample interactions: {list(interaction_db.items())[:5]}")
59
- return interaction_db
60
 
61
  except Exception as e:
62
  print(f"Error loading dataset: {e}")
63
- return create_fallback_database()
64
 
65
  def create_fallback_database():
66
- """Fallback database if dataset loading fails - FIXED STRUCTURE"""
67
- print("Using fallback database")
68
  return {
69
  ('warfarin', 'aspirin'): 'Severe',
70
  ('aspirin', 'warfarin'): 'Severe',
@@ -72,99 +67,52 @@ def create_fallback_database():
72
  ('ibuprofen', 'warfarin'): 'Severe',
73
  ('simvastatin', 'clarithromycin'): 'Severe',
74
  ('clarithromycin', 'simvastatin'): 'Severe',
75
- ('clopidogrel', 'omeprazole'): 'Severe',
76
- ('omeprazole', 'clopidogrel'): 'Severe',
77
- ('methotrexate', 'naproxen'): 'Severe',
78
- ('naproxen', 'methotrexate'): 'Severe',
79
- ('lithium', 'ibuprofen'): 'Severe',
80
- ('ibuprofen', 'lithium'): 'Severe',
81
- ('ssri', 'maoi'): 'Severe',
82
- ('maoi', 'ssri'): 'Severe',
83
- ('simvastatin', 'verapamil'): 'Severe',
84
- ('verapamil', 'simvastatin'): 'Severe',
85
- ('warfarin', 'fluconazole'): 'Severe',
86
- ('fluconazole', 'warfarin'): 'Severe',
87
- ('digoxin', 'verapamil'): 'Severe',
88
- ('verapamil', 'digoxin'): 'Severe',
89
-
90
- # Moderate interactions
91
  ('digoxin', 'quinine'): 'Moderate',
92
  ('quinine', 'digoxin'): 'Moderate',
93
- ('lisinopril', 'ibuprofen'): 'Moderate',
94
- ('ibuprofen', 'lisinopril'): 'Moderate',
95
- ('metformin', 'alcohol'): 'Moderate',
96
- ('alcohol', 'metformin'): 'Moderate',
97
- ('levothyroxine', 'calcium'): 'Moderate',
98
- ('calcium', 'levothyroxine'): 'Moderate',
99
- ('atorvastatin', 'orange juice'): 'Moderate',
100
- ('orange juice', 'atorvastatin'): 'Moderate',
101
- ('phenytoin', 'warfarin'): 'Moderate',
102
- ('warfarin', 'phenytoin'): 'Moderate',
103
- ('theophylline', 'ciprofloxacin'): 'Moderate',
104
- ('ciprofloxacin', 'theophylline'): 'Moderate',
105
- ('warfarin', 'acetaminophen'): 'Moderate',
106
- ('acetaminophen', 'warfarin'): 'Moderate',
107
- ('metoprolol', 'verapamil'): 'Moderate',
108
- ('verapamil', 'metoprolol'): 'Moderate',
109
- ('spironolactone', 'digoxin'): 'Moderate',
110
- ('digoxin', 'spironolactone'): 'Moderate',
111
-
112
- # Mild interactions
113
  ('metformin', 'ibuprofen'): 'Mild',
114
  ('ibuprofen', 'metformin'): 'Mild',
115
- ('omeprazole', 'calcium'): 'Mild',
116
- ('calcium', 'omeprazole'): 'Mild',
117
- ('vitamin d', 'calcium'): 'Mild',
118
- ('calcium', 'vitamin d'): 'Mild',
119
- ('aspirin', 'vitamin c'): 'Mild',
120
- ('vitamin c', 'aspirin'): 'Mild',
121
- ('atorvastatin', 'vitamin d'): 'Mild',
122
- ('vitamin d', 'atorvastatin'): 'Mild',
123
- ('metformin', 'vitamin b12'): 'Mild',
124
- ('vitamin b12', 'metformin'): 'Mild',
125
- ('omeprazole', 'vitamin b12'): 'Mild',
126
- ('vitamin b12', 'omeprazole'): 'Mild',
127
- ('aspirin', 'ginger'): 'Mild',
128
- ('ginger', 'aspirin'): 'Mild',
129
- ('warfarin', 'green tea'): 'Mild',
130
- ('green tea', 'warfarin'): 'Mild',
131
- ('levothyroxine', 'iron'): 'Mild',
132
- ('iron', 'levothyroxine'): 'Mild',
133
-
134
- # No interactions
135
  ('vitamin c', 'vitamin d'): 'No Interaction',
136
  ('vitamin d', 'vitamin c'): 'No Interaction',
137
- ('calcium', 'vitamin d'): 'No Interaction',
138
- ('vitamin d', 'calcium'): 'No Interaction',
139
- ('omeprazole', 'vitamin d'): 'No Interaction',
140
- ('vitamin d', 'omeprazole'): 'No Interaction',
141
- ('metformin', 'vitamin d'): 'No Interaction',
142
- ('vitamin d', 'metformin'): 'No Interaction',
143
- ('aspirin', 'vitamin e'): 'No Interaction',
144
- ('vitamin e', 'aspirin'): 'No Interaction',
145
- ('atorvastatin', 'coenzyme q10'): 'No Interaction',
146
- ('coenzyme q10', 'atorvastatin'): 'No Interaction',
147
- ('levothyroxine', 'vitamin d'): 'No Interaction',
148
- ('vitamin d', 'levothyroxine'): 'No Interaction',
149
- ('metoprolol', 'magnesium'): 'No Interaction',
150
- ('magnesium', 'metoprolol'): 'No Interaction',
151
- ('lisinopril', 'potassium'): 'No Interaction',
152
- ('potassium', 'lisinopril'): 'No Interaction',
153
- ('simvastatin', 'vitamin e'): 'No Interaction',
154
- ('vitamin e', 'simvastatin'): 'No Interaction',
155
  }
156
 
157
- # Load your dataset
158
- print("Loading drug interaction dataset...")
159
- interaction_db = load_drug_interaction_dataset()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
161
- def predict_interaction(drug_names):
162
- """Predict interaction between two drugs using your labeled dataset"""
163
  try:
164
  if not drug_names or ',' not in drug_names:
165
  return "❌ Please enter two drug names separated by a comma (e.g., 'Warfarin, Aspirin')"
166
 
167
- # Split the input
168
  drugs = [drug.strip() for drug in drug_names.split(',')]
169
  if len(drugs) != 2:
170
  return "❌ Please enter exactly two drug names separated by a comma"
@@ -172,11 +120,9 @@ def predict_interaction(drug_names):
172
  drug1, drug2 = drugs[0].lower(), drugs[1].lower()
173
  print(f"Looking up: '{drug1}' + '{drug2}'")
174
 
175
- # Check if this interaction exists in your dataset
176
- prediction = interaction_db.get((drug1, drug2))
177
-
178
  if prediction:
179
- # Add appropriate emoji and formatting based on severity
180
  if prediction.lower() == 'severe':
181
  return f"🚨 **SEVERE INTERACTION**: {prediction}\n⚠️ This combination may be life-threatening. Consult healthcare provider immediately."
182
  elif prediction.lower() == 'moderate':
@@ -187,30 +133,44 @@ def predict_interaction(drug_names):
187
  return f"βœ… **NO INTERACTION**: {prediction}\n🟒 These drugs appear to be safe to use together."
188
  else:
189
  return f"πŸ“Š **INTERACTION LEVEL**: {prediction}"
190
- else:
191
- # Log all available keys for debugging
192
- print(f"Keys in interaction_db: {list(interaction_db.keys())[:10]}... (total {len(interaction_db)})")
193
-
194
- # Check if either drug exists in the database
195
- drug1_exists = any(d1 == drug1 or d2 == drug1 for d1, d2 in interaction_db.keys())
196
- drug2_exists = any(d1 == drug2 or d2 == drug2 for d1, d2 in interaction_db.keys())
197
-
198
- if not drug1_exists and not drug2_exists:
199
- return f"❓ **UNKNOWN DRUGS**: Neither '{drugs[0]}' nor '{drugs[1]}' found in database.\nπŸ’‘ Try checking spelling or use generic names."
200
- elif not drug1_exists:
201
- return f"❓ **UNKNOWN DRUG**: '{drugs[0]}' not found in database.\nπŸ’‘ Try checking spelling or use generic name."
202
- elif not drug2_exists:
203
- return f"❓ **UNKNOWN DRUG**: '{drugs[1]}' not found in database.\nπŸ’‘ Try checking spelling or use generic name."
204
  else:
205
- return f"❓ **NO DATA AVAILABLE**: No interaction data found for '{drugs[0]}' and '{drugs[1]}'.\nπŸ’‘ Consult healthcare provider for guidance."
 
 
 
 
 
 
 
 
 
 
 
 
206
 
207
  except Exception as e:
208
  return f"❌ Error: {str(e)}"
209
 
 
 
 
 
210
  # Create interface
211
  with gr.Blocks(title="Drug Interaction Predictor", theme=gr.themes.Soft()) as demo:
212
  gr.Markdown("# πŸ’Š Drug Interaction Predictor")
213
- gr.Markdown("**Predict potential drug interactions using clinical data**")
214
 
215
  with gr.Row():
216
  with gr.Column():
@@ -231,14 +191,16 @@ with gr.Blocks(title="Drug Interaction Predictor", theme=gr.themes.Soft()) as de
231
  )
232
 
233
  # Show dataset info and debugging
234
- gr.Markdown(f"*πŸ“Š Dataset loaded with {len(interaction_db)} drug pair interactions*")
 
235
 
236
  # Add a debug section to show what's actually in the database
237
- sample_pairs = list(interaction_db.items())[:10]
238
- debug_info = "Sample database entries:\n" + "\n".join([f"β€’ {k}: {v}" for k, v in sample_pairs])
 
239
  gr.Markdown(f"```\n{debug_info}\n```")
240
 
241
- # Examples from your dataset
242
  gr.Examples(
243
  examples=[
244
  "Warfarin, Aspirin",
@@ -251,7 +213,7 @@ with gr.Blocks(title="Drug Interaction Predictor", theme=gr.themes.Soft()) as de
251
  label="πŸ§ͺ Try these examples:"
252
  )
253
 
254
- predict_btn.click(predict_interaction, drug_input, output)
255
 
256
  # Add disclaimer
257
  gr.Markdown("""
 
1
  import gradio as gr
2
  import pandas as pd
3
  import os
4
+ import pubchempy as pcp
5
 
6
  # Load your labeled dataset with exact column names
7
  def load_drug_interaction_dataset():
8
  """Load your labeled drug interaction dataset with exact column names"""
9
  try:
 
10
  dataset_path = 'merged_cleaned_dataset.csv'
 
11
  print(f"Looking for dataset: {dataset_path}")
12
  print("Files in directory:", os.listdir('.'))
13
 
14
  if not os.path.exists(dataset_path):
15
  print(f"Dataset file {dataset_path} not found!")
16
+ return {}, create_fallback_database() # Return empty dict and fallback if file missing
17
 
 
18
  print(f"Loading dataset from: {dataset_path}")
19
  df = pd.read_csv(dataset_path)
20
  print(f"Dataset columns: {df.columns.tolist()}")
21
  print(f"Dataset shape: {df.shape}")
22
+ print(f"First few rows:\n{df.head().to_string()}")
23
 
 
24
  interaction_db = {}
25
  count = 0
26
 
27
+ for index, row in df.iterrows():
28
  try:
 
29
  drug1 = str(row['Drug 1_normalized']).lower().strip()
30
  drug2 = str(row['Drug 2_normalized']).lower().strip()
31
  severity = str(row['severity']).strip()
32
 
 
33
  if (not all([drug1, drug2, severity]) or
34
  drug1 == 'nan' or drug2 == 'nan' or
35
  severity == 'nan' or severity.lower() == 'none'):
36
  continue
37
 
 
38
  severity = severity.capitalize()
39
  if severity == 'No interaction':
40
  severity = 'No Interaction'
41
 
 
42
  interaction_db[(drug1, drug2)] = severity
43
+ interaction_db[(drug2, drug1)] = severity
44
  count += 1
45
+ if drug1 == 'warfarin' and drug2 == 'aspirin':
46
+ print(f"Found Warfarin, Aspirin at row {index} with severity: {severity}")
47
 
48
  except Exception as e:
49
+ print(f"Error processing row {index}: {e}")
50
  continue
51
 
52
  print(f"βœ… Successfully loaded {count} drug interactions from dataset")
53
  print(f"Sample interactions: {list(interaction_db.items())[:5]}")
54
+ return interaction_db, create_fallback_database()
55
 
56
  except Exception as e:
57
  print(f"Error loading dataset: {e}")
58
+ return {}, create_fallback_database()
59
 
60
  def create_fallback_database():
61
+ """Fallback database for missing drug pairs"""
62
+ print("Initializing fallback database")
63
  return {
64
  ('warfarin', 'aspirin'): 'Severe',
65
  ('aspirin', 'warfarin'): 'Severe',
 
67
  ('ibuprofen', 'warfarin'): 'Severe',
68
  ('simvastatin', 'clarithromycin'): 'Severe',
69
  ('clarithromycin', 'simvastatin'): 'Severe',
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  ('digoxin', 'quinine'): 'Moderate',
71
  ('quinine', 'digoxin'): 'Moderate',
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  ('metformin', 'ibuprofen'): 'Mild',
73
  ('ibuprofen', 'metformin'): 'Mild',
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  ('vitamin c', 'vitamin d'): 'No Interaction',
75
  ('vitamin d', 'vitamin c'): 'No Interaction',
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  }
77
 
78
+ # Function to fetch drug features from PubChem
79
+ def get_pubchem_features(drug_name):
80
+ try:
81
+ compounds = pcp.get_compounds(drug_name, 'name')
82
+ if not compounds:
83
+ return None
84
+ compound = compounds[0] # Take the first match
85
+ return {
86
+ 'molecular_weight': compound.molecular_weight,
87
+ 'xlogp': compound.xlogp if compound.xlogp else 0,
88
+ 'tpsa': compound.tpsa if compound.tpsa else 0,
89
+ 'h_bond_donor_count': compound.h_bond_donor_count,
90
+ 'h_bond_acceptor_count': compound.h_bond_acceptor_count
91
+ }
92
+ except Exception as e:
93
+ print(f"Error fetching PubChem data for {drug_name}: {e}")
94
+ return None
95
+
96
+ # Simple prediction based on PubChem features (placeholder logic)
97
+ def predict_from_features(drug1_features, drug2_features):
98
+ if not drug1_features or not drug2_features:
99
+ return "❓ **NO DATA AVAILABLE**: Unable to fetch features for prediction."
100
+
101
+ # Placeholder: Compare molecular weight difference and XLogP similarity
102
+ weight_diff = abs(drug1_features['molecular_weight'] - drug2_features['xlogp'])
103
+ if weight_diff > 200: # Arbitrary threshold for severe interaction
104
+ return "🚨 **SEVERE INTERACTION**: Predicted based on feature difference.\n⚠️ Consult healthcare provider immediately."
105
+ elif weight_diff > 100:
106
+ return "⚠️ **MODERATE INTERACTION**: Predicted based on feature difference.\nπŸ“‹ Requires monitoring."
107
+ else:
108
+ return "βœ… **NO INTERACTION**: Predicted based on feature similarity.\n🟒 Appears safe to use together."
109
 
110
+ def predict_interaction(drug_names, dataset_db, fallback_db):
111
+ """Predict interaction between two drugs using dataset, fallback, and PubChem"""
112
  try:
113
  if not drug_names or ',' not in drug_names:
114
  return "❌ Please enter two drug names separated by a comma (e.g., 'Warfarin, Aspirin')"
115
 
 
116
  drugs = [drug.strip() for drug in drug_names.split(',')]
117
  if len(drugs) != 2:
118
  return "❌ Please enter exactly two drug names separated by a comma"
 
120
  drug1, drug2 = drugs[0].lower(), drugs[1].lower()
121
  print(f"Looking up: '{drug1}' + '{drug2}'")
122
 
123
+ # Check dataset first
124
+ prediction = dataset_db.get((drug1, drug2))
 
125
  if prediction:
 
126
  if prediction.lower() == 'severe':
127
  return f"🚨 **SEVERE INTERACTION**: {prediction}\n⚠️ This combination may be life-threatening. Consult healthcare provider immediately."
128
  elif prediction.lower() == 'moderate':
 
133
  return f"βœ… **NO INTERACTION**: {prediction}\n🟒 These drugs appear to be safe to use together."
134
  else:
135
  return f"πŸ“Š **INTERACTION LEVEL**: {prediction}"
136
+
137
+ # Check fallback database if not in dataset
138
+ fallback_prediction = fallback_db.get((drug1, drug2))
139
+ if fallback_prediction:
140
+ if fallback_prediction.lower() == 'severe':
141
+ return f"🚨 **SEVERE INTERACTION**: {fallback_prediction} (from fallback)\n⚠️ This combination may be life-threatening. Consult healthcare provider immediately."
142
+ elif fallback_prediction.lower() == 'moderate':
143
+ return f"⚠️ **MODERATE INTERACTION**: {fallback_prediction} (from fallback)\nπŸ“‹ Requires monitoring. Consult healthcare provider."
144
+ elif fallback_prediction.lower() == 'mild':
145
+ return f"⚑ **MILD INTERACTION**: {fallback_prediction} (from fallback)\nπŸ’‘ Minimal clinical significance but monitor for effects."
146
+ elif 'no interaction' in fallback_prediction.lower():
147
+ return f"βœ… **NO INTERACTION**: {fallback_prediction} (from fallback)\n🟒 These drugs appear to be safe to use together."
 
 
148
  else:
149
+ return f"πŸ“Š **INTERACTION LEVEL**: {fallback_prediction} (from fallback)"
150
+
151
+ # If not in fallback, fetch PubChem features and predict
152
+ drug1_features = get_pubchem_features(drug1)
153
+ drug2_features = get_pubchem_features(drug2)
154
+ if drug1_features and drug2_features:
155
+ return predict_from_features(drug1_features, drug2_features)
156
+ else:
157
+ found_drugs = set()
158
+ for d1, d2 in {**dataset_db, **fallback_db}.keys():
159
+ found_drugs.add(d1)
160
+ found_drugs.add(d2)
161
+ return f"❓ **NO DATA AVAILABLE**: No interaction data for '{drugs[0]}' and '{drugs[1]}'.\nπŸ’‘ Known drugs: {sorted(list(found_drugs))[:5]}... Consult healthcare provider."
162
 
163
  except Exception as e:
164
  return f"❌ Error: {str(e)}"
165
 
166
+ # Load your dataset and fallback
167
+ print("Loading drug interaction dataset...")
168
+ dataset_db, fallback_db = load_drug_interaction_dataset()
169
+
170
  # Create interface
171
  with gr.Blocks(title="Drug Interaction Predictor", theme=gr.themes.Soft()) as demo:
172
  gr.Markdown("# πŸ’Š Drug Interaction Predictor")
173
+ gr.Markdown("**Predict potential drug interactions using clinical data and PubChem**")
174
 
175
  with gr.Row():
176
  with gr.Column():
 
191
  )
192
 
193
  # Show dataset info and debugging
194
+ gr.Markdown(f"*πŸ“Š Dataset loaded with {len(dataset_db)} drug pair interactions*")
195
+ gr.Markdown(f"*πŸ“‹ Fallback database contains {len(fallback_db)} drug pair interactions*")
196
 
197
  # Add a debug section to show what's actually in the database
198
+ sample_dataset_pairs = list(dataset_db.items())[:5]
199
+ sample_fallback_pairs = list(fallback_db.items())[:5]
200
+ debug_info = "Sample dataset entries:\n" + "\n".join([f"β€’ {k}: {v}" for k, v in sample_dataset_pairs]) + "\n\nSample fallback entries:\n" + "\n".join([f"β€’ {k}: {v}" for k, v in sample_fallback_pairs])
201
  gr.Markdown(f"```\n{debug_info}\n```")
202
 
203
+ # Examples from your dataset and fallback
204
  gr.Examples(
205
  examples=[
206
  "Warfarin, Aspirin",
 
213
  label="πŸ§ͺ Try these examples:"
214
  )
215
 
216
+ predict_btn.click(fn=lambda x: predict_interaction(x, dataset_db, fallback_db), inputs=drug_input, outputs=output)
217
 
218
  # Add disclaimer
219
  gr.Markdown("""