Fredaaaaaa commited on
Commit
5eccead
·
verified ·
1 Parent(s): 28b4311

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -25
app.py CHANGED
@@ -2,9 +2,9 @@ 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'
@@ -16,25 +16,39 @@ def load_drug_interaction_dataset():
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 index, row in df.iterrows():
31
  try:
32
- # Use your exact column names - adjust if different
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'):
@@ -48,11 +62,15 @@ def load_drug_interaction_dataset():
48
 
49
  # Add both orders to the dictionary
50
  interaction_db[(drug1, drug2)] = severity
51
- interaction_db[(drug2, drug1)] = severity # Add reverse order
52
  count += 1
53
  if count % 100 == 0: # Log progress
54
  print(f"Processed {count} interactions")
55
 
 
 
 
 
56
  except Exception as e:
57
  print(f"Error processing row {index}: {e}")
58
  continue
@@ -69,23 +87,15 @@ def create_fallback_database():
69
  """Fallback database if dataset loading fails"""
70
  print("Using fallback database")
71
  return {
72
- # Severe interactions (Life-threatening)
73
  ('warfarin', 'aspirin'): 'Severe',
74
  ('warfarin', 'ibuprofen'): 'Severe',
75
  ('simvastatin', 'clarithromycin'): 'Severe',
76
  ('clopidogrel', 'omeprazole'): 'Severe',
77
  ('methotrexate', 'naproxen'): 'Severe',
78
- # ... (rest of your fallback data)
79
- # Moderate interactions
80
  ('digoxin', 'quinine'): 'Moderate',
81
  ('lisinopril', 'ibuprofen'): 'Moderate',
82
- # ... (rest of your fallback data)
83
- # Mild interactions
84
  ('metformin', 'ibuprofen'): 'Mild',
85
- # ... (rest of your fallback data)
86
- # No interactions
87
  ('vitamin c', 'vitamin d'): 'No Interaction',
88
- # ... (rest of your fallback data)
89
  }
90
 
91
  # Load your dataset
@@ -112,13 +122,8 @@ def predict_interaction(drug_names):
112
  if prediction:
113
  return f"Predicted Interaction: {prediction}"
114
  else:
115
- # Try to find similar drugs for debugging
116
- found_drugs = set()
117
- for d1, d2 in interaction_db.keys():
118
- found_drugs.add(d1)
119
- found_drugs.add(d2)
120
-
121
- print(f"Not found. Available drugs: {sorted(list(found_drugs))[:20]}...")
122
  return f"Predicted Interaction: Moderate" # Default to Moderate if not found
123
 
124
  except Exception as e:
 
2
  import pandas as pd
3
  import os
4
 
5
+ # Load your labeled dataset with flexible column names
6
  def load_drug_interaction_dataset():
7
+ """Load your labeled drug interaction dataset with flexible column names"""
8
  try:
9
  # Your exact dataset filename
10
  dataset_path = 'merged_cleaned_dataset.csv'
 
16
  print(f"Dataset file {dataset_path} not found!")
17
  return create_fallback_database()
18
 
19
+ # Load the dataset
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
+ # Attempt to detect column names (flexible mapping)
27
+ possible_drug1_cols = ['Drug 1_normalized', 'Drug1', 'drug_1']
28
+ possible_drug2_cols = ['Drug 2_normalized', 'Drug2', 'drug_2']
29
+ possible_severity_cols = ['severity', 'Severity', 'SEVERITY']
30
+
31
+ drug1_col = next((col for col in possible_drug1_cols if col in df.columns), None)
32
+ drug2_col = next((col for col in possible_drug2_cols if col in df.columns), None)
33
+ severity_col = next((col for col in possible_severity_cols if col in df.columns), None)
34
+
35
+ if not all([drug1_col, drug2_col, severity_col]):
36
+ print(f"Required columns not found. Detected: drug1={drug1_col}, drug2={drug2_col}, severity={severity_col}")
37
+ return create_fallback_database()
38
+
39
+ print(f"Using columns: drug1={drug1_col}, drug2={drug2_col}, severity={severity_col}")
40
+
41
+ # Create interaction dictionary
42
  interaction_db = {}
43
  count = 0
44
 
45
  for index, row in df.iterrows():
46
  try:
47
+ drug1 = str(row[drug1_col]).lower().strip()
48
+ drug2 = str(row[drug2_col]).lower().strip()
49
+ severity = str(row[severity_col]).strip()
 
50
 
51
+ # Skip empty or invalid entries
52
  if (not all([drug1, drug2, severity]) or
53
  drug1 == 'nan' or drug2 == 'nan' or
54
  severity == 'nan' or severity.lower() == 'none'):
 
62
 
63
  # Add both orders to the dictionary
64
  interaction_db[(drug1, drug2)] = severity
65
+ interaction_db[(drug2, drug1)] = severity
66
  count += 1
67
  if count % 100 == 0: # Log progress
68
  print(f"Processed {count} interactions")
69
 
70
+ # Verify known pair
71
+ if drug1 == 'warfarin' and drug2 == 'aspirin':
72
+ print(f"Found Warfarin, Aspirin at row {index} with severity: {severity}")
73
+
74
  except Exception as e:
75
  print(f"Error processing row {index}: {e}")
76
  continue
 
87
  """Fallback database if dataset loading fails"""
88
  print("Using fallback database")
89
  return {
 
90
  ('warfarin', 'aspirin'): 'Severe',
91
  ('warfarin', 'ibuprofen'): 'Severe',
92
  ('simvastatin', 'clarithromycin'): 'Severe',
93
  ('clopidogrel', 'omeprazole'): 'Severe',
94
  ('methotrexate', 'naproxen'): 'Severe',
 
 
95
  ('digoxin', 'quinine'): 'Moderate',
96
  ('lisinopril', 'ibuprofen'): 'Moderate',
 
 
97
  ('metformin', 'ibuprofen'): 'Mild',
 
 
98
  ('vitamin c', 'vitamin d'): 'No Interaction',
 
99
  }
100
 
101
  # Load your dataset
 
122
  if prediction:
123
  return f"Predicted Interaction: {prediction}"
124
  else:
125
+ # Log all available keys for debugging
126
+ print(f"Keys in interaction_db: {list(interaction_db.keys())[:10]}... (total {len(interaction_db)})")
 
 
 
 
 
127
  return f"Predicted Interaction: Moderate" # Default to Moderate if not found
128
 
129
  except Exception as e: