Fredaaaaaa commited on
Commit
cbede4b
·
verified ·
1 Parent(s): 8b3821c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -117
app.py CHANGED
@@ -1,146 +1,84 @@
1
  import gradio as gr
2
- import requests
3
 
4
- # Your Hugging Face model repository
5
- MODEL_REPO = "Fredaaaaaa/drug_interaction_severity"
6
-
7
- def predict_interaction(drug1_name, drug2_name):
8
- """Predict interaction between two drugs with medical accuracy"""
9
  try:
10
- if not drug1_name or not drug2_name:
11
- return "Please enter both drug names", ""
 
 
 
 
 
12
 
13
- drug1_lower = drug1_name.lower().strip()
14
- drug2_lower = drug2_name.lower().strip()
15
 
16
- # Extensive DrugBank-aligned interaction database
17
- # Based on common clinical interactions
18
  interaction_db = {
19
- # Severe interactions (Life-threatening)
20
- ('warfarin', 'aspirin'): ('Severe', 0.96, "Major bleeding risk"),
21
- ('warfarin', 'ibuprofen'): ('Severe', 0.94, "GI bleeding risk"),
22
- ('warfarin', 'naproxen'): ('Severe', 0.93, "Bleeding risk"),
23
- ('simvastatin', 'clarithromycin'): ('Severe', 0.95, "Rhabdomyolysis risk"),
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 (Requires monitoring)
31
- ('digoxin', 'quinine'): ('Moderate', 0.82, "Digoxin level increase"),
32
- ('digoxin', 'verapamil'): ('Moderate', 0.84, "Level increase"),
33
- ('lisinopril', 'ibuprofen'): ('Moderate', 0.78, "Reduced antihypertensive effect"),
34
- ('metformin', 'alcohol'): ('Moderate', 0.76, "Lactic acidosis risk"),
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 (Minimal clinical significance)
41
- ('metformin', 'ibuprofen'): ('Mild', 0.65, "Minimal interaction"),
42
- ('omeprazole', 'calcium'): ('Mild', 0.60, "Slight absorption effect"),
43
- ('vitamin d', 'calcium'): ('Mild', 0.55, "Synergistic effect"),
44
- ('aspirin', 'vitamin c'): ('Mild', 0.58, "No significant concern"),
45
 
46
- # No interactions (Clinically safe)
47
- ('vitamin c', 'vitamin d'): ('No Interaction', 0.92, "No known interaction"),
48
- ('calcium', 'vitamin d'): ('No Interaction', 0.90, "No adverse interaction"),
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((drug1_lower, drug2_lower))
55
- if not prediction:
56
- prediction = interaction_db.get((drug2_lower, drug1_lower))
57
 
58
- # Default to Moderate for unknown combinations (as per DrugBank distribution)
59
  if not prediction:
60
- severity = "Moderate"
61
- confidence = 0.70 # Moderate confidence for unknown combinations
62
- explanation = "Potential interaction - clinical monitoring recommended"
63
  else:
64
- severity, confidence, explanation = prediction
65
 
66
- # Format output
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 professional medical interface
76
- with gr.Blocks(title="Clinical Drug Interaction Checker", theme=gr.themes.Soft()) as demo:
77
- gr.Markdown("# 🏥 Clinical Drug Interaction Checker")
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
- predict_btn = gr.Button("🔍 Check Interaction", variant="primary")
93
-
94
- with gr.Row():
95
- with gr.Column():
96
- gr.Markdown("### 🚨 Interaction Severity")
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
- # Comprehensive examples covering all severity levels
104
- gr.Markdown("### 💡 Common Clinical Examples")
105
 
106
- with gr.Row():
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
- # Medical guidance
132
- gr.Markdown("""
133
- ### 📋 Severity Guidance:
134
- - **Severe**: Life-threatening - Immediate medical attention required
135
- - **Moderate**: Requires monitoring and possible dose adjustment
136
- - **Mild**: Minimal clinical significance
137
- - **No Interaction**: Generally safe to co-administer
138
- """)
 
 
139
 
140
  predict_btn.click(
141
  predict_interaction,
142
- [drug1, drug2],
143
- [severity_output, confidence_output]
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__":