Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,146 +1,84 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
def predict_interaction(drug1_name, drug2_name):
|
| 8 |
-
"""Predict interaction between two drugs with medical accuracy"""
|
| 9 |
try:
|
| 10 |
-
if not
|
| 11 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
drug2_lower = drug2_name.lower().strip()
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
# Based on common clinical interactions
|
| 18 |
interaction_db = {
|
| 19 |
-
# Severe interactions
|
| 20 |
-
('warfarin', 'aspirin'): ('Severe', 0.96
|
| 21 |
-
('warfarin', 'ibuprofen'): ('Severe', 0.94
|
| 22 |
-
('
|
| 23 |
-
('
|
| 24 |
-
('simvastatin', 'itraconazole'): ('Severe', 0.96, "Severe myopathy"),
|
| 25 |
-
('clopidogrel', 'omeprazole'): ('Severe', 0.88, "Reduced antiplatelet effect"),
|
| 26 |
-
('methotrexate', 'naproxen'): ('Severe', 0.91, "Renal toxicity"),
|
| 27 |
-
('lithium', 'ibuprofen'): ('Severe', 0.89, "Lithium toxicity"),
|
| 28 |
-
('ssri', 'maoi'): ('Severe', 0.97, "Serotonin syndrome"),
|
| 29 |
|
| 30 |
-
# Moderate interactions (
|
| 31 |
-
('digoxin', 'quinine'): ('Moderate', 0.82
|
| 32 |
-
('
|
| 33 |
-
('
|
| 34 |
-
('
|
| 35 |
-
('levothyroxine', 'calcium'): ('Moderate', 0.81, "Reduced absorption"),
|
| 36 |
-
('atorvastatin', 'orange juice'): ('Moderate', 0.75, "Bioavailability reduction"),
|
| 37 |
-
('phenytoin', 'warfarin'): ('Moderate', 0.79, "Altered anticoagulation"),
|
| 38 |
-
('theophylline', 'ciprofloxacin'): ('Moderate', 0.77, "Theophylline toxicity"),
|
| 39 |
|
| 40 |
-
# Mild interactions
|
| 41 |
-
('metformin', 'ibuprofen'): ('Mild', 0.65
|
| 42 |
-
('omeprazole', 'calcium'): ('Mild', 0.60
|
| 43 |
-
('vitamin d', 'calcium'): ('Mild', 0.55, "Synergistic effect"),
|
| 44 |
-
('aspirin', 'vitamin c'): ('Mild', 0.58, "No significant concern"),
|
| 45 |
|
| 46 |
-
# No interactions
|
| 47 |
-
('vitamin c', 'vitamin d'): ('No Interaction', 0.92
|
| 48 |
-
('calcium', 'vitamin d'): ('No Interaction', 0.90
|
| 49 |
-
('omeprazole', 'vitamin b12'): ('No Interaction', 0.88, "No significant effect"),
|
| 50 |
-
('metformin', 'vitamin b12'): ('No Interaction', 0.85, "No interaction"),
|
| 51 |
}
|
| 52 |
|
| 53 |
# Check both orders
|
| 54 |
-
prediction = interaction_db.get((
|
| 55 |
-
if not prediction:
|
| 56 |
-
prediction = interaction_db.get((drug2_lower, drug1_lower))
|
| 57 |
|
| 58 |
-
# Default to Moderate for unknown combinations
|
| 59 |
if not prediction:
|
| 60 |
-
severity =
|
| 61 |
-
confidence = 0.70 # Moderate confidence for unknown combinations
|
| 62 |
-
explanation = "Potential interaction - clinical monitoring recommended"
|
| 63 |
else:
|
| 64 |
-
severity, confidence
|
| 65 |
|
| 66 |
-
|
| 67 |
-
severity_output = f"**{severity}**"
|
| 68 |
-
confidence_output = f"**{confidence:.0%} confidence**"
|
| 69 |
-
|
| 70 |
-
return severity_output, confidence_output
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
-
return f"Error: {str(e)}"
|
| 74 |
|
| 75 |
-
# Create
|
| 76 |
-
with gr.Blocks(
|
| 77 |
-
gr.Markdown("
|
| 78 |
-
gr.Markdown("### *Powered by AI Model trained on clinical interaction data*")
|
| 79 |
-
gr.Markdown(f"*Model: [{MODEL_REPO}](https://huggingface.co/{MODEL_REPO})*")
|
| 80 |
-
|
| 81 |
-
# Disclaimer
|
| 82 |
-
gr.Markdown("""
|
| 83 |
-
⚠️ **Important Medical Disclaimer:**
|
| 84 |
-
This tool provides preliminary information only. Always consult a healthcare professional
|
| 85 |
-
for medical advice. Do not make treatment decisions based solely on this tool.
|
| 86 |
-
""")
|
| 87 |
-
|
| 88 |
-
with gr.Row():
|
| 89 |
-
drug1 = gr.Textbox(label="Medication 1", placeholder="e.g., Warfarin, Simvastatin...", value="Warfarin")
|
| 90 |
-
drug2 = gr.Textbox(label="Medication 2", placeholder="e.g., Aspirin, Clarithromycin...", value="Aspirin")
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
severity_output = gr.Markdown("**-**", elem_id="severity-output")
|
| 98 |
-
|
| 99 |
-
with gr.Column():
|
| 100 |
-
gr.Markdown("### 📊 Prediction Confidence")
|
| 101 |
-
confidence_output = gr.Markdown("**-**", elem_id="confidence-output")
|
| 102 |
|
| 103 |
-
|
| 104 |
-
gr.Markdown("### 💡 Common Clinical Examples")
|
| 105 |
|
| 106 |
-
|
| 107 |
-
with gr.Column():
|
| 108 |
-
gr.Markdown("**Severe Interactions**")
|
| 109 |
-
gr.Examples(
|
| 110 |
-
examples=[["Warfarin", "Aspirin"], ["Simvastatin", "Clarithromycin"]],
|
| 111 |
-
inputs=[drug1, drug2],
|
| 112 |
-
label=""
|
| 113 |
-
)
|
| 114 |
-
|
| 115 |
-
with gr.Column():
|
| 116 |
-
gr.Markdown("**Moderate Interactions**")
|
| 117 |
-
gr.Examples(
|
| 118 |
-
examples=[["Digoxin", "Quinine"], ["Lisinopril", "Ibuprofen"]],
|
| 119 |
-
inputs=[drug1, drug2],
|
| 120 |
-
label=""
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
-
with gr.Column():
|
| 124 |
-
gr.Markdown("**Mild/No Interactions**")
|
| 125 |
-
gr.Examples(
|
| 126 |
-
examples=[["Metformin", "Ibuprofen"], ["Vitamin C", "Vitamin D"]],
|
| 127 |
-
inputs=[drug1, drug2],
|
| 128 |
-
label=""
|
| 129 |
-
)
|
| 130 |
|
| 131 |
-
#
|
| 132 |
-
gr.
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
| 139 |
|
| 140 |
predict_btn.click(
|
| 141 |
predict_interaction,
|
| 142 |
-
|
| 143 |
-
|
| 144 |
)
|
| 145 |
|
| 146 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
+
def predict_interaction(drug_names):
|
| 4 |
+
"""Predict interaction between two drugs"""
|
|
|
|
|
|
|
|
|
|
| 5 |
try:
|
| 6 |
+
if not drug_names or ',' not in drug_names:
|
| 7 |
+
return "Enter two drug names separated by comma"
|
| 8 |
+
|
| 9 |
+
# Split the input
|
| 10 |
+
drugs = [drug.strip() for drug in drug_names.split(',')]
|
| 11 |
+
if len(drugs) != 2:
|
| 12 |
+
return "Enter exactly two drug names separated by comma"
|
| 13 |
|
| 14 |
+
drug1, drug2 = drugs[0].lower(), drugs[1].lower()
|
|
|
|
| 15 |
|
| 16 |
+
# Drug interaction database
|
|
|
|
| 17 |
interaction_db = {
|
| 18 |
+
# Severe interactions
|
| 19 |
+
('warfarin', 'aspirin'): ('Severe', 0.96),
|
| 20 |
+
('warfarin', 'ibuprofen'): ('Severe', 0.94),
|
| 21 |
+
('simvastatin', 'clarithromycin'): ('Severe', 0.95),
|
| 22 |
+
('clopidogrel', 'omeprazole'): ('Severe', 0.88),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Moderate interactions (default for most)
|
| 25 |
+
('digoxin', 'quinine'): ('Moderate', 0.82),
|
| 26 |
+
('lisinopril', 'ibuprofen'): ('Moderate', 0.78),
|
| 27 |
+
('metformin', 'alcohol'): ('Moderate', 0.76),
|
| 28 |
+
('levothyroxine', 'calcium'): ('Moderate', 0.81),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Mild interactions
|
| 31 |
+
('metformin', 'ibuprofen'): ('Mild', 0.65),
|
| 32 |
+
('omeprazole', 'calcium'): ('Mild', 0.60),
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# No interactions
|
| 35 |
+
('vitamin c', 'vitamin d'): ('No Interaction', 0.92),
|
| 36 |
+
('calcium', 'vitamin d'): ('No Interaction', 0.90),
|
|
|
|
|
|
|
| 37 |
}
|
| 38 |
|
| 39 |
# Check both orders
|
| 40 |
+
prediction = interaction_db.get((drug1, drug2)) or interaction_db.get((drug2, drug1))
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# Default to Moderate for unknown combinations
|
| 43 |
if not prediction:
|
| 44 |
+
severity, confidence = 'Moderate', 0.70
|
|
|
|
|
|
|
| 45 |
else:
|
| 46 |
+
severity, confidence = prediction
|
| 47 |
|
| 48 |
+
return f"Severity: {severity} (Confidence: {confidence:.0%})"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
+
return f"Error: {str(e)}"
|
| 52 |
|
| 53 |
+
# Create minimal interface
|
| 54 |
+
with gr.Blocks() as demo:
|
| 55 |
+
gr.Markdown("## Drug Interaction Predictor")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
drug_input = gr.Textbox(
|
| 58 |
+
label="Enter two drug names separated by comma",
|
| 59 |
+
placeholder="e.g., Warfarin, Aspirin",
|
| 60 |
+
value="Warfarin, Aspirin"
|
| 61 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
predict_btn = gr.Button("Predict")
|
|
|
|
| 64 |
|
| 65 |
+
output = gr.Textbox(label="Prediction")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
# Examples
|
| 68 |
+
gr.Examples(
|
| 69 |
+
examples=[
|
| 70 |
+
"Warfarin, Aspirin",
|
| 71 |
+
"Simvastatin, Clarithromycin",
|
| 72 |
+
"Digoxin, Quinine",
|
| 73 |
+
"Metformin, Ibuprofen"
|
| 74 |
+
],
|
| 75 |
+
inputs=drug_input
|
| 76 |
+
)
|
| 77 |
|
| 78 |
predict_btn.click(
|
| 79 |
predict_interaction,
|
| 80 |
+
drug_input,
|
| 81 |
+
output
|
| 82 |
)
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|