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
| 2 |
+
import torch.nn.functional as F # Importing the functional module for softmax
|
| 3 |
+
from torch import nn
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Assume model and tokenizer are already loaded
|
| 8 |
+
|
| 9 |
+
def predict_drug_target_interaction(sentence):
|
| 10 |
+
# Tokenize the input sentence
|
| 11 |
+
inputs = tokenizer.encode_plus(
|
| 12 |
+
sentence,
|
| 13 |
+
add_special_tokens=True,
|
| 14 |
+
max_length=128,
|
| 15 |
+
padding="max_length",
|
| 16 |
+
truncation=True,
|
| 17 |
+
return_tensors='pt'
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
input_ids = inputs['input_ids'].to(device)
|
| 21 |
+
attention_mask = inputs['attention_mask'].to(device)
|
| 22 |
+
token_type_ids = inputs.get('token_type_ids', None)
|
| 23 |
+
if token_type_ids is not None:
|
| 24 |
+
token_type_ids = token_type_ids.to(device)
|
| 25 |
+
|
| 26 |
+
# Make prediction
|
| 27 |
+
with torch.inference_mode():
|
| 28 |
+
outputs = model(input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids)
|
| 29 |
+
logits = outputs.logits
|
| 30 |
+
probabilities = F.softmax(logits, dim=-1) # Calculate softmax probabilities
|
| 31 |
+
predictions = torch.argmax(logits, dim=-1)
|
| 32 |
+
|
| 33 |
+
# Convert predictions to human-readable labels
|
| 34 |
+
label = 'Drug-Target Interaction' if predictions.item() == 1 else 'No Interaction'
|
| 35 |
+
|
| 36 |
+
# Extract probabilities for both classes
|
| 37 |
+
prob_interaction = probabilities[0][1].item() # Probability for Drug-Target Interaction
|
| 38 |
+
prob_no_interaction = probabilities[0][0].item() # Probability for No Interaction
|
| 39 |
+
|
| 40 |
+
return label, prob_interaction, prob_no_interaction
|
| 41 |
+
|
| 42 |
+
# Gradio Interface
|
| 43 |
+
def combined_prediction_and_extraction(sentence):
|
| 44 |
+
# Get the prediction and probabilities
|
| 45 |
+
label, prob_interaction, prob_no_interaction = predict_drug_target_interaction(sentence)
|
| 46 |
+
|
| 47 |
+
if prob_interaction > prob_no_interaction:
|
| 48 |
+
response = "There is a possible interaction between drug and target in the given input! ✔️"
|
| 49 |
+
elif prob_interaction < prob_no_interaction:
|
| 50 |
+
response = "There is NO interaction between the drug and target in the given input. ❌"
|
| 51 |
+
else:
|
| 52 |
+
response = "This interaction needs further studies to classify. 🔬"
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
pred_labels_and_probs_gradio = {"Drug Interaction": prob_interaction, "No Interaction": prob_no_interaction}
|
| 56 |
+
|
| 57 |
+
return pred_labels_and_probs_gradio, response
|
| 58 |
+
|
| 59 |
+
# Description for the new tab
|
| 60 |
+
description = """
|
| 61 |
+
# The Importance of Drug-Target Interactions in Pharmaceutical Development
|
| 62 |
+
|
| 63 |
+
## 🌟 **Mechanism of Action**
|
| 64 |
+
Understanding how a drug interacts with its target—usually a protein, enzyme, or receptor—is essential for uncovering its mechanism of action. This insight helps researchers grasp the therapeutic effects of drugs, paving the way for the development of **more effective treatments**.
|
| 65 |
+
|
| 66 |
+
## 🎯 **Selectivity and Specificity**
|
| 67 |
+
Targeting specific proteins and pathways minimizes side effects and boosts drug efficacy. **High specificity** ensures that the drug primarily interacts with its intended target, reducing the risk of off-target effects that could lead to adverse reactions.
|
| 68 |
+
|
| 69 |
+
## 💡 **Drug Design and Development**
|
| 70 |
+
Knowledge of drug-target interactions is crucial for crafting new pharmaceuticals. **Optimizing lead compounds** to enhance their affinity for the target can significantly elevate a drug's effectiveness, leading to **innovative treatment solutions**.
|
| 71 |
+
|
| 72 |
+
## 🔍 **Predicting Pharmacokinetics and Pharmacodynamics**
|
| 73 |
+
Understanding these interactions aids in predicting how a drug behaves in the body, including its absorption, distribution, metabolism, and excretion. This knowledge is vital for determining **appropriate dosages** and anticipating potential drug-drug interactions.
|
| 74 |
+
|
| 75 |
+
## 🛡️ **Resistance Mechanisms**
|
| 76 |
+
In fields like oncology and infectious diseases, drug-target interactions are crucial for understanding how **resistance** to treatments develops. By studying these interactions, researchers can devise strategies to **overcome or prevent resistance**, ensuring better patient outcomes.
|
| 77 |
+
|
| 78 |
+
## ⚠️ **Safety and Toxicity**
|
| 79 |
+
Understanding how drugs interact with unintended targets is essential for assessing **safety profiles**. This information is key to identifying potential toxic effects and **mitigating risks** associated with drug therapy.
|
| 80 |
+
|
| 81 |
+
"""
|
| 82 |
+
|
| 83 |
+
datainfo = """
|
| 84 |
+
The most prominent node in the graph is metformin, which is linked to various targets. This suggests that metformin has a wide range of biological interactions, possibly indicating its involvement in multiple pathways or therapeutic effects beyond its traditional use as an anti-diabetic drug.
|
| 85 |
+
|
| 86 |
+
* Metformin interacts with AMP-activated protein kinase (AMPK), which is known to play a crucial role in cellular energy homeostasis. This aligns with metformin’s known mechanism of action in diabetes, where it helps modulate glucose metabolism through AMPK activation.
|
| 87 |
+
Other significant metformin interactions include ROR1.69 and nucleoside, indicating additional pathways of relevance, possibly connected to immune modulation or other metabolic pathways.
|
| 88 |
+
|
| 89 |
+
* Warfarin, an anticoagulant, is another highly connected node. Its targets include CYP2C9, an enzyme critical for its metabolism, and S-warfarin, which represents its active form. The network also includes its interaction with estrogen, AXIN1, and monoamine oxidase A, indicating potential off-target effects or interactions with other biological pathways.
|
| 90 |
+
|
| 91 |
+
* The interaction between serotonin transporter protein and drugs like norepinephrine and serotonin indicates involvement in pathways related to mood regulation, which could be pertinent in psychiatric conditions.
|
| 92 |
+
|
| 93 |
+
> The network shows that some drugs share common targets. Both metformin and warfarin interact with multiple common targets (like estrogen). This overlap could imply the potential for drug-drug interactions in patients taking both medications, possibly requiring careful management of therapy.
|
| 94 |
+
"""
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def display_image():
|
| 98 |
+
return "DTI_knowledge graph highlighted.png" # Change this to your image path or URL
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
title = "Unlocking Precision: Predicting Drug-Target Interactions for Safer, Smarter Treatments"
|
| 103 |
+
article = "Stay Safe!"
|
| 104 |
+
examples = [
|
| 105 |
+
["Targeting the secretion of sEV PD-L1 has emerged as a promising strategy to enhance immunotherapy"],
|
| 106 |
+
["These results suggested that AM would be a useful platform for the development of a new radiopharmaceutical targeting ER"],
|
| 107 |
+
["AP significantly depended on a three-way interaction among the mouse group ORX-KO vs WT, the wake-sleep state, and the drug or vehicle infused."],
|
| 108 |
+
["In addition, the pharmacodynamic genes SLC6A4 serotonin transporter and HTR2A serotonin-2A receptor have been examined in relation to efficacy and side effect profiles of these drugs"]
|
| 109 |
+
]
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
with gr.Blocks() as demo:
|
| 113 |
+
gr.Markdown(f"# {title}")
|
| 114 |
+
|
| 115 |
+
with gr.Tab("Model Inference"):
|
| 116 |
+
with gr.Row():
|
| 117 |
+
with gr.Column(scale=1, min_width=300):
|
| 118 |
+
input_text = gr.Textbox(label="Input", placeholder="Enter your text here...")
|
| 119 |
+
gr.Markdown("### Examples")
|
| 120 |
+
gr.Examples(
|
| 121 |
+
examples=examples,
|
| 122 |
+
inputs=input_text,
|
| 123 |
+
outputs=None, # Outputs are handled by the button click
|
| 124 |
+
label="Select an Example"
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
# Create a Row for Submit and Clear buttons
|
| 128 |
+
with gr.Row():
|
| 129 |
+
submit_button = gr.Button("Submit")
|
| 130 |
+
clear_button = gr.Button("Clear")
|
| 131 |
+
|
| 132 |
+
with gr.Column(scale=1, min_width=300):
|
| 133 |
+
predictions_output = gr.Label(num_top_classes=2, label="Predictions")
|
| 134 |
+
inference_output = gr.Textbox(label="Prediction Inference", interactive=False) # Making it read-only
|
| 135 |
+
|
| 136 |
+
# Bind the button to the prediction function
|
| 137 |
+
submit_button.click(
|
| 138 |
+
fn=combined_prediction_and_extraction,
|
| 139 |
+
inputs=input_text,
|
| 140 |
+
outputs=[predictions_output, inference_output]
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
# Bind the clear button to reset the input field and outputs
|
| 144 |
+
clear_button.click(
|
| 145 |
+
fn=lambda: ("", "", ""), # Reset input and outputs
|
| 146 |
+
inputs=None,
|
| 147 |
+
outputs=[input_text, predictions_output, inference_output]
|
| 148 |
+
)
|
| 149 |
+
with gr.Tab("Description"):
|
| 150 |
+
gr.Markdown(description)
|
| 151 |
+
|
| 152 |
+
with gr.Tab("About Data"):
|
| 153 |
+
gr.Markdown(datainfo)
|
| 154 |
+
gr.Image(value=display_image(), type="filepath")
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
# Launch the interface
|
| 158 |
+
demo.launch()
|