Spaces:
Running
Running
File size: 4,548 Bytes
189ce61 |
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
import gradio as gr
import os
from openai import OpenAI
import re
def extract_medicine_names(api_key, image_path, model_choice):
"""
Extract medicine names from a prescription image using OpenRouter API
"""
if not api_key or api_key.strip() == "":
return "Please provide a valid OpenRouter API key."
if not image_path:
return "Please upload an image."
try:
# Initialize the OpenAI client with OpenRouter base URL
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=api_key,
)
# Select the model based on user's choice
if model_choice == "Llama 4 Maverick":
model = "meta-llama/llama-4-maverick:free"
else: # Default to Kimi VL
model = "moonshotai/kimi-vl-a3b-thinking:free"
# Prepare image for API
with open(image_path, "rb") as image_file:
import base64
image_data = base64.b64encode(image_file.read()).decode("utf-8")
# Create the completion
completion = client.chat.completions.create(
extra_headers={
"HTTP-Referer": "https://medicine-extractor-app.com",
"X-Title": "Medicine Name Extractor",
},
model=model,
messages=[
{
"role": "system",
"content": "You are a specialized medical assistant. Your task is to analyze prescription images and extract ONLY the names of medicines/medications. Return them as a clear, numbered list without any other commentary. If you cannot identify any medicine names, state that clearly."
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "Please extract and list ONLY the names of medicines or medications from this prescription image."
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{image_data}"
}
}
]
}
]
)
# Get the response
result = completion.choices[0].message.content
# Process to ensure we're only returning medicine names
# This is a basic processing step - the system prompt should already help focus the response
if "no medicine" in result.lower() or "cannot identify" in result.lower():
return "No medicine names were identified in the prescription image."
return result
except Exception as e:
return f"Error: {str(e)}"
# Define the Gradio interface
with gr.Blocks(title="Medicine Name Extractor", theme=gr.themes.Soft()) as app:
gr.Markdown("# Medicine Name Extractor")
gr.Markdown("Upload a prescription image and the app will extract medication names using AI vision models.")
with gr.Row():
with gr.Column():
api_key = gr.Textbox(
label="OpenRouter API Key",
placeholder="Enter your OpenRouter API key here",
type="password"
)
model_choice = gr.Radio(
["Llama 4 Maverick", "Kimi VL"],
label="Select AI Model",
value="Kimi VL"
)
image_input = gr.Image(
label="Upload Prescription Image",
type="filepath"
)
submit_btn = gr.Button("Extract Medicine Names", variant="primary")
with gr.Column():
output = gr.Textbox(
label="Extracted Medicine Names",
lines=10
)
# Handle the submission
submit_btn.click(
fn=extract_medicine_names,
inputs=[api_key, image_input, model_choice],
outputs=output
)
gr.Markdown("""
## How to use
1. Enter your OpenRouter API key
2. Select an AI vision model
3. Upload a clear image of a medical prescription
4. Click "Extract Medicine Names"
## Privacy Note
Your prescription images are processed securely. No data is stored on our servers.
""")
# Launch the app
if __name__ == "__main__":
app.launch() |