shukdevdatta123 commited on
Commit
189ce61
·
verified ·
1 Parent(s): 706b7d2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -0
app.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from openai import OpenAI
4
+ import re
5
+
6
+ def extract_medicine_names(api_key, image_path, model_choice):
7
+ """
8
+ Extract medicine names from a prescription image using OpenRouter API
9
+ """
10
+ if not api_key or api_key.strip() == "":
11
+ return "Please provide a valid OpenRouter API key."
12
+
13
+ if not image_path:
14
+ return "Please upload an image."
15
+
16
+ try:
17
+ # Initialize the OpenAI client with OpenRouter base URL
18
+ client = OpenAI(
19
+ base_url="https://openrouter.ai/api/v1",
20
+ api_key=api_key,
21
+ )
22
+
23
+ # Select the model based on user's choice
24
+ if model_choice == "Llama 4 Maverick":
25
+ model = "meta-llama/llama-4-maverick:free"
26
+ else: # Default to Kimi VL
27
+ model = "moonshotai/kimi-vl-a3b-thinking:free"
28
+
29
+ # Prepare image for API
30
+ with open(image_path, "rb") as image_file:
31
+ import base64
32
+ image_data = base64.b64encode(image_file.read()).decode("utf-8")
33
+
34
+ # Create the completion
35
+ completion = client.chat.completions.create(
36
+ extra_headers={
37
+ "HTTP-Referer": "https://medicine-extractor-app.com",
38
+ "X-Title": "Medicine Name Extractor",
39
+ },
40
+ model=model,
41
+ messages=[
42
+ {
43
+ "role": "system",
44
+ "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."
45
+ },
46
+ {
47
+ "role": "user",
48
+ "content": [
49
+ {
50
+ "type": "text",
51
+ "text": "Please extract and list ONLY the names of medicines or medications from this prescription image."
52
+ },
53
+ {
54
+ "type": "image_url",
55
+ "image_url": {
56
+ "url": f"data:image/jpeg;base64,{image_data}"
57
+ }
58
+ }
59
+ ]
60
+ }
61
+ ]
62
+ )
63
+
64
+ # Get the response
65
+ result = completion.choices[0].message.content
66
+
67
+ # Process to ensure we're only returning medicine names
68
+ # This is a basic processing step - the system prompt should already help focus the response
69
+ if "no medicine" in result.lower() or "cannot identify" in result.lower():
70
+ return "No medicine names were identified in the prescription image."
71
+
72
+ return result
73
+
74
+ except Exception as e:
75
+ return f"Error: {str(e)}"
76
+
77
+ # Define the Gradio interface
78
+ with gr.Blocks(title="Medicine Name Extractor", theme=gr.themes.Soft()) as app:
79
+ gr.Markdown("# Medicine Name Extractor")
80
+ gr.Markdown("Upload a prescription image and the app will extract medication names using AI vision models.")
81
+
82
+ with gr.Row():
83
+ with gr.Column():
84
+ api_key = gr.Textbox(
85
+ label="OpenRouter API Key",
86
+ placeholder="Enter your OpenRouter API key here",
87
+ type="password"
88
+ )
89
+
90
+ model_choice = gr.Radio(
91
+ ["Llama 4 Maverick", "Kimi VL"],
92
+ label="Select AI Model",
93
+ value="Kimi VL"
94
+ )
95
+
96
+ image_input = gr.Image(
97
+ label="Upload Prescription Image",
98
+ type="filepath"
99
+ )
100
+
101
+ submit_btn = gr.Button("Extract Medicine Names", variant="primary")
102
+
103
+ with gr.Column():
104
+ output = gr.Textbox(
105
+ label="Extracted Medicine Names",
106
+ lines=10
107
+ )
108
+
109
+ # Handle the submission
110
+ submit_btn.click(
111
+ fn=extract_medicine_names,
112
+ inputs=[api_key, image_input, model_choice],
113
+ outputs=output
114
+ )
115
+
116
+ gr.Markdown("""
117
+ ## How to use
118
+ 1. Enter your OpenRouter API key
119
+ 2. Select an AI vision model
120
+ 3. Upload a clear image of a medical prescription
121
+ 4. Click "Extract Medicine Names"
122
+
123
+ ## Privacy Note
124
+ Your prescription images are processed securely. No data is stored on our servers.
125
+ """)
126
+
127
+ # Launch the app
128
+ if __name__ == "__main__":
129
+ app.launch()