File size: 2,193 Bytes
5f8da30 404c154 b221614 404c154 b221614 404c154 80f19e7 404c154 80f19e7 404c154 80f19e7 404c154 80f19e7 404c154 140261e 404c154 140261e 80f19e7 404c154 80f19e7 404c154 80f19e7 404c154 875ca35 80f19e7 | 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 | import gradio as gr
import os
import base64
from groq import Groq
from PIL import Image
import io
# 1. Setup Groq Client (Ensure GROQ_API_KEY is in Space Secrets)
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
# 2. Function to convert PIL image to Base64
def encode_image(image):
buffered = io.BytesIO()
image.save(buffered, format="JPEG")
return base64.b64encode(buffered.getvalue()).decode('utf-8')
# 3. Load and encode fixed reference images from the local folder
REF_PATHS = ["references/reference_1.jpeg", "references/reference_2.jpeg"]
FIXED_BASE64 = []
for path in REF_PATHS:
if os.path.exists(path):
img = Image.open(path).convert("RGB")
FIXED_BASE64.append(encode_image(img))
def detect_covering(query_image):
if query_image is None:
return "Please upload an image."
# Encode the user's query image
query_b64 = encode_image(query_image)
# Build the multi-image message content
content = [{"type": "text", "text": "First two reference images show a green pole around the tree. Determine if the LAST image contains the SAME type of green pole. Answer ONLY YES or NO."}]
# Add reference images to content
for b64 in FIXED_BASE64:
content.append({
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{b64}"}
})
# Add the final query image
content.append({
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{query_b64}"}
})
# Call Groq API (Llama 3.2 Vision supports up to 5 images per request)
completion = client.chat.completions.create(
model="meta-llama/llama-4-scout-17b-16e-instruct",
messages=[{"role": "user", "content": content}],
temperature=0.0, # Keep output consistent
max_tokens=10
)
return completion.choices[0].message.content.strip().upper()
# Gradio Interface
demo = gr.Interface(
fn=detect_covering,
inputs=gr.Image(type="pil", label="Upload Query Image"),
outputs="text",
title="Tree Guard Detector",
description="Uses pre-set reference images from the repo to detect tree guards via Groq."
)
demo.launch() |