Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,13 +21,14 @@ def load_drug_interaction_dataset():
|
|
| 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
|
| 31 |
try:
|
| 32 |
# Use your exact column names
|
| 33 |
drug1 = str(row['Drug 1_normalized']).lower().strip()
|
|
@@ -38,6 +39,7 @@ def load_drug_interaction_dataset():
|
|
| 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
|
|
@@ -45,16 +47,21 @@ def load_drug_interaction_dataset():
|
|
| 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
|
| 51 |
count += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
except Exception as e:
|
| 54 |
-
print(f"Error processing row {
|
| 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 |
|
|
@@ -66,93 +73,18 @@ def create_fallback_database():
|
|
| 66 |
"""Fallback database if dataset loading fails - FIXED STRUCTURE"""
|
| 67 |
print("Using fallback database")
|
| 68 |
return {
|
| 69 |
-
# Severe interactions (Life-threatening) - STORE ONLY SEVERITY STRINGS
|
| 70 |
('warfarin', 'aspirin'): 'Severe',
|
| 71 |
('aspirin', 'warfarin'): 'Severe',
|
| 72 |
('warfarin', 'ibuprofen'): 'Severe',
|
| 73 |
('ibuprofen', 'warfarin'): 'Severe',
|
| 74 |
('simvastatin', 'clarithromycin'): 'Severe',
|
| 75 |
('clarithromycin', 'simvastatin'): 'Severe',
|
| 76 |
-
('clopidogrel', 'omeprazole'): 'Severe',
|
| 77 |
-
('omeprazole', 'clopidogrel'): 'Severe',
|
| 78 |
-
('methotrexate', 'naproxen'): 'Severe',
|
| 79 |
-
('naproxen', 'methotrexate'): 'Severe',
|
| 80 |
-
('lithium', 'ibuprofen'): 'Severe',
|
| 81 |
-
('ibuprofen', 'lithium'): 'Severe',
|
| 82 |
-
('ssri', 'maoi'): 'Severe',
|
| 83 |
-
('maoi', 'ssri'): 'Severe',
|
| 84 |
-
('simvastatin', 'verapamil'): 'Severe',
|
| 85 |
-
('verapamil', 'simvastatin'): 'Severe',
|
| 86 |
-
('warfarin', 'fluconazole'): 'Severe',
|
| 87 |
-
('fluconazole', 'warfarin'): 'Severe',
|
| 88 |
-
('digoxin', 'verapamil'): 'Severe',
|
| 89 |
-
('verapamil', 'digoxin'): 'Severe',
|
| 90 |
-
|
| 91 |
-
# Moderate interactions (Requires monitoring)
|
| 92 |
('digoxin', 'quinine'): 'Moderate',
|
| 93 |
('quinine', 'digoxin'): 'Moderate',
|
| 94 |
-
('lisinopril', 'ibuprofen'): 'Moderate',
|
| 95 |
-
('ibuprofen', 'lisinopril'): 'Moderate',
|
| 96 |
-
('metformin', 'alcohol'): 'Moderate',
|
| 97 |
-
('alcohol', 'metformin'): 'Moderate',
|
| 98 |
-
('levothyroxine', 'calcium'): 'Moderate',
|
| 99 |
-
('calcium', 'levothyroxine'): 'Moderate',
|
| 100 |
-
('atorvastatin', 'orange juice'): 'Moderate',
|
| 101 |
-
('orange juice', 'atorvastatin'): 'Moderate',
|
| 102 |
-
('phenytoin', 'warfarin'): 'Moderate',
|
| 103 |
-
('warfarin', 'phenytoin'): 'Moderate',
|
| 104 |
-
('theophylline', 'ciprofloxacin'): 'Moderate',
|
| 105 |
-
('ciprofloxacin', 'theophylline'): 'Moderate',
|
| 106 |
-
('warfarin', 'acetaminophen'): 'Moderate',
|
| 107 |
-
('acetaminophen', 'warfarin'): 'Moderate',
|
| 108 |
-
('metoprolol', 'verapamil'): 'Moderate',
|
| 109 |
-
('verapamil', 'metoprolol'): 'Moderate',
|
| 110 |
-
('spironolactone', 'digoxin'): 'Moderate',
|
| 111 |
-
('digoxin', 'spironolactone'): 'Moderate',
|
| 112 |
-
|
| 113 |
-
# Mild interactions (Minimal clinical significance)
|
| 114 |
('metformin', 'ibuprofen'): 'Mild',
|
| 115 |
('ibuprofen', 'metformin'): 'Mild',
|
| 116 |
-
('omeprazole', 'calcium'): 'Mild',
|
| 117 |
-
('calcium', 'omeprazole'): 'Mild',
|
| 118 |
-
('vitamin d', 'calcium'): 'Mild',
|
| 119 |
-
('calcium', 'vitamin d'): 'Mild',
|
| 120 |
-
('aspirin', 'vitamin c'): 'Mild',
|
| 121 |
-
('vitamin c', 'aspirin'): 'Mild',
|
| 122 |
-
('atorvastatin', 'vitamin d'): 'Mild',
|
| 123 |
-
('vitamin d', 'atorvastatin'): 'Mild',
|
| 124 |
-
('metformin', 'vitamin b12'): 'Mild',
|
| 125 |
-
('vitamin b12', 'metformin'): 'Mild',
|
| 126 |
-
('omeprazole', 'vitamin b12'): 'Mild',
|
| 127 |
-
('vitamin b12', 'omeprazole'): 'Mild',
|
| 128 |
-
('aspirin', 'ginger'): 'Mild',
|
| 129 |
-
('ginger', 'aspirin'): 'Mild',
|
| 130 |
-
('warfarin', 'green tea'): 'Mild',
|
| 131 |
-
('green tea', 'warfarin'): 'Mild',
|
| 132 |
-
('levothyroxine', 'iron'): 'Mild',
|
| 133 |
-
('iron', 'levothyroxine'): 'Mild',
|
| 134 |
-
|
| 135 |
-
# No interactions (Clinically safe)
|
| 136 |
('vitamin c', 'vitamin d'): 'No Interaction',
|
| 137 |
('vitamin d', 'vitamin c'): 'No Interaction',
|
| 138 |
-
('calcium', 'vitamin d'): 'No Interaction',
|
| 139 |
-
('vitamin d', 'calcium'): 'No Interaction',
|
| 140 |
-
('omeprazole', 'vitamin d'): 'No Interaction',
|
| 141 |
-
('vitamin d', 'omeprazole'): 'No Interaction',
|
| 142 |
-
('metformin', 'vitamin d'): 'No Interaction',
|
| 143 |
-
('vitamin d', 'metformin'): 'No Interaction',
|
| 144 |
-
('aspirin', 'vitamin e'): 'No Interaction',
|
| 145 |
-
('vitamin e', 'aspirin'): 'No Interaction',
|
| 146 |
-
('atorvastatin', 'coenzyme q10'): 'No Interaction',
|
| 147 |
-
('coenzyme q10', 'atorvastatin'): 'No Interaction',
|
| 148 |
-
('levothyroxine', 'vitamin d'): 'No Interaction',
|
| 149 |
-
('vitamin d', 'levothyroxine'): 'No Interaction',
|
| 150 |
-
('metoprolol', 'magnesium'): 'No Interaction',
|
| 151 |
-
('magnesium', 'metoprolol'): 'No Interaction',
|
| 152 |
-
('lisinopril', 'potassium'): 'No Interaction',
|
| 153 |
-
('potassium', 'lisinopril'): 'No Interaction',
|
| 154 |
-
('simvastatin', 'vitamin e'): 'No Interaction',
|
| 155 |
-
('vitamin e', 'simvastatin'): 'No Interaction',
|
| 156 |
}
|
| 157 |
|
| 158 |
# Load your dataset
|
|
@@ -196,6 +128,7 @@ def predict_interaction(drug_names):
|
|
| 196 |
found_drugs.add(d2)
|
| 197 |
|
| 198 |
print(f"Not found. Available drugs: {sorted(list(found_drugs))[:20]}...")
|
|
|
|
| 199 |
|
| 200 |
# Check if either drug exists in the database
|
| 201 |
drug1_exists = any(d1 == drug1 or d2 == drug1 for d1, d2 in interaction_db.keys())
|
|
@@ -259,12 +192,6 @@ with gr.Blocks(title="Drug Interaction Predictor", theme=gr.themes.Soft()) as de
|
|
| 259 |
|
| 260 |
predict_btn.click(predict_interaction, drug_input, output)
|
| 261 |
|
| 262 |
-
# Debug button functionality
|
| 263 |
-
def show_debug():
|
| 264 |
-
return debug_database(), gr.update(visible=True)
|
| 265 |
-
|
| 266 |
-
debug_btn.click(show_debug, outputs=[debug_output, debug_output])
|
| 267 |
-
|
| 268 |
# Add disclaimer
|
| 269 |
gr.Markdown("""
|
| 270 |
---
|
|
|
|
| 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().to_string()}")
|
| 25 |
|
| 26 |
# Create interaction dictionary using your exact column names
|
| 27 |
interaction_db = {}
|
| 28 |
count = 0
|
| 29 |
+
severe_count = 0 # Track severe interactions
|
| 30 |
|
| 31 |
+
for index, row in df.iterrows():
|
| 32 |
try:
|
| 33 |
# Use your exact column names
|
| 34 |
drug1 = str(row['Drug 1_normalized']).lower().strip()
|
|
|
|
| 39 |
if (not all([drug1, drug2, severity]) or
|
| 40 |
drug1 == 'nan' or drug2 == 'nan' or
|
| 41 |
severity == 'nan' or severity.lower() == 'none'):
|
| 42 |
+
print(f"Skipping invalid row {index}: {drug1}, {drug2}, {severity}")
|
| 43 |
continue
|
| 44 |
|
| 45 |
# Clean up severity labels
|
|
|
|
| 47 |
if severity == 'No interaction':
|
| 48 |
severity = 'No Interaction'
|
| 49 |
|
| 50 |
+
# Add both orders to the dictionary
|
| 51 |
interaction_db[(drug1, drug2)] = severity
|
| 52 |
+
interaction_db[(drug2, drug1)] = severity
|
| 53 |
count += 1
|
| 54 |
+
if severity.lower() == 'severe':
|
| 55 |
+
severe_count += 1
|
| 56 |
+
if drug1 == 'warfarin' and drug2 == 'aspirin':
|
| 57 |
+
print(f"Found Warfarin, Aspirin at row {index} with severity: {severity}")
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
+
print(f"Error processing row {index}: {e}")
|
| 61 |
continue
|
| 62 |
|
| 63 |
print(f"✅ Successfully loaded {count} drug interactions from dataset")
|
| 64 |
+
print(f"Number of 'Severe' interactions: {severe_count}")
|
| 65 |
print(f"Sample interactions: {list(interaction_db.items())[:5]}")
|
| 66 |
return interaction_db
|
| 67 |
|
|
|
|
| 73 |
"""Fallback database if dataset loading fails - FIXED STRUCTURE"""
|
| 74 |
print("Using fallback database")
|
| 75 |
return {
|
|
|
|
| 76 |
('warfarin', 'aspirin'): 'Severe',
|
| 77 |
('aspirin', 'warfarin'): 'Severe',
|
| 78 |
('warfarin', 'ibuprofen'): 'Severe',
|
| 79 |
('ibuprofen', 'warfarin'): 'Severe',
|
| 80 |
('simvastatin', 'clarithromycin'): 'Severe',
|
| 81 |
('clarithromycin', 'simvastatin'): 'Severe',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
('digoxin', 'quinine'): 'Moderate',
|
| 83 |
('quinine', 'digoxin'): 'Moderate',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
('metformin', 'ibuprofen'): 'Mild',
|
| 85 |
('ibuprofen', 'metformin'): 'Mild',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
('vitamin c', 'vitamin d'): 'No Interaction',
|
| 87 |
('vitamin d', 'vitamin c'): 'No Interaction',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
}
|
| 89 |
|
| 90 |
# Load your dataset
|
|
|
|
| 128 |
found_drugs.add(d2)
|
| 129 |
|
| 130 |
print(f"Not found. Available drugs: {sorted(list(found_drugs))[:20]}...")
|
| 131 |
+
print(f"Keys in interaction_db: {list(interaction_db.keys())[:5]}... (total {len(interaction_db)})")
|
| 132 |
|
| 133 |
# Check if either drug exists in the database
|
| 134 |
drug1_exists = any(d1 == drug1 or d2 == drug1 for d1, d2 in interaction_db.keys())
|
|
|
|
| 192 |
|
| 193 |
predict_btn.click(predict_interaction, drug_input, output)
|
| 194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
# Add disclaimer
|
| 196 |
gr.Markdown("""
|
| 197 |
---
|