shukdevdatta123 commited on
Commit
726de45
·
verified ·
1 Parent(s): 227ffe0

Create v2.txt

Browse files
Files changed (1) hide show
  1. v2.txt +381 -0
v2.txt ADDED
@@ -0,0 +1,381 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import numpy as np
4
+ import os
5
+ import base64
6
+ from together import Together
7
+
8
+ def extract_medicines(api_key, image):
9
+ """
10
+ Extract medicine names from a prescription image using Together AI's Llama-Vision-Free model
11
+ """
12
+ # Check if API key is provided
13
+ if not api_key:
14
+ return "Please enter your Together API key."
15
+
16
+ if image is None:
17
+ return "Please upload an image."
18
+
19
+ try:
20
+ # Initialize Together client with the provided API key
21
+ client = Together(api_key=api_key)
22
+
23
+ # Convert image to base64
24
+ with open(image, "rb") as img_file:
25
+ img_data = img_file.read()
26
+ b64_img = base64.b64encode(img_data).decode('utf-8')
27
+
28
+ # Make API call with base64 encoded image
29
+ response = client.chat.completions.create(
30
+ model="meta-llama/Llama-Vision-Free",
31
+ messages=[
32
+ {
33
+ "role": "system",
34
+ "content": "You are an expert in identifying medicine names from prescription images."
35
+ },
36
+ {
37
+ "role": "user",
38
+ "content": [
39
+ {
40
+ "type": "text",
41
+ "text": "Please extract the names of the medicines only."
42
+ },
43
+ {
44
+ "type": "image_url",
45
+ "image_url": {
46
+ "url": f"data:image/jpeg;base64,{b64_img}"
47
+ }
48
+ }
49
+ ]
50
+ }
51
+ ]
52
+ )
53
+
54
+ # Extract medicine names from response
55
+ medicine_list = response.choices[0].message.content
56
+ return medicine_list
57
+
58
+ except Exception as e:
59
+ return f"Error: {str(e)}"
60
+
61
+ def recommend_medicine(api_key, medicine_name, csv_file=None):
62
+ """
63
+ Use Together API to recommend alternative medicines based on input medicine name
64
+ using data from the provided CSV file with specific column structure.
65
+ It will use AI to find similar medicines even if the exact name isn't in the dataset.
66
+ """
67
+ try:
68
+ # If CSV file is provided, use it; otherwise use default
69
+ if csv_file is not None:
70
+ # Read the uploaded CSV
71
+ if isinstance(csv_file, str): # Path to default CSV
72
+ df = pd.read_csv(csv_file)
73
+ else: # Uploaded file
74
+ df = pd.read_csv(csv_file.name)
75
+ else:
76
+ # Use the default medicine_dataset.csv in the current directory
77
+ try:
78
+ df = pd.read_csv("medicine_dataset.csv")
79
+ except FileNotFoundError:
80
+ return "Error: Default medicine_dataset.csv not found. Please upload a CSV file."
81
+
82
+ # Check if medicine is in the dataset
83
+ medicine_exists = medicine_name in df['name'].values
84
+
85
+ # Create a helpful context about the dataset to send to the LLM
86
+ dataset_overview = f"The dataset contains {len(df)} medicines with columns for name, substitutes, side effects, uses, chemical class, etc."
87
+
88
+ # Sample of medicine names to give the model context
89
+ sample_names = df['name'].sample(min(20, len(df))).tolist()
90
+ medicine_sample = f"Sample medicines in the dataset: {', '.join(sample_names)}"
91
+
92
+ # Extract specific medicine data if available
93
+ medicine_data = None
94
+ medicine_info_str = ""
95
+ if medicine_exists:
96
+ medicine_data = df[df['name'] == medicine_name]
97
+ medicine_info_str = medicine_data.to_string(index=False)
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
107
+ - sideEffect0 through sideEffect41: Possible side effects
108
+ - use0 through use4: Medical uses
109
+ - Chemical Class: The chemical classification
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
127
+ - Include appropriate medical disclaimers
128
+ - Format your response clearly with headings for each alternative medicine
129
+ """
130
+
131
+ # Initialize Together client with the API key
132
+ client = Together(api_key=api_key)
133
+
134
+ # Make API call
135
+ response = client.chat.completions.create(
136
+ model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
137
+ messages=[
138
+ {
139
+ "role": "system",
140
+ "content": system_prompt
141
+ },
142
+ {
143
+ "role": "user",
144
+ "content": f"Please recommend alternatives for {medicine_name} based on the available information."
145
+ }
146
+ ],
147
+ max_tokens=2000,
148
+ temperature=0.7 # Slightly higher temperature for creative recommendations
149
+ )
150
+
151
+ # Get the raw response
152
+ recommendation_text = response.choices[0].message.content
153
+
154
+ # Add disclaimer
155
+ final_response = recommendation_text + "\n\n---\n\n**DISCLAIMER:** This information is for educational purposes only. Always consult with a healthcare professional before making any changes to your medication."
156
+
157
+ return final_response
158
+
159
+ except Exception as e:
160
+ return f"Error: {str(e)}"
161
+
162
+ def send_medicine_to_recommender(api_key, medicine_names, csv_file):
163
+ """
164
+ Takes medicine names extracted from prescription and gets recommendations
165
+ """
166
+ if not medicine_names or medicine_names.startswith("Error") or medicine_names.startswith("Please"):
167
+ return "Please extract valid medicine names first"
168
+
169
+ # Extract the first medicine name from the list (assuming it's the first line or first item)
170
+ medicine_lines = medicine_names.strip().split('\n')
171
+ if not medicine_lines:
172
+ return "No valid medicine name found in extraction results"
173
+
174
+ # Get the first medicine name (remove any bullet points or numbers)
175
+ first_medicine = medicine_lines[0]
176
+ # Clean up the medicine name (remove bullets, numbers, etc.)
177
+ first_medicine = first_medicine.lstrip('•-*0123456789. ').strip()
178
+
179
+ # Check if we have a valid medicine name
180
+ if not first_medicine:
181
+ return "Could not identify a valid medicine name from extraction"
182
+
183
+ # Call the recommend medicine function with the first extracted medicine
184
+ return recommend_medicine(api_key, first_medicine, csv_file)
185
+
186
+ def analyze_full_prescription(api_key, medicine_names, csv_file):
187
+ """
188
+ Takes all extracted medicine names and analyzes their interactions and provides comprehensive information
189
+ """
190
+ if not medicine_names or medicine_names.startswith("Error") or medicine_names.startswith("Please"):
191
+ return "Please extract valid medicine names first"
192
+
193
+ try:
194
+ # Parse the medicine names from the extracted text
195
+ medicine_lines = medicine_names.strip().split('\n')
196
+ cleaned_medicines = []
197
+
198
+ # Clean up medicine names (remove bullets, numbers, etc.)
199
+ for medicine in medicine_lines:
200
+ cleaned_medicine = medicine.lstrip('•-*0123456789. ').strip()
201
+ if cleaned_medicine:
202
+ cleaned_medicines.append(cleaned_medicine)
203
+
204
+ if not cleaned_medicines:
205
+ return "No valid medicine names found in extraction"
206
+
207
+ # Create a prompt for the LLM to analyze the full prescription
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
+
220
+ # Initialize Together client with the API key
221
+ client = Together(api_key=api_key)
222
+
223
+ # Make API call
224
+ response = client.chat.completions.create(
225
+ model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
226
+ messages=[
227
+ {
228
+ "role": "system",
229
+ "content": system_prompt
230
+ },
231
+ {
232
+ "role": "user",
233
+ "content": f"Please analyze this prescription with the following medicines: {medicines_list}"
234
+ }
235
+ ],
236
+ max_tokens=2000,
237
+ temperature=0.3 # Lower temperature for more factual responses
238
+ )
239
+
240
+ analysis_text = response.choices[0].message.content
241
+
242
+ # Add disclaimer
243
+ final_response = analysis_text + "\n\n---\n\n**DISCLAIMER:** This analysis is for informational purposes only and should not replace professional medical advice. Always consult with your healthcare provider about your prescription."
244
+
245
+ return final_response
246
+
247
+ except Exception as e:
248
+ return f"Error: {str(e)}"
249
+
250
+ # Create Gradio interface with tabs for all functionalities
251
+ with gr.Blocks(title="Medicine Assistant") as app:
252
+ gr.Markdown("# Medicine Assistant")
253
+ gr.Markdown("This application helps you extract medicine names from prescriptions, find alternative medicines, and analyze full prescriptions.")
254
+
255
+ # API key input (shared between tabs)
256
+ api_key_input = gr.Textbox(
257
+ label="Together API Key",
258
+ placeholder="Enter your Together API key here...",
259
+ type="password"
260
+ )
261
+
262
+ # Create a file input for CSV that can be shared between tabs
263
+ csv_file_input = gr.File(
264
+ label="Upload Medicine CSV (Optional)",
265
+ file_types=[".csv"],
266
+ type="filepath"
267
+ )
268
+ gr.Markdown("If no CSV is uploaded, the app will use the default 'medicine_dataset.csv' file.")
269
+
270
+ with gr.Tabs():
271
+ with gr.Tab("Prescription Medicine Extractor"):
272
+ gr.Markdown("## Prescription Medicine Extractor")
273
+ gr.Markdown("Upload a prescription image to extract medicine names using Together AI's Llama-Vision-Free model.")
274
+
275
+ with gr.Row():
276
+ with gr.Column():
277
+ image_input = gr.Image(type="filepath", label="Upload Prescription Image")
278
+ extract_btn = gr.Button("Extract Medicines")
279
+
280
+ with gr.Column():
281
+ extracted_output = gr.Textbox(label="Extracted Medicines", lines=10)
282
+
283
+ with gr.Row():
284
+ with gr.Column(scale=1):
285
+ recommend_from_extract_btn = gr.Button("Get Recommendations for First Medicine", variant="primary")
286
+ analyze_full_btn = gr.Button("Analyze Full Prescription", variant="secondary")
287
+
288
+ with gr.Column(scale=2):
289
+ output_tabs = gr.Tabs()
290
+ with output_tabs:
291
+ with gr.Tab("Recommendations"):
292
+ recommendation_from_extract_output = gr.Markdown()
293
+ with gr.Tab("Full Analysis"):
294
+ full_analysis_output = gr.Markdown()
295
+
296
+ # Connect the buttons to functions
297
+ extract_btn.click(
298
+ fn=extract_medicines,
299
+ inputs=[api_key_input, image_input],
300
+ outputs=extracted_output
301
+ )
302
+
303
+ recommend_from_extract_btn.click(
304
+ fn=send_medicine_to_recommender,
305
+ inputs=[api_key_input, extracted_output, csv_file_input],
306
+ outputs=recommendation_from_extract_output
307
+ )
308
+
309
+ analyze_full_btn.click(
310
+ fn=analyze_full_prescription,
311
+ inputs=[api_key_input, extracted_output, csv_file_input],
312
+ outputs=full_analysis_output
313
+ )
314
+
315
+ gr.Markdown("""
316
+ ### How to use:
317
+ 1. Enter your Together API key
318
+ 2. Upload a clear image of a prescription
319
+ 3. Click 'Extract Medicines' to see the identified medicines
320
+ 4. Optionally upload a custom medicine dataset CSV
321
+ 5. Choose to:
322
+ - Get alternatives for the first medicine
323
+ - Analyze the entire prescription for interactions and information
324
+
325
+ ### Note:
326
+ - Your API key is used only for the current session
327
+ - For best results, ensure the prescription image is clear and readable
328
+ """)
329
+
330
+ with gr.Tab("Medicine Alternative Recommender"):
331
+ gr.Markdown("## Medicine Alternative Recommender")
332
+ gr.Markdown("This tool recommends alternative medicines based on an input medicine name using the Together API.")
333
+
334
+ with gr.Row():
335
+ with gr.Column():
336
+ medicine_name = gr.Textbox(
337
+ label="Medicine Name",
338
+ placeholder="Enter a medicine name (e.g., Augmentin 625 Duo)"
339
+ )
340
+ submit_btn = gr.Button("Get Recommendations", variant="primary")
341
+
342
+ with gr.Column():
343
+ recommendation_output = gr.Markdown()
344
+
345
+ submit_btn.click(
346
+ recommend_medicine,
347
+ inputs=[api_key_input, medicine_name, csv_file_input],
348
+ outputs=recommendation_output
349
+ )
350
+
351
+ gr.Markdown("""
352
+ ## How to use this tool:
353
+ 1. Enter your Together API key (same key used across the application)
354
+ 2. Enter a medicine name - the AI will find it or match similar alternatives
355
+ 3. Click "Get Recommendations" to see alternatives
356
+
357
+ ### Features:
358
+ - Even if the exact medicine isn't in the database, the AI will try to find similar alternatives
359
+ - The system analyzes the medicine name to determine its likely purpose and composition
360
+ - Recommendations include substitutes, side effects, and usage information
361
+ """)
362
+
363
+ gr.Markdown("""
364
+ ## About This Application
365
+
366
+ This Medicine Assistant application combines powerful tools powered by Large Language Models:
367
+
368
+ 1. **Prescription Medicine Extractor**: Uses computer vision AI to identify medicine names from prescription images
369
+ 2. **Medicine Alternative Recommender**: Provides detailed information about alternative medications
370
+ 3. **Prescription Analyzer**: Analyzes entire prescriptions for potential interactions and insights
371
+
372
+ 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.
373
+
374
+ ### Important Note
375
+
376
+ This application is for informational purposes only. Always consult with a healthcare professional before making any changes to your medication regimen.
377
+ """)
378
+
379
+ # Launch the app
380
+ if __name__ == "__main__":
381
+ app.launch()