Spaces:
Build error
Build error
File size: 1,335 Bytes
c3cba0a | 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 | from groq import Groq
from dotenv import load_dotenv
import base64
load_dotenv()
# image_path="images/acne.jpg"
def load_image_as_base64(image_path):
"""
Reads an image from the filesystem and returns its base64-encoded string.
"""
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
def build_groq_messages(query_text, image_base64):
"""
Constructs the messages payload for Groq chat completion with image input.
"""
return [
{
"role": "user",
"content": [
{"type": "text", "text": query_text},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64}"}},
],
}
]
def analyze_image_with_groq(image_path, query,model):
"""
Calls Groq Vision-enabled model with the image and returns the response.
"""
client = Groq()
image_base64 = load_image_as_base64(image_path)
messages = build_groq_messages(query, image_base64)
model = model
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=1,
max_completion_tokens=1024,
top_p=1,
stream=False,
stop=None,
)
return response.choices[0].message.content
|