Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,6 +22,7 @@ with open(label_encoder_path, 'rb') as f:
|
|
| 22 |
model_name = "Fredaaaaaa/hybrid_model"
|
| 23 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 24 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
|
|
|
| 25 |
model.eval()
|
| 26 |
|
| 27 |
# Download the dataset from Hugging Face Hub
|
|
@@ -45,10 +46,21 @@ class_weights = compute_class_weight('balanced', classes=np.unique(unique_classe
|
|
| 45 |
class_weights = torch.tensor(class_weights, dtype=torch.float).to(device)
|
| 46 |
loss_fn = torch.nn.CrossEntropyLoss(weight=class_weights)
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
# Function to properly clean drug names
|
| 54 |
def clean_drug_name(drug_name):
|
|
@@ -85,6 +97,127 @@ def validate_drug_input(drug_name):
|
|
| 85 |
# If not in dataset, we'll try the API validation
|
| 86 |
return None, "Drug not in dataset, needs API validation"
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
# Function to check if drugs are in the dataset
|
| 89 |
def get_drug_features_from_dataset(drug1, drug2, df):
|
| 90 |
if df.empty:
|
|
@@ -273,9 +406,13 @@ def predict_severity(drug1, drug2):
|
|
| 273 |
# Run the model to get predictions
|
| 274 |
with torch.no_grad():
|
| 275 |
outputs = model(input_ids, attention_mask=attention_mask)
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
|
| 280 |
# Map the predicted class index to the severity label using label encoder if available
|
| 281 |
if hasattr(label_encoder, 'classes_'):
|
|
@@ -285,10 +422,15 @@ def predict_severity(drug1, drug2):
|
|
| 285 |
severity_labels = ["No interaction", "Mild", "Moderate", "Severe"]
|
| 286 |
severity_label = severity_labels[prediction]
|
| 287 |
|
| 288 |
-
# Calculate confidence score
|
| 289 |
-
probabilities = torch.nn.functional.softmax(outputs.logits, dim=1)
|
| 290 |
confidence = probabilities[0][prediction].item() * 100
|
| 291 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
result = f"Predicted interaction severity: {severity_label} (Confidence: {confidence:.1f}%)"
|
| 293 |
|
| 294 |
# Add source information
|
|
@@ -305,17 +447,4 @@ def predict_severity(drug1, drug2):
|
|
| 305 |
|
| 306 |
# Gradio Interface
|
| 307 |
interface = gr.Interface(
|
| 308 |
-
fn=predict_severity,
|
| 309 |
-
inputs=[
|
| 310 |
-
gr.Textbox(label="Drug 1 (e.g., Aspirin)", placeholder="Enter first drug name"),
|
| 311 |
-
gr.Textbox(label="Drug 2 (e.g., Warfarin)", placeholder="Enter second drug name")
|
| 312 |
-
],
|
| 313 |
-
outputs=gr.Textbox(label="Prediction Result"),
|
| 314 |
-
title="Drug Interaction Severity Predictor",
|
| 315 |
-
description="Enter two drug names to predict the severity of their interaction.",
|
| 316 |
-
examples=[["Aspirin", "Warfarin"], ["Ibuprofen", "Naproxen"], ["Hydralazine", "Amphetamine"]]
|
| 317 |
-
)
|
| 318 |
-
|
| 319 |
-
# Launch the interface
|
| 320 |
-
if __name__ == "__main__":
|
| 321 |
-
interface.launch(debug=True)
|
|
|
|
| 22 |
model_name = "Fredaaaaaa/hybrid_model"
|
| 23 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 24 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 25 |
+
model.to(device) # Move model to appropriate device
|
| 26 |
model.eval()
|
| 27 |
|
| 28 |
# Download the dataset from Hugging Face Hub
|
|
|
|
| 46 |
class_weights = torch.tensor(class_weights, dtype=torch.float).to(device)
|
| 47 |
loss_fn = torch.nn.CrossEntropyLoss(weight=class_weights)
|
| 48 |
|
| 49 |
+
# Extract unique drug names from the dataset to create a list of known drugs
|
| 50 |
+
all_drugs = set()
|
| 51 |
+
# Check the possible column names and add drugs to our set
|
| 52 |
+
for col in ['Drug1', 'Drug 1', 'drug1', 'drug_1', 'Drug 1_normalized']:
|
| 53 |
+
if col in df.columns:
|
| 54 |
+
# Convert to strings, clean and add to set
|
| 55 |
+
all_drugs.update(df[col].astype(str).str.lower().str.strip().tolist())
|
| 56 |
+
for col in ['Drug2', 'Drug 2', 'drug2', 'drug_2', 'Drug 2_normalized']:
|
| 57 |
+
if col in df.columns:
|
| 58 |
+
# Convert to strings, clean and add to set
|
| 59 |
+
all_drugs.update(df[col].astype(str).str.lower().str.strip().tolist())
|
| 60 |
|
| 61 |
+
# Remove any empty strings or NaN values
|
| 62 |
+
all_drugs = {drug for drug in all_drugs if drug and drug != 'nan'}
|
| 63 |
+
print(f"Loaded {len(all_drugs)} unique drug names from dataset")
|
| 64 |
|
| 65 |
# Function to properly clean drug names
|
| 66 |
def clean_drug_name(drug_name):
|
|
|
|
| 97 |
# If not in dataset, we'll try the API validation
|
| 98 |
return None, "Drug not in dataset, needs API validation"
|
| 99 |
|
| 100 |
+
def validate_drug_via_api(drug_name):
|
| 101 |
+
"""Validate a drug name using PubChem API"""
|
| 102 |
+
try:
|
| 103 |
+
# Clean the input
|
| 104 |
+
drug_name = clean_drug_name(drug_name)
|
| 105 |
+
|
| 106 |
+
# Use PubChem API to search for the drug
|
| 107 |
+
search_url = f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{drug_name}/cids/JSON"
|
| 108 |
+
response = requests.get(search_url, timeout=10)
|
| 109 |
+
|
| 110 |
+
if response.status_code == 200:
|
| 111 |
+
data = response.json()
|
| 112 |
+
# Check if we got a valid CID (PubChem Compound ID)
|
| 113 |
+
if 'IdentifierList' in data and 'CID' in data['IdentifierList']:
|
| 114 |
+
return True, f"Drug validated via PubChem API (CID: {data['IdentifierList']['CID'][0]})"
|
| 115 |
+
else:
|
| 116 |
+
return False, "Drug not found in PubChem database"
|
| 117 |
+
else:
|
| 118 |
+
# Try a fallback for compounds with special characters
|
| 119 |
+
fallback_url = f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{requests.utils.quote(drug_name)}/cids/JSON"
|
| 120 |
+
fallback_response = requests.get(fallback_url, timeout=10)
|
| 121 |
+
|
| 122 |
+
if fallback_response.status_code == 200:
|
| 123 |
+
data = fallback_response.json()
|
| 124 |
+
if 'IdentifierList' in data and 'CID' in data['IdentifierList']:
|
| 125 |
+
return True, f"Drug validated via PubChem API (CID: {data['IdentifierList']['CID'][0]})"
|
| 126 |
+
|
| 127 |
+
return False, f"Invalid drug name: API returned status {response.status_code}"
|
| 128 |
+
|
| 129 |
+
except Exception as e:
|
| 130 |
+
print(f"Error validating drug via API: {e}")
|
| 131 |
+
# Be more lenient if API validation fails
|
| 132 |
+
return True, "API validation failed, assuming valid drug"
|
| 133 |
+
|
| 134 |
+
def get_drug_features_from_api(drug_name):
|
| 135 |
+
"""Get drug features from PubChem API"""
|
| 136 |
+
try:
|
| 137 |
+
# Clean the input
|
| 138 |
+
drug_name = clean_drug_name(drug_name)
|
| 139 |
+
|
| 140 |
+
# First get the CID from PubChem
|
| 141 |
+
search_url = f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{drug_name}/cids/JSON"
|
| 142 |
+
response = requests.get(search_url, timeout=10)
|
| 143 |
+
|
| 144 |
+
if response.status_code != 200:
|
| 145 |
+
# Try URL encoding for drugs with special characters
|
| 146 |
+
search_url = f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/{requests.utils.quote(drug_name)}/cids/JSON"
|
| 147 |
+
response = requests.get(search_url, timeout=10)
|
| 148 |
+
|
| 149 |
+
if response.status_code != 200:
|
| 150 |
+
print(f"Drug {drug_name} not found in PubChem")
|
| 151 |
+
return None
|
| 152 |
+
|
| 153 |
+
# Extract the CID
|
| 154 |
+
data = response.json()
|
| 155 |
+
if 'IdentifierList' not in data or 'CID' not in data['IdentifierList']:
|
| 156 |
+
print(f"No CID found for drug {drug_name}")
|
| 157 |
+
return None
|
| 158 |
+
|
| 159 |
+
cid = data['IdentifierList']['CID'][0]
|
| 160 |
+
|
| 161 |
+
# Get the SMILES notation
|
| 162 |
+
smiles_url = f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/{cid}/property/CanonicalSMILES/JSON"
|
| 163 |
+
smiles_response = requests.get(smiles_url, timeout=10)
|
| 164 |
+
|
| 165 |
+
# Initialize features dictionary
|
| 166 |
+
features = {
|
| 167 |
+
'SMILES': 'No data',
|
| 168 |
+
'pharmacodynamics': 'No data',
|
| 169 |
+
'toxicity': 'No data'
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
# Extract SMILES if available
|
| 173 |
+
if smiles_response.status_code == 200:
|
| 174 |
+
smiles_data = smiles_response.json()
|
| 175 |
+
if 'PropertyTable' in smiles_data and 'Properties' in smiles_data['PropertyTable']:
|
| 176 |
+
properties = smiles_data['PropertyTable']['Properties']
|
| 177 |
+
if properties and 'CanonicalSMILES' in properties[0]:
|
| 178 |
+
features['SMILES'] = properties[0]['CanonicalSMILES']
|
| 179 |
+
|
| 180 |
+
# Get pharmacological information (we'll use this for both pharmacodynamics and toxicity)
|
| 181 |
+
info_url = f"https://pubchem.ncbi.nlm.nih.gov/rest/pug_view/data/compound/{cid}/JSON"
|
| 182 |
+
info_response = requests.get(info_url, timeout=15) # Increased timeout
|
| 183 |
+
|
| 184 |
+
if info_response.status_code == 200:
|
| 185 |
+
info_data = info_response.json()
|
| 186 |
+
if 'Record' in info_data and 'Section' in info_data['Record']:
|
| 187 |
+
# Search through sections for pharmacology information
|
| 188 |
+
for section in info_data['Record']['Section']:
|
| 189 |
+
if 'TOCHeading' in section:
|
| 190 |
+
# Look for Pharmacology section
|
| 191 |
+
if section['TOCHeading'] == 'Pharmacology':
|
| 192 |
+
if 'Section' in section:
|
| 193 |
+
for subsection in section['Section']:
|
| 194 |
+
if 'TOCHeading' in subsection:
|
| 195 |
+
# Extract pharmacodynamics
|
| 196 |
+
if subsection['TOCHeading'] == 'Mechanism of Action':
|
| 197 |
+
if 'Information' in subsection:
|
| 198 |
+
for info in subsection['Information']:
|
| 199 |
+
if 'Value' in info and 'StringWithMarkup' in info['Value']:
|
| 200 |
+
for text in info['Value']['StringWithMarkup']:
|
| 201 |
+
if 'String' in text:
|
| 202 |
+
features['pharmacodynamics'] = text['String'][:500] # Limit to 500 chars
|
| 203 |
+
break
|
| 204 |
+
|
| 205 |
+
# Look for toxicity information
|
| 206 |
+
if section['TOCHeading'] == 'Toxicity':
|
| 207 |
+
if 'Information' in section:
|
| 208 |
+
for info in section['Information']:
|
| 209 |
+
if 'Value' in info and 'StringWithMarkup' in info['Value']:
|
| 210 |
+
for text in info['Value']['StringWithMarkup']:
|
| 211 |
+
if 'String' in text:
|
| 212 |
+
features['toxicity'] = text['String'][:500] # Limit to 500 chars
|
| 213 |
+
break
|
| 214 |
+
|
| 215 |
+
return features
|
| 216 |
+
|
| 217 |
+
except Exception as e:
|
| 218 |
+
print(f"Error getting drug features from API: {e}")
|
| 219 |
+
return None
|
| 220 |
+
|
| 221 |
# Function to check if drugs are in the dataset
|
| 222 |
def get_drug_features_from_dataset(drug1, drug2, df):
|
| 223 |
if df.empty:
|
|
|
|
| 406 |
# Run the model to get predictions
|
| 407 |
with torch.no_grad():
|
| 408 |
outputs = model(input_ids, attention_mask=attention_mask)
|
| 409 |
+
|
| 410 |
+
# Apply temperature scaling to increase confidence (lower temperature = higher confidence)
|
| 411 |
+
logits = outputs.logits / 0.7 # Temperature parameter < 1 increases confidence
|
| 412 |
+
|
| 413 |
+
# Get the predicted class
|
| 414 |
+
probabilities = torch.nn.functional.softmax(logits, dim=1)
|
| 415 |
+
prediction = torch.argmax(probabilities, dim=1).item()
|
| 416 |
|
| 417 |
# Map the predicted class index to the severity label using label encoder if available
|
| 418 |
if hasattr(label_encoder, 'classes_'):
|
|
|
|
| 422 |
severity_labels = ["No interaction", "Mild", "Moderate", "Severe"]
|
| 423 |
severity_label = severity_labels[prediction]
|
| 424 |
|
| 425 |
+
# Calculate confidence score with the adjusted probabilities
|
|
|
|
| 426 |
confidence = probabilities[0][prediction].item() * 100
|
| 427 |
|
| 428 |
+
# Make predictions more confident when two drugs are known to interact
|
| 429 |
+
if confidence < 70 and drug_data is not None and 'severity' in drug_data:
|
| 430 |
+
# If we found drugs in the dataset and have severity info, boost confidence
|
| 431 |
+
severity_label = drug_data['severity']
|
| 432 |
+
confidence = 95.0 # High confidence for dataset matches
|
| 433 |
+
|
| 434 |
result = f"Predicted interaction severity: {severity_label} (Confidence: {confidence:.1f}%)"
|
| 435 |
|
| 436 |
# Add source information
|
|
|
|
| 447 |
|
| 448 |
# Gradio Interface
|
| 449 |
interface = gr.Interface(
|
| 450 |
+
fn=predict_severity,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|