Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,65 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
import pandas as pd
|
| 5 |
-
import
|
| 6 |
|
| 7 |
-
# Load your labeled dataset
|
| 8 |
def load_drug_interaction_dataset():
|
| 9 |
-
"""Load your labeled drug interaction dataset"""
|
| 10 |
try:
|
| 11 |
-
#
|
| 12 |
-
dataset_path =
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
df = pd.read_csv(dataset_path)
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
# Create interaction dictionary
|
| 18 |
interaction_db = {}
|
|
|
|
| 19 |
|
| 20 |
-
# Assuming your dataset has columns: Drug1, Drug2, Severity, Confidence
|
| 21 |
for _, row in df.iterrows():
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
print(f"✅
|
|
|
|
| 32 |
return interaction_db
|
| 33 |
|
| 34 |
except Exception as e:
|
| 35 |
print(f"Error loading dataset: {e}")
|
| 36 |
-
# Fallback to a basic database if dataset loading fails
|
| 37 |
return create_fallback_database()
|
| 38 |
|
| 39 |
def create_fallback_database():
|
|
@@ -88,9 +114,9 @@ def create_fallback_database():
|
|
| 88 |
('lisinopril', 'potassium'): ('No Interaction', 0.84),
|
| 89 |
('simvastatin', 'vitamin e'): ('No Interaction', 0.83),
|
| 90 |
}
|
| 91 |
-
|
| 92 |
|
| 93 |
# Load your dataset
|
|
|
|
| 94 |
interaction_db = load_drug_interaction_dataset()
|
| 95 |
|
| 96 |
def predict_interaction(drug_names):
|
|
@@ -105,16 +131,22 @@ def predict_interaction(drug_names):
|
|
| 105 |
return "Enter exactly two drug names separated by comma"
|
| 106 |
|
| 107 |
drug1, drug2 = drugs[0].lower(), drugs[1].lower()
|
|
|
|
| 108 |
|
| 109 |
# Check if this interaction exists in your dataset
|
| 110 |
prediction = interaction_db.get((drug1, drug2))
|
| 111 |
|
| 112 |
if prediction:
|
| 113 |
-
|
| 114 |
-
return f"Severity: {severity} (Confidence: {confidence:.0%}) - From your dataset"
|
| 115 |
else:
|
| 116 |
-
#
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
except Exception as e:
|
| 120 |
return f"Error: {str(e)}"
|
|
@@ -122,7 +154,7 @@ def predict_interaction(drug_names):
|
|
| 122 |
# Create interface
|
| 123 |
with gr.Blocks() as demo:
|
| 124 |
gr.Markdown("## Drug Interaction Predictor")
|
| 125 |
-
gr.Markdown("**Using your
|
| 126 |
|
| 127 |
drug_input = gr.Textbox(
|
| 128 |
label="Enter two drug names separated by comma",
|
|
@@ -133,10 +165,23 @@ with gr.Blocks() as demo:
|
|
| 133 |
predict_btn = gr.Button("Predict")
|
| 134 |
output = gr.Textbox(label="Prediction")
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
predict_btn.click(predict_interaction, drug_input, output)
|
| 137 |
|
| 138 |
if __name__ == "__main__":
|
| 139 |
-
demo.launch()
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
| 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():
|
|
|
|
| 114 |
('lisinopril', 'potassium'): ('No Interaction', 0.84),
|
| 115 |
('simvastatin', 'vitamin e'): ('No Interaction', 0.83),
|
| 116 |
}
|
|
|
|
| 117 |
|
| 118 |
# Load your dataset
|
| 119 |
+
print("Loading drug interaction dataset...")
|
| 120 |
interaction_db = load_drug_interaction_dataset()
|
| 121 |
|
| 122 |
def predict_interaction(drug_names):
|
|
|
|
| 131 |
return "Enter exactly two drug names separated by comma"
|
| 132 |
|
| 133 |
drug1, drug2 = drugs[0].lower(), drugs[1].lower()
|
| 134 |
+
print(f"Looking up: '{drug1}' + '{drug2}'")
|
| 135 |
|
| 136 |
# Check if this interaction exists in your dataset
|
| 137 |
prediction = interaction_db.get((drug1, drug2))
|
| 138 |
|
| 139 |
if prediction:
|
| 140 |
+
return f"Severity: {prediction}"
|
|
|
|
| 141 |
else:
|
| 142 |
+
# Try to find similar drugs for debugging
|
| 143 |
+
found_drugs = set()
|
| 144 |
+
for d1, d2 in interaction_db.keys():
|
| 145 |
+
found_drugs.add(d1)
|
| 146 |
+
found_drugs.add(d2)
|
| 147 |
+
|
| 148 |
+
print(f"Not found. Available drugs: {sorted(list(found_drugs))[:20]}...")
|
| 149 |
+
return f"Severity: Moderate - ({drug1}, {drug2} not found in dataset)"
|
| 150 |
|
| 151 |
except Exception as e:
|
| 152 |
return f"Error: {str(e)}"
|
|
|
|
| 154 |
# Create interface
|
| 155 |
with gr.Blocks() as demo:
|
| 156 |
gr.Markdown("## Drug Interaction Predictor")
|
| 157 |
+
gr.Markdown("**Using your merged_cleaned_dataset.csv**")
|
| 158 |
|
| 159 |
drug_input = gr.Textbox(
|
| 160 |
label="Enter two drug names separated by comma",
|
|
|
|
| 165 |
predict_btn = gr.Button("Predict")
|
| 166 |
output = gr.Textbox(label="Prediction")
|
| 167 |
|
| 168 |
+
# Show dataset info
|
| 169 |
+
gr.Markdown(f"*Dataset: merged_cleaned_dataset.csv*")
|
| 170 |
+
gr.Markdown(f"*Loaded {len(interaction_db)//2} interactions*")
|
| 171 |
+
|
| 172 |
+
# Examples from your dataset
|
| 173 |
+
gr.Examples(
|
| 174 |
+
examples=[
|
| 175 |
+
"warfarin, aspirin",
|
| 176 |
+
"simvastatin, clarithromycin",
|
| 177 |
+
"digoxin, quinine",
|
| 178 |
+
"metformin, alcohol"
|
| 179 |
+
],
|
| 180 |
+
inputs=drug_input,
|
| 181 |
+
label="Try these examples:"
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
predict_btn.click(predict_interaction, drug_input, output)
|
| 185 |
|
| 186 |
if __name__ == "__main__":
|
| 187 |
+
demo.launch()
|
|
|
|
|
|
|
|
|