File size: 3,167 Bytes
cb7be78 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
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() |