shukdevdatta123's picture
Create v1.txt
cb7be78 verified
import gradio as gr
from together import Together
import os
import base64
def extract_medicines(api_key, image):
"""
Extract medicine names from a prescription image using Together AI's Llama-Vision-Free model
"""
# Check if API key is provided
if not api_key:
return "Please enter your Together API key."
if image is None:
return "Please upload an image."
try:
# Initialize Together client with the provided API key
client = Together(api_key=api_key)
# Convert image to base64
with open(image, "rb") as img_file:
img_data = img_file.read()
b64_img = base64.b64encode(img_data).decode('utf-8')
# Make API call with base64 encoded image
response = client.chat.completions.create(
model="meta-llama/Llama-Vision-Free",
messages=[
{
"role": "system",
"content": "You are an expert in identifying medicine names from prescription images."
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "Please extract the names of the medicines only."
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{b64_img}"
}
}
]
}
]
)
# Extract medicine names from response
medicine_list = response.choices[0].message.content
return medicine_list
except Exception as e:
return f"Error: {str(e)}"
# Create Gradio interface
with gr.Blocks(title="Prescription Medicine Extractor") as app:
gr.Markdown("## Prescription Medicine Extractor")
gr.Markdown("Upload a prescription image to extract medicine names using Together AI's Llama-Vision-Free model.")
with gr.Row():
with gr.Column():
api_key_input = gr.Textbox(
label="Together API Key",
placeholder="Enter your Together API key here...",
type="password"
)
image_input = gr.Image(type="filepath", label="Upload Prescription Image")
submit_btn = gr.Button("Extract Medicines")
with gr.Column():
output = gr.Textbox(label="Extracted Medicines", lines=10)
submit_btn.click(
fn=extract_medicines,
inputs=[api_key_input, image_input],
outputs=output
)
gr.Markdown("""
### How to use:
1. Enter your Together API key
2. Upload a clear image of a prescription
3. Click 'Extract Medicines' to see the results
### Note:
- Your API key is used only for the current session
- For best results, ensure the prescription image is clear and readable
""")
# Launch the app
if __name__ == "__main__":
app.launch()