| import gradio as gr |
| import os |
| import base64 |
| from groq import Groq |
| from PIL import Image |
| import io |
|
|
| |
| client = Groq(api_key=os.environ.get("GROQ_API_KEY")) |
|
|
| |
| def encode_image(image): |
| buffered = io.BytesIO() |
| image.save(buffered, format="JPEG") |
| return base64.b64encode(buffered.getvalue()).decode('utf-8') |
|
|
| |
| 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." |
|
|
| |
| query_b64 = encode_image(query_image) |
|
|
| |
| 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."}] |
| |
| |
| for b64 in FIXED_BASE64: |
| content.append({ |
| "type": "image_url", |
| "image_url": {"url": f"data:image/jpeg;base64,{b64}"} |
| }) |
|
|
| |
| content.append({ |
| "type": "image_url", |
| "image_url": {"url": f"data:image/jpeg;base64,{query_b64}"} |
| }) |
|
|
| |
| completion = client.chat.completions.create( |
| model="meta-llama/llama-4-scout-17b-16e-instruct", |
| messages=[{"role": "user", "content": content}], |
| temperature=0.0, |
| max_tokens=10 |
| ) |
| |
| return completion.choices[0].message.content.strip().upper() |
|
|
| |
| 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() |