vasanthi8134 commited on
Commit
490d21e
·
verified ·
1 Parent(s): 181e17e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import json
3
+ import os
4
+ from glob import glob
5
+
6
+ import gradio as gr
7
+ from openai import OpenAI
8
+ from transformers import pipeline
9
+
10
+ CLASS_LABELS = ["Egyptian Mau", "leonberger", "samoyed"]
11
+
12
+ MODEL_REPO = "vasanthi8134/oxford-pets-3class-vit"
13
+ CLIP_MODEL = "openai/clip-vit-base-patch32"
14
+ OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-4.1-mini")
15
+
16
+ openai_api_key = os.getenv("OPENAI_API_KEY")
17
+ openai_client = OpenAI(api_key=openai_api_key) if openai_api_key else None
18
+
19
+ vit_classifier = pipeline(
20
+ "image-classification",
21
+ model=MODEL_REPO,
22
+ )
23
+
24
+ clip_classifier = pipeline(
25
+ "zero-shot-image-classification",
26
+ model=CLIP_MODEL,
27
+ )
28
+
29
+ def encode_image(image_path):
30
+ with open(image_path, "rb") as f:
31
+ return base64.b64encode(f.read()).decode("utf-8")
32
+
33
+ def classify_with_openai(image_path):
34
+ if openai_client is None:
35
+ return {
36
+ "error": "Missing OPENAI_API_KEY in Hugging Face Space Secrets."
37
+ }
38
+
39
+ prompt = (
40
+ "Classify the pet in this image. "
41
+ f"Choose exactly one label from this list: {CLASS_LABELS}. "
42
+ 'Return valid JSON with keys: "label", "confidence", "reasoning". '
43
+ "Confidence must be a number between 0 and 1."
44
+ )
45
+
46
+ base64_image = encode_image(image_path)
47
+
48
+ response = openai_client.responses.create(
49
+ model=OPENAI_MODEL,
50
+ input=[
51
+ {
52
+ "role": "user",
53
+ "content": [
54
+ {"type": "input_text", "text": prompt},
55
+ {
56
+ "type": "input_image",
57
+ "image_url": f"data:image/jpeg;base64,{base64_image}",
58
+ },
59
+ ],
60
+ }
61
+ ],
62
+ )
63
+
64
+ try:
65
+ return json.loads(response.output_text)
66
+ except Exception:
67
+ return {"raw_response": response.output_text}
68
+
69
+ def classify_pet(image_path):
70
+ vit_results = vit_classifier(image_path)
71
+ vit_output = {item["label"]: round(float(item["score"]), 4) for item in vit_results}
72
+
73
+ clip_results = clip_classifier(image_path, candidate_labels=CLASS_LABELS)
74
+ clip_output = {item["label"]: round(float(item["score"]), 4) for item in clip_results}
75
+
76
+ openai_output = classify_with_openai(image_path)
77
+
78
+ return {
79
+ "your_model_vit": vit_output,
80
+ "open_source_clip": clip_output,
81
+ "closed_source_openai": openai_output,
82
+ }
83
+
84
+ example_files = []
85
+ for ext in ["jpg", "jpeg", "png", "webp"]:
86
+ example_files.extend(glob(f"example_images/*.{ext}"))
87
+ example_files.extend(glob(f"example_images/*.{ext.upper()}"))
88
+
89
+ example_files = [[path] for path in sorted(example_files)]
90
+
91
+ iface = gr.Interface(
92
+ fn=classify_pet,
93
+ inputs=gr.Image(type="filepath", label="Upload pet image"),
94
+ outputs=gr.JSON(label="Model comparison"),
95
+ title="Pet Classification Comparison",
96
+ description=(
97
+ "Compare a fine-tuned ViT model, a zero-shot CLIP model, "
98
+ "and an OpenAI vision model on 3 pet classes: "
99
+ "Egyptian Mau, leonberger, samoyed."
100
+ ),
101
+ examples=example_files if example_files else None,
102
+ allow_flagging="never",
103
+ )
104
+
105
+ iface.launch()