Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from mistralai import Mistral
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
from datetime import datetime
|
5 |
+
api_key = os.environ["MISTRAL_API_KEY"]
|
6 |
+
model = "mistral-large-latest"
|
7 |
+
client = Mistral(api_key=api_key)
|
8 |
+
def interaction_check(prompt):
|
9 |
+
"""
|
10 |
+
Check for drug interactions and provide pharmacist guidance
|
11 |
+
"""
|
12 |
+
try:
|
13 |
+
# Input validation
|
14 |
+
if not prompt or prompt.strip() == "":
|
15 |
+
return "⚠️ Please enter your medications or questions about drug interactions."
|
16 |
+
|
17 |
+
if len(prompt.strip()) < 5:
|
18 |
+
return "⚠️ Please provide more details about your medications for a proper assessment."
|
19 |
+
|
20 |
+
# Enhanced system prompt with better instructions
|
21 |
+
system_prompt = f"""You are a knowledgeable pharmacist assistant specializing in drug interaction detection and medication safety.
|
22 |
+
|
23 |
+
Your responsibilities:
|
24 |
+
1. Analyze potential drug interactions from the user's medication list
|
25 |
+
2. Identify contraindications and warnings
|
26 |
+
3. Provide clear, actionable advice
|
27 |
+
4. Always recommend consulting with a licensed pharmacist or healthcare provider
|
28 |
+
5. Use appropriate medical terminology while remaining accessible
|
29 |
+
6. Structure your response clearly with sections if multiple interactions are found
|
30 |
+
|
31 |
+
Important guidelines:
|
32 |
+
- Always emphasize that this is preliminary guidance only
|
33 |
+
- Recommend professional consultation for all medication concerns
|
34 |
+
- Be specific about interaction severity (minor, moderate, major)
|
35 |
+
- Include timing recommendations when relevant
|
36 |
+
- Mention food interactions if applicable
|
37 |
+
- Note any special monitoring requirements
|
38 |
+
|
39 |
+
Format your response with clear sections and use emojis for visual clarity where appropriate."""
|
40 |
+
|
41 |
+
chat_response = client.chat.complete(
|
42 |
+
model=model,
|
43 |
+
messages=[
|
44 |
+
{
|
45 |
+
"role": "system",
|
46 |
+
"content": system_prompt
|
47 |
+
},
|
48 |
+
{
|
49 |
+
"role": "user",
|
50 |
+
"content": f"Please analyze the following for drug interactions: {prompt}",
|
51 |
+
},
|
52 |
+
],
|
53 |
+
temperature=0.1, # Lower temperature for more consistent medical advice
|
54 |
+
max_tokens=1000 # Ensure comprehensive responses
|
55 |
+
)
|
56 |
+
|
57 |
+
response = chat_response.choices[0].message.content
|
58 |
+
|
59 |
+
# Add disclaimer footer
|
60 |
+
disclaimer = "\n\n---\n⚠️ **IMPORTANT DISCLAIMER**: This information is for educational purposes only and should not replace professional medical advice. Always consult with a licensed pharmacist or healthcare provider before making any changes to your medications."
|
61 |
+
|
62 |
+
return response + disclaimer
|
63 |
+
|
64 |
+
except Exception as e:
|
65 |
+
error_msg = f"❌ **Error occurred**: {str(e)}\n\nPlease try again or contact support if the issue persists."
|
66 |
+
return error_msg
|
67 |
+
|
68 |
+
def clear_inputs():
|
69 |
+
"""Clear the input field"""
|
70 |
+
return ""
|
71 |
+
|
72 |
+
def get_example_interactions():
|
73 |
+
"""Return example drug interaction queries"""
|
74 |
+
examples = [
|
75 |
+
"I'm taking warfarin and my doctor prescribed ibuprofen for pain. Are there any interactions?",
|
76 |
+
"Can I take omeprazole with clopidogrel? I have both prescribed.",
|
77 |
+
"I'm on metformin for diabetes and want to know if it's safe with alcohol",
|
78 |
+
"Taking lisinopril and potassium supplements - any concerns?",
|
79 |
+
"I have simvastatin and clarithromycin prescribed together"
|
80 |
+
]
|
81 |
+
return examples
|
82 |
+
|
83 |
+
# Custom CSS for better styling
|
84 |
+
custom_css = """
|
85 |
+
#warning {
|
86 |
+
background-color: #667eea;
|
87 |
+
border: 1px solid #ffeaa7;
|
88 |
+
border-radius: 8px;
|
89 |
+
padding: 16px;
|
90 |
+
margin: 10px 0;
|
91 |
+
}
|
92 |
+
|
93 |
+
#main-container {
|
94 |
+
max-width: 900px;
|
95 |
+
margin: 0 auto;
|
96 |
+
}
|
97 |
+
|
98 |
+
.gradio-container {
|
99 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
100 |
+
}
|
101 |
+
|
102 |
+
#submit-btn {
|
103 |
+
background: linear-gradient(45deg, #667eea 0%, #764ba2 100%);
|
104 |
+
border: none;
|
105 |
+
color: white;
|
106 |
+
font-weight: bold;
|
107 |
+
}
|
108 |
+
|
109 |
+
#clear-btn {
|
110 |
+
background-color: #6c757d;
|
111 |
+
border: none;
|
112 |
+
color: white;
|
113 |
+
}
|
114 |
+
"""
|
115 |
+
|
116 |
+
# Create the Gradio interface
|
117 |
+
with gr.Blocks(css=custom_css, title="Drug Interaction Assistant") as demo:
|
118 |
+
|
119 |
+
with gr.Column(elem_id="main-container"):
|
120 |
+
# Header
|
121 |
+
gr.Markdown(
|
122 |
+
"""
|
123 |
+
# 💊 Drug Interaction Assistant
|
124 |
+
|
125 |
+
Get preliminary guidance on potential drug interactions and medication safety concerns.
|
126 |
+
This tool helps identify possible interactions between medications.
|
127 |
+
""",
|
128 |
+
elem_id="header"
|
129 |
+
)
|
130 |
+
|
131 |
+
# Warning notice
|
132 |
+
gr.Markdown(
|
133 |
+
"""
|
134 |
+
⚠️ **MEDICAL DISCLAIMER**: This tool provides educational information only.
|
135 |
+
Always consult with a licensed pharmacist, doctor, or healthcare provider
|
136 |
+
before making any decisions about your medications.
|
137 |
+
""",
|
138 |
+
elem_id="warning"
|
139 |
+
)
|
140 |
+
|
141 |
+
with gr.Row():
|
142 |
+
with gr.Column(scale=2):
|
143 |
+
# Input section
|
144 |
+
gr.Markdown("### Enter Your Medications or Questions")
|
145 |
+
|
146 |
+
medication_input = gr.Textbox(
|
147 |
+
label="Medications & Questions",
|
148 |
+
placeholder="Example: I'm taking warfarin and ibuprofen. Are there any interactions I should know about?",
|
149 |
+
lines=4,
|
150 |
+
max_lines=8
|
151 |
+
)
|
152 |
+
|
153 |
+
with gr.Row():
|
154 |
+
submit_btn = gr.Button("🔍 Check Interactions", variant="primary", elem_id="submit-btn")
|
155 |
+
clear_btn = gr.Button("🗑️ Clear", elem_id="clear-btn")
|
156 |
+
|
157 |
+
with gr.Column(scale=1):
|
158 |
+
# Examples section
|
159 |
+
gr.Markdown("### Example Questions")
|
160 |
+
example_queries = get_example_interactions()
|
161 |
+
for i, example in enumerate(example_queries[:3]): # Show first 3 examples
|
162 |
+
gr.Markdown(f"**Example {i+1}:** {example}")
|
163 |
+
|
164 |
+
# Output section
|
165 |
+
gr.Markdown("### 📋 Analysis Results")
|
166 |
+
output_display = gr.Markdown(
|
167 |
+
value="Enter your medications above and click 'Check Interactions' to get started.",
|
168 |
+
elem_id="output"
|
169 |
+
)
|
170 |
+
|
171 |
+
# Usage tips
|
172 |
+
with gr.Accordion("💡 Usage Tips", open=False):
|
173 |
+
gr.Markdown(
|
174 |
+
"""
|
175 |
+
**For best results:**
|
176 |
+
- List all medications you're taking (prescription and over-the-counter)
|
177 |
+
- Include dosages if known
|
178 |
+
- Mention any supplements or herbal products
|
179 |
+
- Be specific about your concerns
|
180 |
+
- Include timing of when you take each medication
|
181 |
+
|
182 |
+
**This tool can help with:**
|
183 |
+
- Drug-drug interactions
|
184 |
+
- Food-drug interactions
|
185 |
+
- Timing recommendations
|
186 |
+
- General medication safety concerns
|
187 |
+
|
188 |
+
**Remember:** Always verify with a healthcare professional!
|
189 |
+
"""
|
190 |
+
)
|
191 |
+
|
192 |
+
# Event handlers
|
193 |
+
submit_btn.click(
|
194 |
+
fn=interaction_check,
|
195 |
+
inputs=medication_input,
|
196 |
+
outputs=output_display
|
197 |
+
)
|
198 |
+
|
199 |
+
clear_btn.click(
|
200 |
+
fn=clear_inputs,
|
201 |
+
outputs=medication_input
|
202 |
+
)
|
203 |
+
|
204 |
+
medication_input.submit(
|
205 |
+
fn=interaction_check,
|
206 |
+
inputs=medication_input,
|
207 |
+
outputs=output_display
|
208 |
+
)
|
209 |
+
|
210 |
+
# Add footer with timestamp
|
211 |
+
footer_text = f"Last updated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
|
212 |
+
|
213 |
+
if __name__ == "__main__":
|
214 |
+
demo.launch()
|