Update app.py
Browse files
app.py
CHANGED
@@ -98,11 +98,9 @@ def recommend_medicine(api_key, medicine_name, csv_file=None):
|
|
98 |
|
99 |
# Create system prompt with dataset context
|
100 |
system_prompt = f"""You are a pharmaceutical expert system that recommends alternative medicines based on a comprehensive medicine dataset. The user has provided the medicine name "{medicine_name}".
|
101 |
-
|
102 |
DATASET INFORMATION:
|
103 |
{dataset_overview}
|
104 |
{medicine_sample}
|
105 |
-
|
106 |
The dataset has the following columns:
|
107 |
- name: Medicine name
|
108 |
- substitute0 through substitute4: Potential substitute medicines
|
@@ -112,23 +110,17 @@ The dataset has the following columns:
|
|
112 |
- Habit Forming: Whether the medicine is habit-forming
|
113 |
- Therapeutic Class: The therapeutic classification
|
114 |
- Action Class: How the medicine works
|
115 |
-
|
116 |
YOUR TASK:
|
117 |
-
|
118 |
{"The medicine was found in the dataset with the following information:" if medicine_exists else "The medicine was NOT found in the dataset with an exact match. Your task is to:"}
|
119 |
-
|
120 |
{medicine_info_str if medicine_exists else "1. Identify what kind of medicine this likely is based on its name (e.g., antibiotics, pain relievers, etc.)"}
|
121 |
{'' if medicine_exists else "2. Look for medicines in the sample list that might be similar or serve similar purposes"}
|
122 |
-
|
123 |
Please recommend alternative medicines for "{medicine_name}" with the following details for each:
|
124 |
1. Name of the alternative medicine
|
125 |
2. Why it's a good alternative (similar chemical composition, therapeutic use, etc.)
|
126 |
3. Potential side effects to be aware of
|
127 |
4. Usage recommendations
|
128 |
5. Similarity to the original medicine (high, medium, low)
|
129 |
-
|
130 |
Include at least 3-5 alternatives if possible.
|
131 |
-
|
132 |
IMPORTANT:
|
133 |
- If the medicine name contains strength or formulation (like "500mg" or "Duo"), focus on finding the base medicine first
|
134 |
- Explain why these alternatives might be suitable replacements
|
@@ -216,15 +208,12 @@ def analyze_full_prescription(api_key, medicine_names, csv_file):
|
|
216 |
medicines_list = ", ".join(cleaned_medicines)
|
217 |
|
218 |
system_prompt = f"""You are a pharmaceutical expert analyzing a full prescription containing the following medicines: {medicines_list}.
|
219 |
-
|
220 |
Please provide a comprehensive analysis including:
|
221 |
-
|
222 |
1. Purpose: The likely medical condition(s) being treated with this combination of medicines
|
223 |
2. Potential interactions: Any known drug interactions between these medicines
|
224 |
3. Side effects: Common side effects to watch for when taking this combination
|
225 |
4. Recommendations: General advice for the patient taking these medicines
|
226 |
5. Questions for the doctor: Important questions the patient should ask their healthcare provider
|
227 |
-
|
228 |
Base your analysis on pharmacological knowledge about these medicines and their typical uses.
|
229 |
"""
|
230 |
|
@@ -258,51 +247,444 @@ Base your analysis on pharmacological knowledge about these medicines and their
|
|
258 |
except Exception as e:
|
259 |
return f"Error: {str(e)}"
|
260 |
|
261 |
-
#
|
262 |
-
|
263 |
-
|
264 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
265 |
|
266 |
# API key input (shared between tabs)
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
|
|
|
|
|
|
|
|
272 |
|
273 |
# Create a file input for CSV that can be shared between tabs
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
|
|
|
|
|
|
280 |
|
281 |
-
with gr.Tabs():
|
282 |
with gr.Tab("Prescription Medicine Extractor"):
|
283 |
-
gr.
|
284 |
-
|
|
|
|
|
285 |
|
286 |
with gr.Row():
|
287 |
-
with gr.Column():
|
288 |
-
image_input = gr.Image(
|
289 |
-
|
|
|
|
|
|
|
|
|
290 |
|
291 |
-
with gr.Column():
|
292 |
-
extracted_output = gr.Textbox(
|
|
|
|
|
|
|
|
|
293 |
|
294 |
with gr.Row():
|
295 |
with gr.Column(scale=1):
|
296 |
-
|
297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
298 |
|
299 |
with gr.Column(scale=2):
|
300 |
-
output_tabs = gr.Tabs()
|
301 |
with output_tabs:
|
302 |
with gr.Tab("Recommendations"):
|
303 |
-
recommendation_from_extract_output = gr.Markdown()
|
304 |
with gr.Tab("Full Analysis"):
|
305 |
-
full_analysis_output = gr.Markdown()
|
|
|
|
|
306 |
|
307 |
# Connect the buttons to functions
|
308 |
extract_btn.click(
|
@@ -323,69 +705,32 @@ with gr.Blocks(title="Medicine Assistant") as app:
|
|
323 |
outputs=full_analysis_output
|
324 |
)
|
325 |
|
326 |
-
gr.Markdown("""
|
327 |
-
### How to use:
|
328 |
-
1. Enter your Together API key
|
329 |
-
2. Upload a clear image of a prescription
|
330 |
-
3. Click 'Extract Medicines' to see the identified medicines
|
331 |
-
4. Optionally upload a custom medicine dataset CSV
|
332 |
-
5. Choose to:
|
333 |
-
- Get alternatives for the first medicine
|
334 |
-
- Analyze the entire prescription for interactions and information
|
335 |
-
|
336 |
-
### Note:
|
337 |
-
- Your API key is used only for the current session
|
338 |
-
- For best results, ensure the prescription image is clear and readable
|
339 |
-
""")
|
340 |
-
|
341 |
with gr.Tab("Medicine Alternative Recommender"):
|
342 |
-
gr.
|
343 |
-
|
|
|
|
|
344 |
|
345 |
with gr.Row():
|
346 |
with gr.Column():
|
347 |
medicine_name = gr.Textbox(
|
348 |
label="Medicine Name",
|
349 |
-
placeholder="Enter a medicine name (e.g., Augmentin 625 Duo)"
|
|
|
350 |
)
|
351 |
-
submit_btn = gr.Button("Get Recommendations",
|
|
|
352 |
|
353 |
with gr.Column():
|
354 |
-
recommendation_output = gr.Markdown()
|
355 |
|
356 |
submit_btn.click(
|
357 |
recommend_medicine,
|
358 |
inputs=[api_key_input, medicine_name, csv_file_input],
|
359 |
outputs=recommendation_output
|
360 |
)
|
361 |
-
|
362 |
-
gr.Markdown("""
|
363 |
-
## How to use this tool:
|
364 |
-
1. Enter your Together API key (same key used across the application)
|
365 |
-
2. Enter a medicine name - the AI will find it or match similar alternatives
|
366 |
-
3. Click "Get Recommendations" to see alternatives
|
367 |
-
|
368 |
-
### Features:
|
369 |
-
- Even if the exact medicine isn't in the database, the AI will try to find similar alternatives
|
370 |
-
- The system analyzes the medicine name to determine its likely purpose and composition
|
371 |
-
- Recommendations include substitutes, side effects, and usage information
|
372 |
-
""")
|
373 |
-
|
374 |
-
gr.Markdown("""
|
375 |
-
## About This Application
|
376 |
-
|
377 |
-
This Medicine Assistant application combines powerful tools powered by Large Language Models:
|
378 |
-
|
379 |
-
1. **Prescription Medicine Extractor**: Uses computer vision AI to identify medicine names from prescription images
|
380 |
-
2. **Medicine Alternative Recommender**: Provides detailed information about alternative medications
|
381 |
-
3. **Prescription Analyzer**: Analyzes entire prescriptions for potential interactions and insights
|
382 |
-
|
383 |
-
All tools utilize the Together AI platform for advanced AI capabilities. Your API key is not stored and is only used to make API calls during your active session.
|
384 |
-
|
385 |
-
### Important Note
|
386 |
|
387 |
-
|
388 |
-
""")
|
389 |
|
390 |
# Launch the app
|
391 |
if __name__ == "__main__":
|
|
|
98 |
|
99 |
# Create system prompt with dataset context
|
100 |
system_prompt = f"""You are a pharmaceutical expert system that recommends alternative medicines based on a comprehensive medicine dataset. The user has provided the medicine name "{medicine_name}".
|
|
|
101 |
DATASET INFORMATION:
|
102 |
{dataset_overview}
|
103 |
{medicine_sample}
|
|
|
104 |
The dataset has the following columns:
|
105 |
- name: Medicine name
|
106 |
- substitute0 through substitute4: Potential substitute medicines
|
|
|
110 |
- Habit Forming: Whether the medicine is habit-forming
|
111 |
- Therapeutic Class: The therapeutic classification
|
112 |
- Action Class: How the medicine works
|
|
|
113 |
YOUR TASK:
|
|
|
114 |
{"The medicine was found in the dataset with the following information:" if medicine_exists else "The medicine was NOT found in the dataset with an exact match. Your task is to:"}
|
|
|
115 |
{medicine_info_str if medicine_exists else "1. Identify what kind of medicine this likely is based on its name (e.g., antibiotics, pain relievers, etc.)"}
|
116 |
{'' if medicine_exists else "2. Look for medicines in the sample list that might be similar or serve similar purposes"}
|
|
|
117 |
Please recommend alternative medicines for "{medicine_name}" with the following details for each:
|
118 |
1. Name of the alternative medicine
|
119 |
2. Why it's a good alternative (similar chemical composition, therapeutic use, etc.)
|
120 |
3. Potential side effects to be aware of
|
121 |
4. Usage recommendations
|
122 |
5. Similarity to the original medicine (high, medium, low)
|
|
|
123 |
Include at least 3-5 alternatives if possible.
|
|
|
124 |
IMPORTANT:
|
125 |
- If the medicine name contains strength or formulation (like "500mg" or "Duo"), focus on finding the base medicine first
|
126 |
- Explain why these alternatives might be suitable replacements
|
|
|
208 |
medicines_list = ", ".join(cleaned_medicines)
|
209 |
|
210 |
system_prompt = f"""You are a pharmaceutical expert analyzing a full prescription containing the following medicines: {medicines_list}.
|
|
|
211 |
Please provide a comprehensive analysis including:
|
|
|
212 |
1. Purpose: The likely medical condition(s) being treated with this combination of medicines
|
213 |
2. Potential interactions: Any known drug interactions between these medicines
|
214 |
3. Side effects: Common side effects to watch for when taking this combination
|
215 |
4. Recommendations: General advice for the patient taking these medicines
|
216 |
5. Questions for the doctor: Important questions the patient should ask their healthcare provider
|
|
|
217 |
Base your analysis on pharmacological knowledge about these medicines and their typical uses.
|
218 |
"""
|
219 |
|
|
|
247 |
except Exception as e:
|
248 |
return f"Error: {str(e)}"
|
249 |
|
250 |
+
# Custom CSS for styling the application
|
251 |
+
custom_css = """
|
252 |
+
:root {
|
253 |
+
--primary-color: #3498db;
|
254 |
+
--secondary-color: #2ecc71;
|
255 |
+
--accent-color: #e74c3c;
|
256 |
+
--background-color: #f9f9f9;
|
257 |
+
--card-bg: #ffffff;
|
258 |
+
--text-color: #333333;
|
259 |
+
--light-text: #777777;
|
260 |
+
--border-radius: 10px;
|
261 |
+
--shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
262 |
+
--hover-shadow: 0 10px 15px rgba(0, 0, 0, 0.15);
|
263 |
+
}
|
264 |
+
|
265 |
+
body {
|
266 |
+
font-family: 'Roboto', sans-serif;
|
267 |
+
background-color: var(--background-color);
|
268 |
+
color: var(--text-color);
|
269 |
+
}
|
270 |
+
|
271 |
+
/* Header Styling */
|
272 |
+
.app-header {
|
273 |
+
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
|
274 |
+
padding: 20px;
|
275 |
+
border-radius: var(--border-radius);
|
276 |
+
color: white;
|
277 |
+
margin-bottom: 20px;
|
278 |
+
box-shadow: var(--shadow);
|
279 |
+
text-align: center;
|
280 |
+
}
|
281 |
+
|
282 |
+
.app-header h1 {
|
283 |
+
margin: 0;
|
284 |
+
font-size: 2.5rem;
|
285 |
+
font-weight: 700;
|
286 |
+
}
|
287 |
+
|
288 |
+
.app-header p {
|
289 |
+
margin: 10px 0 0;
|
290 |
+
font-size: 1.1rem;
|
291 |
+
opacity: 0.9;
|
292 |
+
}
|
293 |
+
|
294 |
+
/* General Card Styling */
|
295 |
+
.card {
|
296 |
+
background-color: var(--card-bg);
|
297 |
+
border-radius: var(--border-radius);
|
298 |
+
padding: 20px;
|
299 |
+
margin-bottom: 20px;
|
300 |
+
box-shadow: var(--shadow);
|
301 |
+
transition: box-shadow 0.3s ease;
|
302 |
+
}
|
303 |
+
|
304 |
+
.card:hover {
|
305 |
+
box-shadow: var(--hover-shadow);
|
306 |
+
}
|
307 |
+
|
308 |
+
.card-header {
|
309 |
+
border-bottom: 1px solid #eee;
|
310 |
+
padding-bottom: 10px;
|
311 |
+
margin-bottom: 15px;
|
312 |
+
font-weight: 600;
|
313 |
+
color: var(--primary-color);
|
314 |
+
}
|
315 |
+
|
316 |
+
/* Button Styling */
|
317 |
+
.primary-btn button {
|
318 |
+
background: linear-gradient(to right, var(--primary-color), #2980b9) !important;
|
319 |
+
border: none !important;
|
320 |
+
color: white !important;
|
321 |
+
padding: 10px 20px !important;
|
322 |
+
border-radius: var(--border-radius) !important;
|
323 |
+
font-weight: 600 !important;
|
324 |
+
transition: transform 0.2s, box-shadow 0.2s !important;
|
325 |
+
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1) !important;
|
326 |
+
}
|
327 |
+
|
328 |
+
.primary-btn button:hover {
|
329 |
+
transform: translateY(-2px) !important;
|
330 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15) !important;
|
331 |
+
}
|
332 |
+
|
333 |
+
.secondary-btn button {
|
334 |
+
background: linear-gradient(to right, var(--secondary-color), #27ae60) !important;
|
335 |
+
border: none !important;
|
336 |
+
color: white !important;
|
337 |
+
padding: 10px 20px !important;
|
338 |
+
border-radius: var(--border-radius) !important;
|
339 |
+
font-weight: 600 !important;
|
340 |
+
transition: transform 0.2s, box-shadow 0.2s !important;
|
341 |
+
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1) !important;
|
342 |
+
}
|
343 |
+
|
344 |
+
.secondary-btn button:hover {
|
345 |
+
transform: translateY(-2px) !important;
|
346 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15) !important;
|
347 |
+
}
|
348 |
+
|
349 |
+
.accent-btn button {
|
350 |
+
background: linear-gradient(to right, var(--accent-color), #c0392b) !important;
|
351 |
+
border: none !important;
|
352 |
+
color: white !important;
|
353 |
+
padding: 10px 20px !important;
|
354 |
+
border-radius: var(--border-radius) !important;
|
355 |
+
font-weight: 600 !important;
|
356 |
+
transition: transform 0.2s, box-shadow 0.2s !important;
|
357 |
+
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1) !important;
|
358 |
+
}
|
359 |
+
|
360 |
+
.accent-btn button:hover {
|
361 |
+
transform: translateY(-2px) !important;
|
362 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15) !important;
|
363 |
+
}
|
364 |
+
|
365 |
+
/* Input Styling */
|
366 |
+
.custom-input input, .custom-input textarea {
|
367 |
+
border-radius: var(--border-radius) !important;
|
368 |
+
border: 1px solid #ddd !important;
|
369 |
+
padding: 12px !important;
|
370 |
+
transition: border-color 0.3s, box-shadow 0.3s !important;
|
371 |
+
}
|
372 |
+
|
373 |
+
.custom-input input:focus, .custom-input textarea:focus {
|
374 |
+
border-color: var(--primary-color) !important;
|
375 |
+
box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2) !important;
|
376 |
+
}
|
377 |
+
|
378 |
+
/* Tab Styling */
|
379 |
+
.custom-tabs .tabs {
|
380 |
+
border-bottom: 2px solid #eee !important;
|
381 |
+
}
|
382 |
+
|
383 |
+
.custom-tabs .tab-nav button {
|
384 |
+
border: none !important;
|
385 |
+
background: transparent !important;
|
386 |
+
padding: 10px 20px !important;
|
387 |
+
margin: 0 !important;
|
388 |
+
color: var(--light-text) !important;
|
389 |
+
font-weight: 600 !important;
|
390 |
+
transition: color 0.3s !important;
|
391 |
+
}
|
392 |
+
|
393 |
+
.custom-tabs .tab-nav button[data-selected="true"] {
|
394 |
+
color: var(--primary-color) !important;
|
395 |
+
border-bottom: 3px solid var(--primary-color) !important;
|
396 |
+
}
|
397 |
+
|
398 |
+
/* File Upload Styling */
|
399 |
+
.custom-file-upload {
|
400 |
+
border: 2px dashed #ddd !important;
|
401 |
+
border-radius: var(--border-radius) !important;
|
402 |
+
padding: 20px !important;
|
403 |
+
text-align: center !important;
|
404 |
+
transition: border-color 0.3s !important;
|
405 |
+
}
|
406 |
+
|
407 |
+
.custom-file-upload:hover {
|
408 |
+
border-color: var(--primary-color) !important;
|
409 |
+
}
|
410 |
+
|
411 |
+
/* Output Styling */
|
412 |
+
.output-area {
|
413 |
+
background-color: #f5f7fa !important;
|
414 |
+
border-radius: var(--border-radius) !important;
|
415 |
+
padding: 15px !important;
|
416 |
+
border-left: 4px solid var(--primary-color) !important;
|
417 |
+
}
|
418 |
+
|
419 |
+
/* Info Box Styling */
|
420 |
+
.info-box {
|
421 |
+
background-color: rgba(52, 152, 219, 0.1) !important;
|
422 |
+
border-left: 4px solid var(--primary-color) !important;
|
423 |
+
padding: 15px !important;
|
424 |
+
border-radius: var(--border-radius) !important;
|
425 |
+
margin: 20px 0 !important;
|
426 |
+
}
|
427 |
+
|
428 |
+
/* Animation for loading state */
|
429 |
+
@keyframes pulse {
|
430 |
+
0% { opacity: 1; }
|
431 |
+
50% { opacity: 0.6; }
|
432 |
+
100% { opacity: 1; }
|
433 |
+
}
|
434 |
+
|
435 |
+
.loading {
|
436 |
+
animation: pulse 1.5s infinite ease-in-out;
|
437 |
+
}
|
438 |
+
|
439 |
+
/* Responsive improvements */
|
440 |
+
@media (max-width: 768px) {
|
441 |
+
.app-header h1 {
|
442 |
+
font-size: 2rem;
|
443 |
+
}
|
444 |
+
|
445 |
+
.card {
|
446 |
+
padding: 15px;
|
447 |
+
}
|
448 |
+
}
|
449 |
+
|
450 |
+
/* Custom markdown styling */
|
451 |
+
.custom-markdown h1, .custom-markdown h2, .custom-markdown h3 {
|
452 |
+
color: var(--primary-color);
|
453 |
+
}
|
454 |
+
|
455 |
+
.custom-markdown a {
|
456 |
+
color: var(--secondary-color);
|
457 |
+
text-decoration: none;
|
458 |
+
}
|
459 |
+
|
460 |
+
.custom-markdown a:hover {
|
461 |
+
text-decoration: underline;
|
462 |
+
}
|
463 |
+
|
464 |
+
.custom-markdown blockquote {
|
465 |
+
border-left: 4px solid var(--secondary-color);
|
466 |
+
padding-left: 15px;
|
467 |
+
color: var(--light-text);
|
468 |
+
}
|
469 |
+
|
470 |
+
.custom-markdown code {
|
471 |
+
background-color: #f0f0f0;
|
472 |
+
padding: 2px 4px;
|
473 |
+
border-radius: 4px;
|
474 |
+
}
|
475 |
+
|
476 |
+
/* Disclaimer styling */
|
477 |
+
.disclaimer {
|
478 |
+
background-color: rgba(231, 76, 60, 0.1);
|
479 |
+
border-left: 4px solid var(--accent-color);
|
480 |
+
padding: 10px 15px;
|
481 |
+
border-radius: var(--border-radius);
|
482 |
+
margin-top: 20px;
|
483 |
+
font-size: 0.9rem;
|
484 |
+
}
|
485 |
+
|
486 |
+
/* Icon styling */
|
487 |
+
.icon {
|
488 |
+
vertical-align: middle;
|
489 |
+
margin-right: 8px;
|
490 |
+
}
|
491 |
+
|
492 |
+
/* Feature box styling */
|
493 |
+
.feature-box {
|
494 |
+
display: flex;
|
495 |
+
background-color: var(--card-bg);
|
496 |
+
border-radius: var(--border-radius);
|
497 |
+
padding: 15px;
|
498 |
+
margin: 10px 0;
|
499 |
+
box-shadow: var(--shadow);
|
500 |
+
}
|
501 |
+
|
502 |
+
.feature-icon {
|
503 |
+
width: 50px;
|
504 |
+
height: 50px;
|
505 |
+
background-color: rgba(52, 152, 219, 0.2);
|
506 |
+
border-radius: 50%;
|
507 |
+
display: flex;
|
508 |
+
align-items: center;
|
509 |
+
justify-content: center;
|
510 |
+
margin-right: 15px;
|
511 |
+
}
|
512 |
+
|
513 |
+
.feature-content {
|
514 |
+
flex: 1;
|
515 |
+
}
|
516 |
+
|
517 |
+
.feature-content h3 {
|
518 |
+
margin-top: 0;
|
519 |
+
color: var(--primary-color);
|
520 |
+
}
|
521 |
+
|
522 |
+
/* API key styling */
|
523 |
+
.api-key-box {
|
524 |
+
background-color: rgba(46, 204, 113, 0.1);
|
525 |
+
border-radius: var(--border-radius);
|
526 |
+
padding: 15px;
|
527 |
+
margin-bottom: 20px;
|
528 |
+
border: 1px solid rgba(46, 204, 113, 0.3);
|
529 |
+
}
|
530 |
+
"""
|
531 |
+
|
532 |
+
# Custom HTML components
|
533 |
+
html_header = """
|
534 |
+
<div class="app-header">
|
535 |
+
<h1>💊 Medicine Assistant</h1>
|
536 |
+
<p>AI-powered tools for medicine analysis, alternatives, and prescription insights</p>
|
537 |
+
</div>
|
538 |
+
"""
|
539 |
+
|
540 |
+
html_api_key_section = """
|
541 |
+
<div class="api-key-box">
|
542 |
+
<h3>🔑 API Access</h3>
|
543 |
+
<p>Your Together API key is securely used for this session only and is not stored.</p>
|
544 |
+
</div>
|
545 |
+
"""
|
546 |
+
|
547 |
+
html_about_section = """
|
548 |
+
<div class="card">
|
549 |
+
<div class="card-header">About Medicine Assistant</div>
|
550 |
+
<p>This advanced application combines AI-powered tools to help you understand your prescriptions better:</p>
|
551 |
+
|
552 |
+
<div class="feature-box">
|
553 |
+
<div class="feature-icon">📋</div>
|
554 |
+
<div class="feature-content">
|
555 |
+
<h3>Prescription Extraction</h3>
|
556 |
+
<p>Upload an image of your prescription and the AI will identify medicine names using advanced computer vision.</p>
|
557 |
+
</div>
|
558 |
+
</div>
|
559 |
+
|
560 |
+
<div class="feature-box">
|
561 |
+
<div class="feature-icon">🔄</div>
|
562 |
+
<div class="feature-content">
|
563 |
+
<h3>Alternative Medicine Finder</h3>
|
564 |
+
<p>Get detailed information about alternative medications that might serve similar purposes.</p>
|
565 |
+
</div>
|
566 |
+
</div>
|
567 |
+
|
568 |
+
<div class="feature-box">
|
569 |
+
<div class="feature-icon">🔍</div>
|
570 |
+
<div class="feature-content">
|
571 |
+
<h3>Prescription Analysis</h3>
|
572 |
+
<p>Analyze entire prescriptions for potential interactions, uses, and important information.</p>
|
573 |
+
</div>
|
574 |
+
</div>
|
575 |
+
|
576 |
+
<div class="disclaimer">
|
577 |
+
<strong>Important:</strong> This application is for informational purposes only. Always consult with a healthcare professional before making any changes to your medication regimen.
|
578 |
+
</div>
|
579 |
+
</div>
|
580 |
+
"""
|
581 |
+
|
582 |
+
html_how_to_use_extractor = """
|
583 |
+
<div class="info-box">
|
584 |
+
<h3>How to use the Prescription Extractor:</h3>
|
585 |
+
<ol>
|
586 |
+
<li>Enter your Together API key above</li>
|
587 |
+
<li>Upload a clear image of your prescription</li>
|
588 |
+
<li>Click "Extract Medicines" to identify medicines in the image</li>
|
589 |
+
<li>After extraction, you can:
|
590 |
+
<ul>
|
591 |
+
<li>Get alternative recommendations for the first medicine</li>
|
592 |
+
<li>Analyze all medicines for potential interactions and insights</li>
|
593 |
+
</ul>
|
594 |
+
</li>
|
595 |
+
</ol>
|
596 |
+
<p><strong>Tip:</strong> For best results, ensure your prescription image is clear, well-lit, and shows all medicine names clearly.</p>
|
597 |
+
</div>
|
598 |
+
"""
|
599 |
+
|
600 |
+
html_how_to_use_recommender = """
|
601 |
+
<div class="info-box">
|
602 |
+
<h3>How to use the Alternative Recommender:</h3>
|
603 |
+
<ol>
|
604 |
+
<li>Enter your Together API key (same key used across the application)</li>
|
605 |
+
<li>Type the name of a medicine you'd like to find alternatives for</li>
|
606 |
+
<li>Click "Get Recommendations" to see detailed information about possible alternatives</li>
|
607 |
+
</ol>
|
608 |
+
<p><strong>Features:</strong></p>
|
609 |
+
<ul>
|
610 |
+
<li>Finds alternatives even if the exact medicine isn't in the database</li>
|
611 |
+
<li>Analyzes medicine names to determine likely purpose and composition</li>
|
612 |
+
<li>Provides detailed information about substitutes, side effects, and usage</li>
|
613 |
+
</ul>
|
614 |
+
</div>
|
615 |
+
"""
|
616 |
+
|
617 |
+
# Create Gradio interface with tabs for all functionalities and custom styling
|
618 |
+
with gr.Blocks(css=custom_css, title="Medicine Assistant") as app:
|
619 |
+
gr.HTML(html_header)
|
620 |
|
621 |
# API key input (shared between tabs)
|
622 |
+
with gr.Row():
|
623 |
+
with gr.Column():
|
624 |
+
gr.HTML(html_api_key_section)
|
625 |
+
api_key_input = gr.Textbox(
|
626 |
+
label="Together API Key",
|
627 |
+
placeholder="Enter your Together API key here...",
|
628 |
+
type="password",
|
629 |
+
elem_classes=["custom-input"]
|
630 |
+
)
|
631 |
|
632 |
# Create a file input for CSV that can be shared between tabs
|
633 |
+
with gr.Row():
|
634 |
+
with gr.Column():
|
635 |
+
csv_file_input = gr.File(
|
636 |
+
label="Upload Medicine CSV (Optional)",
|
637 |
+
file_types=[".csv"],
|
638 |
+
type="filepath",
|
639 |
+
elem_classes=["custom-file-upload"]
|
640 |
+
)
|
641 |
+
gr.Markdown("If no CSV is uploaded, the app will use the default 'medicine_dataset.csv' file.")
|
642 |
|
643 |
+
with gr.Tabs(elem_classes=["custom-tabs"]) as tabs:
|
644 |
with gr.Tab("Prescription Medicine Extractor"):
|
645 |
+
gr.HTML("""<div class="card">
|
646 |
+
<div class="card-header">Prescription Medicine Extractor</div>
|
647 |
+
<p>Upload a prescription image to extract medicine names using Together AI's Llama-Vision-Free model.</p>
|
648 |
+
</div>""")
|
649 |
|
650 |
with gr.Row():
|
651 |
+
with gr.Column(scale=1):
|
652 |
+
image_input = gr.Image(
|
653 |
+
type="filepath",
|
654 |
+
label="Upload Prescription Image",
|
655 |
+
elem_classes=["custom-file-upload"]
|
656 |
+
)
|
657 |
+
extract_btn = gr.Button("Extract Medicines", elem_classes=["primary-btn"])
|
658 |
|
659 |
+
with gr.Column(scale=2):
|
660 |
+
extracted_output = gr.Textbox(
|
661 |
+
label="Extracted Medicines",
|
662 |
+
lines=10,
|
663 |
+
elem_classes=["custom-input", "output-area"]
|
664 |
+
)
|
665 |
|
666 |
with gr.Row():
|
667 |
with gr.Column(scale=1):
|
668 |
+
with gr.Row():
|
669 |
+
recommend_from_extract_btn = gr.Button(
|
670 |
+
"Get Recommendations for First Medicine",
|
671 |
+
elem_classes=["secondary-btn"]
|
672 |
+
)
|
673 |
+
with gr.Row():
|
674 |
+
analyze_full_btn = gr.Button(
|
675 |
+
"Analyze Full Prescription",
|
676 |
+
elem_classes=["accent-btn"]
|
677 |
+
)
|
678 |
|
679 |
with gr.Column(scale=2):
|
680 |
+
output_tabs = gr.Tabs(elem_classes=["custom-tabs"])
|
681 |
with output_tabs:
|
682 |
with gr.Tab("Recommendations"):
|
683 |
+
recommendation_from_extract_output = gr.Markdown(elem_classes=["custom-markdown"])
|
684 |
with gr.Tab("Full Analysis"):
|
685 |
+
full_analysis_output = gr.Markdown(elem_classes=["custom-markdown"])
|
686 |
+
|
687 |
+
gr.HTML(html_how_to_use_extractor)
|
688 |
|
689 |
# Connect the buttons to functions
|
690 |
extract_btn.click(
|
|
|
705 |
outputs=full_analysis_output
|
706 |
)
|
707 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
708 |
with gr.Tab("Medicine Alternative Recommender"):
|
709 |
+
gr.HTML("""<div class="card">
|
710 |
+
<div class="card-header">Medicine Alternative Recommender</div>
|
711 |
+
<p>This tool recommends alternative medicines based on an input medicine name using the Together API.</p>
|
712 |
+
</div>""")
|
713 |
|
714 |
with gr.Row():
|
715 |
with gr.Column():
|
716 |
medicine_name = gr.Textbox(
|
717 |
label="Medicine Name",
|
718 |
+
placeholder="Enter a medicine name (e.g., Augmentin 625 Duo)",
|
719 |
+
elem_classes=["custom-input"]
|
720 |
)
|
721 |
+
submit_btn = gr.Button("Get Recommendations", elem_classes=["primary-btn"])
|
722 |
+
gr.HTML(html_how_to_use_recommender)
|
723 |
|
724 |
with gr.Column():
|
725 |
+
recommendation_output = gr.Markdown(elem_classes=["custom-markdown"])
|
726 |
|
727 |
submit_btn.click(
|
728 |
recommend_medicine,
|
729 |
inputs=[api_key_input, medicine_name, csv_file_input],
|
730 |
outputs=recommendation_output
|
731 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
732 |
|
733 |
+
gr.HTML(html_about_section)
|
|
|
734 |
|
735 |
# Launch the app
|
736 |
if __name__ == "__main__":
|