Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
# Load models
|
| 5 |
vit_classifier = pipeline("image-classification", model="kuhs/vit-base-oxford-iiit-pets")
|
| 6 |
clip_detector = pipeline(model="openai/clip-vit-large-patch14", task="zero-shot-image-classification")
|
|
@@ -13,6 +25,54 @@ labels_oxford_pets = [
|
|
| 13 |
'samoyed', 'British Shorthair', 'great pyrenees', 'Abyssinian', 'pug', 'saint bernard', 'Russian Blue', 'scottish terrier'
|
| 14 |
]
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def classify_pet(image):
|
| 17 |
vit_results = vit_classifier(image)
|
| 18 |
vit_output = {result['label']: result['score'] for result in vit_results}
|
|
@@ -20,7 +80,13 @@ def classify_pet(image):
|
|
| 20 |
clip_results = clip_detector(image, candidate_labels=labels_oxford_pets)
|
| 21 |
clip_output = {result['label']: result['score'] for result in clip_results}
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
example_images = [
|
| 26 |
["example_images/dog1.jpeg"],
|
|
@@ -35,7 +101,7 @@ iface = gr.Interface(
|
|
| 35 |
inputs=gr.Image(type="filepath"),
|
| 36 |
outputs=gr.JSON(),
|
| 37 |
title="Pet Classification Comparison",
|
| 38 |
-
description="Upload an image of a pet, and compare results from a trained ViT model
|
| 39 |
examples=example_images
|
| 40 |
)
|
| 41 |
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
from openai import OpenAI
|
| 8 |
from transformers import pipeline
|
| 9 |
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-4.1-mini")
|
| 13 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 14 |
+
openai_client = OpenAI(api_key=OPENAI_API_KEY) if OPENAI_API_KEY else None
|
| 15 |
+
|
| 16 |
# Load models
|
| 17 |
vit_classifier = pipeline("image-classification", model="kuhs/vit-base-oxford-iiit-pets")
|
| 18 |
clip_detector = pipeline(model="openai/clip-vit-large-patch14", task="zero-shot-image-classification")
|
|
|
|
| 25 |
'samoyed', 'British Shorthair', 'great pyrenees', 'Abyssinian', 'pug', 'saint bernard', 'Russian Blue', 'scottish terrier'
|
| 26 |
]
|
| 27 |
|
| 28 |
+
|
| 29 |
+
def encode_image(image_path):
|
| 30 |
+
with open(image_path, "rb") as image_file:
|
| 31 |
+
return base64.b64encode(image_file.read()).decode("utf-8")
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def classify_with_openai(image_path):
|
| 35 |
+
if openai_client is None:
|
| 36 |
+
return {
|
| 37 |
+
"error": "Missing OPENAI_API_KEY. Add it to your environment or .env file to enable OpenAI classification."
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
prompt = (
|
| 41 |
+
"Classify the pet in this image. Choose the best matching label from this list: "
|
| 42 |
+
f"{', '.join(labels_oxford_pets)}. "
|
| 43 |
+
"Return valid JSON with exactly these keys: "
|
| 44 |
+
"label, confidence, reasoning. "
|
| 45 |
+
"The confidence must be a number between 0 and 1."
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
base64_image = encode_image(image_path)
|
| 49 |
+
response = openai_client.responses.create(
|
| 50 |
+
model=OPENAI_MODEL,
|
| 51 |
+
input=[
|
| 52 |
+
{
|
| 53 |
+
"role": "user",
|
| 54 |
+
"content": [
|
| 55 |
+
{"type": "input_text", "text": prompt},
|
| 56 |
+
{
|
| 57 |
+
"type": "input_image",
|
| 58 |
+
"image_url": f"data:image/jpeg;base64,{base64_image}",
|
| 59 |
+
},
|
| 60 |
+
],
|
| 61 |
+
}
|
| 62 |
+
],
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
parsed_response = json.loads(response.output_text)
|
| 67 |
+
except json.JSONDecodeError:
|
| 68 |
+
parsed_response = {
|
| 69 |
+
"raw_response": response.output_text,
|
| 70 |
+
"warning": "OpenAI response was not valid JSON.",
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
return parsed_response
|
| 74 |
+
|
| 75 |
+
|
| 76 |
def classify_pet(image):
|
| 77 |
vit_results = vit_classifier(image)
|
| 78 |
vit_output = {result['label']: result['score'] for result in vit_results}
|
|
|
|
| 80 |
clip_results = clip_detector(image, candidate_labels=labels_oxford_pets)
|
| 81 |
clip_output = {result['label']: result['score'] for result in clip_results}
|
| 82 |
|
| 83 |
+
openai_output = classify_with_openai(image)
|
| 84 |
+
|
| 85 |
+
return {
|
| 86 |
+
"ViT Classification": vit_output,
|
| 87 |
+
"CLIP Zero-Shot Classification": clip_output,
|
| 88 |
+
"OpenAI Vision Classification": openai_output,
|
| 89 |
+
}
|
| 90 |
|
| 91 |
example_images = [
|
| 92 |
["example_images/dog1.jpeg"],
|
|
|
|
| 101 |
inputs=gr.Image(type="filepath"),
|
| 102 |
outputs=gr.JSON(),
|
| 103 |
title="Pet Classification Comparison",
|
| 104 |
+
description="Upload an image of a pet, and compare results from a trained ViT model, a zero-shot CLIP model, and an OpenAI vision model.",
|
| 105 |
examples=example_images
|
| 106 |
)
|
| 107 |
|