Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 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
|
| 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 |
|
| 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
|
| 67 |
-
print("
|
| 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 |
-
#
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
def predict_interaction(drug_names):
|
| 162 |
-
"""Predict interaction between two drugs using
|
| 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
|
| 176 |
-
prediction =
|
| 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 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
return f"
|
| 200 |
-
elif
|
| 201 |
-
return f"
|
| 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"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|
|
|
|
| 235 |
|
| 236 |
# Add a debug section to show what's actually in the database
|
| 237 |
-
|
| 238 |
-
|
|
|
|
| 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("""
|