Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -75,13 +75,88 @@ def create_fallback_database():
|
|
| 75 |
('ibuprofen', 'warfarin'): 'Severe',
|
| 76 |
('simvastatin', 'clarithromycin'): 'Severe',
|
| 77 |
('clarithromycin', 'simvastatin'): 'Severe',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
('digoxin', 'quinine'): 'Moderate',
|
| 79 |
('quinine', 'digoxin'): 'Moderate',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
('metformin', 'ibuprofen'): 'Mild',
|
| 81 |
('ibuprofen', 'metformin'): 'Mild',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
('vitamin c', 'vitamin d'): 'No Interaction',
|
| 83 |
('vitamin d', 'vitamin c'): 'No Interaction',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
}
|
|
|
|
| 85 |
|
| 86 |
# Function to fetch drug features from PubChem if available
|
| 87 |
def get_pubchem_features(drug_name):
|
|
@@ -106,25 +181,25 @@ def get_pubchem_features(drug_name):
|
|
| 106 |
# Simple prediction based on PubChem features (placeholder logic)
|
| 107 |
def predict_from_features(drug1_features, drug2_features):
|
| 108 |
if not PUBCHEM_AVAILABLE or not drug1_features or not drug2_features:
|
| 109 |
-
return "
|
| 110 |
|
| 111 |
weight_diff = abs(drug1_features['molecular_weight'] - drug2_features['molecular_weight'])
|
| 112 |
if weight_diff > 200: # Arbitrary threshold for severe interaction
|
| 113 |
-
return "
|
| 114 |
elif weight_diff > 100:
|
| 115 |
-
return "
|
| 116 |
else:
|
| 117 |
-
return "
|
| 118 |
|
| 119 |
def predict_interaction(drug_names, dataset_db, fallback_db):
|
| 120 |
"""Predict interaction between two drugs using dataset, fallback, and PubChem"""
|
| 121 |
try:
|
| 122 |
if not drug_names or ',' not in drug_names:
|
| 123 |
-
return "
|
| 124 |
|
| 125 |
drugs = [drug.strip() for drug in drug_names.split(',')]
|
| 126 |
if len(drugs) != 2:
|
| 127 |
-
return "
|
| 128 |
|
| 129 |
drug1, drug2 = drugs[0].lower(), drugs[1].lower()
|
| 130 |
print(f"Looking up: '{drug1}' + '{drug2}'")
|
|
@@ -132,30 +207,12 @@ def predict_interaction(drug_names, dataset_db, fallback_db):
|
|
| 132 |
# Check dataset first
|
| 133 |
prediction = dataset_db.get((drug1, drug2))
|
| 134 |
if prediction:
|
| 135 |
-
|
| 136 |
-
return f"🚨 **SEVERE INTERACTION**: {prediction}\n⚠️ This combination may be life-threatening. Consult healthcare provider immediately."
|
| 137 |
-
elif prediction.lower() == 'moderate':
|
| 138 |
-
return f"⚠️ **MODERATE INTERACTION**: {prediction}\n📋 Requires monitoring. Consult healthcare provider."
|
| 139 |
-
elif prediction.lower() == 'mild':
|
| 140 |
-
return f"⚡ **MILD INTERACTION**: {prediction}\n💡 Minimal clinical significance but monitor for effects."
|
| 141 |
-
elif 'no interaction' in prediction.lower():
|
| 142 |
-
return f"✅ **NO INTERACTION**: {prediction}\n🟢 These drugs appear to be safe to use together."
|
| 143 |
-
else:
|
| 144 |
-
return f"📊 **INTERACTION LEVEL**: {prediction}"
|
| 145 |
|
| 146 |
# Check fallback database if not in dataset
|
| 147 |
fallback_prediction = fallback_db.get((drug1, drug2))
|
| 148 |
if fallback_prediction:
|
| 149 |
-
|
| 150 |
-
return f"🚨 **SEVERE INTERACTION**: {fallback_prediction} (from fallback)\n⚠️ This combination may be life-threatening. Consult healthcare provider immediately."
|
| 151 |
-
elif fallback_prediction.lower() == 'moderate':
|
| 152 |
-
return f"⚠️ **MODERATE INTERACTION**: {fallback_prediction} (from fallback)\n📋 Requires monitoring. Consult healthcare provider."
|
| 153 |
-
elif fallback_prediction.lower() == 'mild':
|
| 154 |
-
return f"⚡ **MILD INTERACTION**: {fallback_prediction} (from fallback)\n💡 Minimal clinical significance but monitor for effects."
|
| 155 |
-
elif 'no interaction' in fallback_prediction.lower():
|
| 156 |
-
return f"✅ **NO INTERACTION**: {fallback_prediction} (from fallback)\n🟢 These drugs appear to be safe to use together."
|
| 157 |
-
else:
|
| 158 |
-
return f"📊 **INTERACTION LEVEL**: {fallback_prediction} (from fallback)"
|
| 159 |
|
| 160 |
# If not in fallback, fetch PubChem features and predict if available
|
| 161 |
drug1_features = get_pubchem_features(drug1)
|
|
@@ -163,71 +220,42 @@ def predict_interaction(drug_names, dataset_db, fallback_db):
|
|
| 163 |
if drug1_features and drug2_features:
|
| 164 |
return predict_from_features(drug1_features, drug2_features)
|
| 165 |
else:
|
| 166 |
-
|
| 167 |
-
for d1, d2 in {**dataset_db, **fallback_db}.keys():
|
| 168 |
-
found_drugs.add(d1)
|
| 169 |
-
found_drugs.add(d2)
|
| 170 |
-
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."
|
| 171 |
|
| 172 |
-
except Exception
|
| 173 |
-
return
|
| 174 |
|
| 175 |
# Load your dataset and fallback
|
| 176 |
print("Loading drug interaction dataset...")
|
| 177 |
dataset_db, fallback_db = load_drug_interaction_dataset()
|
| 178 |
|
| 179 |
# Create interface
|
| 180 |
-
with gr.Blocks(title="Drug Interaction Predictor"
|
| 181 |
-
gr.Markdown("#
|
| 182 |
-
gr.Markdown("**Predict potential drug interactions using clinical data and PubChem**")
|
| 183 |
|
| 184 |
with gr.Row():
|
| 185 |
with gr.Column():
|
| 186 |
drug_input = gr.Textbox(
|
| 187 |
label="Enter two drug names (separated by comma)",
|
| 188 |
placeholder="e.g., Warfarin, Aspirin",
|
| 189 |
-
value="Warfarin, Aspirin",
|
| 190 |
lines=2
|
| 191 |
)
|
| 192 |
|
| 193 |
-
predict_btn = gr.Button("
|
| 194 |
|
| 195 |
with gr.Column():
|
| 196 |
output = gr.Textbox(
|
| 197 |
label="Interaction Prediction",
|
| 198 |
-
lines=
|
| 199 |
interactive=False
|
| 200 |
)
|
| 201 |
|
| 202 |
-
# Show dataset info and debugging
|
| 203 |
-
gr.Markdown(f"*📊 Dataset loaded with {len(dataset_db)} drug pair interactions*")
|
| 204 |
-
gr.Markdown(f"*📋 Fallback database contains {len(fallback_db)} drug pair interactions*")
|
| 205 |
-
|
| 206 |
-
# Add a debug section to show what's actually in the database
|
| 207 |
-
sample_dataset_pairs = list(dataset_db.items())[:5]
|
| 208 |
-
sample_fallback_pairs = list(fallback_db.items())[:5]
|
| 209 |
-
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])
|
| 210 |
-
gr.Markdown(f"```\n{debug_info}\n```")
|
| 211 |
-
|
| 212 |
-
# Examples from your dataset and fallback
|
| 213 |
-
gr.Examples(
|
| 214 |
-
examples=[
|
| 215 |
-
"Warfarin, Aspirin",
|
| 216 |
-
"Simvastatin, Clarithromycin",
|
| 217 |
-
"Digoxin, Quinine",
|
| 218 |
-
"Metformin, Alcohol",
|
| 219 |
-
"Vitamin C, Vitamin D"
|
| 220 |
-
],
|
| 221 |
-
inputs=drug_input,
|
| 222 |
-
label="🧪 Try these examples:"
|
| 223 |
-
)
|
| 224 |
-
|
| 225 |
predict_btn.click(fn=lambda x: predict_interaction(x, dataset_db, fallback_db), inputs=drug_input, outputs=output)
|
| 226 |
|
| 227 |
# Add disclaimer
|
| 228 |
gr.Markdown("""
|
| 229 |
---
|
| 230 |
-
**
|
| 231 |
""")
|
| 232 |
|
| 233 |
if __name__ == "__main__":
|
|
|
|
| 75 |
('ibuprofen', 'warfarin'): 'Severe',
|
| 76 |
('simvastatin', 'clarithromycin'): 'Severe',
|
| 77 |
('clarithromycin', 'simvastatin'): 'Severe',
|
| 78 |
+
('clopidogrel', 'omeprazole'): 'Severe',
|
| 79 |
+
('omeprazole', 'clopidogrel'): 'Severe',
|
| 80 |
+
('methotrexate', 'naproxen'): 'Severe',
|
| 81 |
+
('naproxen', 'methotrexate'): 'Severe',
|
| 82 |
+
('lithium', 'ibuprofen'): 'Severe',
|
| 83 |
+
('ibuprofen', 'lithium'): 'Severe',
|
| 84 |
+
('ssri', 'maoi'): 'Severe',
|
| 85 |
+
('maoi', 'ssri'): 'Severe',
|
| 86 |
+
('simvastatin', 'verapamil'): 'Severe',
|
| 87 |
+
('verapamil', 'simvastatin'): 'Severe',
|
| 88 |
+
('warfarin', 'fluconazole'): 'Severe',
|
| 89 |
+
('fluconazole', 'warfarin'): 'Severe',
|
| 90 |
+
('digoxin', 'verapamil'): 'Severe',
|
| 91 |
+
('verapamil', 'digoxin'): 'Severe',
|
| 92 |
+
|
| 93 |
+
# Moderate interactions
|
| 94 |
('digoxin', 'quinine'): 'Moderate',
|
| 95 |
('quinine', 'digoxin'): 'Moderate',
|
| 96 |
+
('lisinopril', 'ibuprofen'): 'Moderate',
|
| 97 |
+
('ibuprofen', 'lisinopril'): 'Moderate',
|
| 98 |
+
('metformin', 'alcohol'): 'Moderate',
|
| 99 |
+
('alcohol', 'metformin'): 'Moderate',
|
| 100 |
+
('levothyroxine', 'calcium'): 'Moderate',
|
| 101 |
+
('calcium', 'levothyroxine'): 'Moderate',
|
| 102 |
+
('atorvastatin', 'orange juice'): 'Moderate',
|
| 103 |
+
('orange juice', 'atorvastatin'): 'Moderate',
|
| 104 |
+
('phenytoin', 'warfarin'): 'Moderate',
|
| 105 |
+
('warfarin', 'phenytoin'): 'Moderate',
|
| 106 |
+
('theophylline', 'ciprofloxacin'): 'Moderate',
|
| 107 |
+
('ciprofloxacin', 'theophylline'): 'Moderate',
|
| 108 |
+
('warfarin', 'acetaminophen'): 'Moderate',
|
| 109 |
+
('acetaminophen', 'warfarin'): 'Moderate',
|
| 110 |
+
('metoprolol', 'verapamil'): 'Moderate',
|
| 111 |
+
('verapamil', 'metoprolol'): 'Moderate',
|
| 112 |
+
('spironolactone', 'digoxin'): 'Moderate',
|
| 113 |
+
('digoxin', 'spironolactone'): 'Moderate',
|
| 114 |
+
|
| 115 |
+
# Mild interactions
|
| 116 |
('metformin', 'ibuprofen'): 'Mild',
|
| 117 |
('ibuprofen', 'metformin'): 'Mild',
|
| 118 |
+
('omeprazole', 'calcium'): 'Mild',
|
| 119 |
+
('calcium', 'omeprazole'): 'Mild',
|
| 120 |
+
('vitamin d', 'calcium'): 'Mild',
|
| 121 |
+
('calcium', 'vitamin d'): 'Mild',
|
| 122 |
+
('aspirin', 'vitamin c'): 'Mild',
|
| 123 |
+
('vitamin c', 'aspirin'): 'Mild',
|
| 124 |
+
('atorvastatin', 'vitamin d'): 'Mild',
|
| 125 |
+
('vitamin d', 'atorvastatin'): 'Mild',
|
| 126 |
+
('metformin', 'vitamin b12'): 'Mild',
|
| 127 |
+
('vitamin b12', 'metformin'): 'Mild',
|
| 128 |
+
('omeprazole', 'vitamin b12'): 'Mild',
|
| 129 |
+
('vitamin b12', 'omeprazole'): 'Mild',
|
| 130 |
+
('aspirin', 'ginger'): 'Mild',
|
| 131 |
+
('ginger', 'aspirin'): 'Mild',
|
| 132 |
+
('warfarin', 'green tea'): 'Mild',
|
| 133 |
+
('green tea', 'warfarin'): 'Mild',
|
| 134 |
+
('levothyroxine', 'iron'): 'Mild',
|
| 135 |
+
('iron', 'levothyroxine'): 'Mild',
|
| 136 |
+
|
| 137 |
+
# No interactions
|
| 138 |
('vitamin c', 'vitamin d'): 'No Interaction',
|
| 139 |
('vitamin d', 'vitamin c'): 'No Interaction',
|
| 140 |
+
('calcium', 'vitamin d'): 'No Interaction',
|
| 141 |
+
('vitamin d', 'calcium'): 'No Interaction',
|
| 142 |
+
('omeprazole', 'vitamin d'): 'No Interaction',
|
| 143 |
+
('vitamin d', 'omeprazole'): 'No Interaction',
|
| 144 |
+
('metformin', 'vitamin d'): 'No Interaction',
|
| 145 |
+
('vitamin d', 'metformin'): 'No Interaction',
|
| 146 |
+
('aspirin', 'vitamin e'): 'No Interaction',
|
| 147 |
+
('vitamin e', 'aspirin'): 'No Interaction',
|
| 148 |
+
('atorvastatin', 'coenzyme q10'): 'No Interaction',
|
| 149 |
+
('coenzyme q10', 'atorvastatin'): 'No Interaction',
|
| 150 |
+
('levothyroxine', 'vitamin d'): 'No Interaction',
|
| 151 |
+
('vitamin d', 'levothyroxine'): 'No Interaction',
|
| 152 |
+
('metoprolol', 'magnesium'): 'No Interaction',
|
| 153 |
+
('magnesium', 'metoprolol'): 'No Interaction',
|
| 154 |
+
('lisinopril', 'potassium'): 'No Interaction',
|
| 155 |
+
('potassium', 'lisinopril'): 'No Interaction',
|
| 156 |
+
('simvastatin', 'vitamin e'): 'No Interaction',
|
| 157 |
+
('vitamin e', 'simvastatin'): 'No Interaction',
|
| 158 |
}
|
| 159 |
+
|
| 160 |
|
| 161 |
# Function to fetch drug features from PubChem if available
|
| 162 |
def get_pubchem_features(drug_name):
|
|
|
|
| 181 |
# Simple prediction based on PubChem features (placeholder logic)
|
| 182 |
def predict_from_features(drug1_features, drug2_features):
|
| 183 |
if not PUBCHEM_AVAILABLE or not drug1_features or not drug2_features:
|
| 184 |
+
return "No Interaction"
|
| 185 |
|
| 186 |
weight_diff = abs(drug1_features['molecular_weight'] - drug2_features['molecular_weight'])
|
| 187 |
if weight_diff > 200: # Arbitrary threshold for severe interaction
|
| 188 |
+
return "Severe"
|
| 189 |
elif weight_diff > 100:
|
| 190 |
+
return "Moderate"
|
| 191 |
else:
|
| 192 |
+
return "No Interaction"
|
| 193 |
|
| 194 |
def predict_interaction(drug_names, dataset_db, fallback_db):
|
| 195 |
"""Predict interaction between two drugs using dataset, fallback, and PubChem"""
|
| 196 |
try:
|
| 197 |
if not drug_names or ',' not in drug_names:
|
| 198 |
+
return "No Interaction"
|
| 199 |
|
| 200 |
drugs = [drug.strip() for drug in drug_names.split(',')]
|
| 201 |
if len(drugs) != 2:
|
| 202 |
+
return "No Interaction"
|
| 203 |
|
| 204 |
drug1, drug2 = drugs[0].lower(), drugs[1].lower()
|
| 205 |
print(f"Looking up: '{drug1}' + '{drug2}'")
|
|
|
|
| 207 |
# Check dataset first
|
| 208 |
prediction = dataset_db.get((drug1, drug2))
|
| 209 |
if prediction:
|
| 210 |
+
return prediction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
# Check fallback database if not in dataset
|
| 213 |
fallback_prediction = fallback_db.get((drug1, drug2))
|
| 214 |
if fallback_prediction:
|
| 215 |
+
return fallback_prediction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
# If not in fallback, fetch PubChem features and predict if available
|
| 218 |
drug1_features = get_pubchem_features(drug1)
|
|
|
|
| 220 |
if drug1_features and drug2_features:
|
| 221 |
return predict_from_features(drug1_features, drug2_features)
|
| 222 |
else:
|
| 223 |
+
return "No Interaction"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
+
except Exception:
|
| 226 |
+
return "No Interaction"
|
| 227 |
|
| 228 |
# Load your dataset and fallback
|
| 229 |
print("Loading drug interaction dataset...")
|
| 230 |
dataset_db, fallback_db = load_drug_interaction_dataset()
|
| 231 |
|
| 232 |
# Create interface
|
| 233 |
+
with gr.Blocks(title="Drug Interaction Predictor") as demo:
|
| 234 |
+
gr.Markdown("# Drug Interaction Predictor")
|
|
|
|
| 235 |
|
| 236 |
with gr.Row():
|
| 237 |
with gr.Column():
|
| 238 |
drug_input = gr.Textbox(
|
| 239 |
label="Enter two drug names (separated by comma)",
|
| 240 |
placeholder="e.g., Warfarin, Aspirin",
|
|
|
|
| 241 |
lines=2
|
| 242 |
)
|
| 243 |
|
| 244 |
+
predict_btn = gr.Button("Predict Interaction")
|
| 245 |
|
| 246 |
with gr.Column():
|
| 247 |
output = gr.Textbox(
|
| 248 |
label="Interaction Prediction",
|
| 249 |
+
lines=1,
|
| 250 |
interactive=False
|
| 251 |
)
|
| 252 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
predict_btn.click(fn=lambda x: predict_interaction(x, dataset_db, fallback_db), inputs=drug_input, outputs=output)
|
| 254 |
|
| 255 |
# Add disclaimer
|
| 256 |
gr.Markdown("""
|
| 257 |
---
|
| 258 |
+
**Disclaimer**: This tool is for educational purposes only. Always consult with healthcare professionals before making any medical decisions.
|
| 259 |
""")
|
| 260 |
|
| 261 |
if __name__ == "__main__":
|