Create v1.txt
Browse files
v1.txt
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from together import Together
|
| 3 |
+
import os
|
| 4 |
+
import base64
|
| 5 |
+
|
| 6 |
+
def extract_medicines(api_key, image):
|
| 7 |
+
"""
|
| 8 |
+
Extract medicine names from a prescription image using Together AI's Llama-Vision-Free model
|
| 9 |
+
"""
|
| 10 |
+
# Check if API key is provided
|
| 11 |
+
if not api_key:
|
| 12 |
+
return "Please enter your Together API key."
|
| 13 |
+
|
| 14 |
+
if image is None:
|
| 15 |
+
return "Please upload an image."
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
# Initialize Together client with the provided API key
|
| 19 |
+
client = Together(api_key=api_key)
|
| 20 |
+
|
| 21 |
+
# Convert image to base64
|
| 22 |
+
with open(image, "rb") as img_file:
|
| 23 |
+
img_data = img_file.read()
|
| 24 |
+
b64_img = base64.b64encode(img_data).decode('utf-8')
|
| 25 |
+
|
| 26 |
+
# Make API call with base64 encoded image
|
| 27 |
+
response = client.chat.completions.create(
|
| 28 |
+
model="meta-llama/Llama-Vision-Free",
|
| 29 |
+
messages=[
|
| 30 |
+
{
|
| 31 |
+
"role": "system",
|
| 32 |
+
"content": "You are an expert in identifying medicine names from prescription images."
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"role": "user",
|
| 36 |
+
"content": [
|
| 37 |
+
{
|
| 38 |
+
"type": "text",
|
| 39 |
+
"text": "Please extract the names of the medicines only."
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
"type": "image_url",
|
| 43 |
+
"image_url": {
|
| 44 |
+
"url": f"data:image/jpeg;base64,{b64_img}"
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
]
|
| 48 |
+
}
|
| 49 |
+
]
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Extract medicine names from response
|
| 53 |
+
medicine_list = response.choices[0].message.content
|
| 54 |
+
return medicine_list
|
| 55 |
+
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return f"Error: {str(e)}"
|
| 58 |
+
|
| 59 |
+
# Create Gradio interface
|
| 60 |
+
with gr.Blocks(title="Prescription Medicine Extractor") as app:
|
| 61 |
+
gr.Markdown("## Prescription Medicine Extractor")
|
| 62 |
+
gr.Markdown("Upload a prescription image to extract medicine names using Together AI's Llama-Vision-Free model.")
|
| 63 |
+
|
| 64 |
+
with gr.Row():
|
| 65 |
+
with gr.Column():
|
| 66 |
+
api_key_input = gr.Textbox(
|
| 67 |
+
label="Together API Key",
|
| 68 |
+
placeholder="Enter your Together API key here...",
|
| 69 |
+
type="password"
|
| 70 |
+
)
|
| 71 |
+
image_input = gr.Image(type="filepath", label="Upload Prescription Image")
|
| 72 |
+
submit_btn = gr.Button("Extract Medicines")
|
| 73 |
+
|
| 74 |
+
with gr.Column():
|
| 75 |
+
output = gr.Textbox(label="Extracted Medicines", lines=10)
|
| 76 |
+
|
| 77 |
+
submit_btn.click(
|
| 78 |
+
fn=extract_medicines,
|
| 79 |
+
inputs=[api_key_input, image_input],
|
| 80 |
+
outputs=output
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
gr.Markdown("""
|
| 84 |
+
### How to use:
|
| 85 |
+
1. Enter your Together API key
|
| 86 |
+
2. Upload a clear image of a prescription
|
| 87 |
+
3. Click 'Extract Medicines' to see the results
|
| 88 |
+
|
| 89 |
+
### Note:
|
| 90 |
+
- Your API key is used only for the current session
|
| 91 |
+
- For best results, ensure the prescription image is clear and readable
|
| 92 |
+
""")
|
| 93 |
+
|
| 94 |
+
# Launch the app
|
| 95 |
+
if __name__ == "__main__":
|
| 96 |
+
app.launch()
|