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