Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from together import Together
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
def extract_medicines(api_key, image):
|
| 6 |
"""
|
|
@@ -10,11 +11,19 @@ def extract_medicines(api_key, image):
|
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
response = client.chat.completions.create(
|
| 19 |
model="meta-llama/Llama-Vision-Free",
|
| 20 |
messages=[
|
|
@@ -32,7 +41,7 @@ def extract_medicines(api_key, image):
|
|
| 32 |
{
|
| 33 |
"type": "image_url",
|
| 34 |
"image_url": {
|
| 35 |
-
"url": image
|
| 36 |
}
|
| 37 |
}
|
| 38 |
]
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from together import Together
|
| 3 |
import os
|
| 4 |
+
import base64
|
| 5 |
|
| 6 |
def extract_medicines(api_key, image):
|
| 7 |
"""
|
|
|
|
| 11 |
if not api_key:
|
| 12 |
return "Please enter your Together API key."
|
| 13 |
|
| 14 |
+
if image is None:
|
| 15 |
+
return "Please upload an image."
|
| 16 |
+
|
| 17 |
try:
|
| 18 |
# Initialize Together client with the provided API key
|
| 19 |
client = Together(api_key=api_key)
|
| 20 |
|
| 21 |
+
# Convert image to base64
|
| 22 |
+
with open(image, "rb") as img_file:
|
| 23 |
+
img_data = img_file.read()
|
| 24 |
+
b64_img = base64.b64encode(img_data).decode('utf-8')
|
| 25 |
+
|
| 26 |
+
# Make API call with base64 encoded image
|
| 27 |
response = client.chat.completions.create(
|
| 28 |
model="meta-llama/Llama-Vision-Free",
|
| 29 |
messages=[
|
|
|
|
| 41 |
{
|
| 42 |
"type": "image_url",
|
| 43 |
"image_url": {
|
| 44 |
+
"url": f"data:image/jpeg;base64,{b64_img}"
|
| 45 |
}
|
| 46 |
}
|
| 47 |
]
|