init comit
Browse files- README.md +0 -0
- app.py +86 -0
- requrements.txt +5 -0
README.md
ADDED
|
File without changes
|
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline, CLIPProcessor, CLIPModel
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
import openai
|
| 6 |
+
import base64
|
| 7 |
+
import io
|
| 8 |
+
|
| 9 |
+
# ββ 1. DEIN MODELL (von Hugging Face) ββββββββββββββββββββββββββ
|
| 10 |
+
MY_MODEL_ID = "DEIN-USERNAME/DEIN-MODELL" # β anpassen!
|
| 11 |
+
my_classifier = pipeline("image-classification", model=MY_MODEL_ID)
|
| 12 |
+
|
| 13 |
+
# ββ 2. CLIP (Open-Source) ββββββββββββββββββββββββββββββββββββββ
|
| 14 |
+
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
| 15 |
+
clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
| 16 |
+
|
| 17 |
+
# Deine Klassen (anpassen!)
|
| 18 |
+
LABELS = ["cat", "dog", "bird"] # β deine eigenen Klassen
|
| 19 |
+
|
| 20 |
+
def predict_my_model(image):
|
| 21 |
+
results = my_classifier(image)
|
| 22 |
+
return {r["label"]: r["score"] for r in results}
|
| 23 |
+
|
| 24 |
+
def predict_clip(image):
|
| 25 |
+
inputs = clip_processor(
|
| 26 |
+
text=LABELS, images=image, return_tensors="pt", padding=True
|
| 27 |
+
)
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
outputs = clip_model(**inputs)
|
| 30 |
+
probs = outputs.logits_per_image.softmax(dim=1)[0]
|
| 31 |
+
return {label: float(prob) for label, prob in zip(LABELS, probs)}
|
| 32 |
+
|
| 33 |
+
def predict_openai(image):
|
| 34 |
+
client = openai.OpenAI(api_key=openai.api_key)
|
| 35 |
+
|
| 36 |
+
# Bild zu Base64 konvertieren
|
| 37 |
+
buf = io.BytesIO()
|
| 38 |
+
image.save(buf, format="JPEG")
|
| 39 |
+
b64 = base64.b64encode(buf.getvalue()).decode()
|
| 40 |
+
|
| 41 |
+
response = client.chat.completions.create(
|
| 42 |
+
model="gpt-4o",
|
| 43 |
+
messages=[{
|
| 44 |
+
"role": "user",
|
| 45 |
+
"content": [
|
| 46 |
+
{"type": "image_url",
|
| 47 |
+
"image_url": {"url": f"data:image/jpeg;base64,{b64}"}},
|
| 48 |
+
{"type": "text",
|
| 49 |
+
"text": f"Classify this image as one of: {LABELS}. "
|
| 50 |
+
f"Return only a JSON like: {{\"label\": score, ...}} "
|
| 51 |
+
f"where scores sum to 1."}
|
| 52 |
+
]
|
| 53 |
+
}],
|
| 54 |
+
max_tokens=100
|
| 55 |
+
)
|
| 56 |
+
import json
|
| 57 |
+
return json.loads(response.choices[0].message.content)
|
| 58 |
+
|
| 59 |
+
def classify_all(image):
|
| 60 |
+
r1 = predict_my_model(image)
|
| 61 |
+
r2 = predict_clip(image)
|
| 62 |
+
r3 = predict_openai(image)
|
| 63 |
+
return r1, r2, r3
|
| 64 |
+
|
| 65 |
+
# ββ Beispielbilder βββββββββββββββββββββββββββββββββββββββββββββ
|
| 66 |
+
examples = ["example1.jpg", "example2.jpg"] # β eigene Bilder
|
| 67 |
+
|
| 68 |
+
# ββ Gradio Interface βββββββββββββββββββββββββββββββββββββββββββ
|
| 69 |
+
with gr.Blocks(title="Image Classification Comparison") as demo:
|
| 70 |
+
gr.Markdown("# πΌοΈ Image Classification β Model Comparison")
|
| 71 |
+
gr.Markdown("Compare your custom model, CLIP, and GPT-4o Vision.")
|
| 72 |
+
|
| 73 |
+
with gr.Row():
|
| 74 |
+
img_input = gr.Image(type="pil", label="Upload Image")
|
| 75 |
+
|
| 76 |
+
btn = gr.Button("Classify!", variant="primary")
|
| 77 |
+
|
| 78 |
+
with gr.Row():
|
| 79 |
+
out1 = gr.Label(label="π·οΈ My Model")
|
| 80 |
+
out2 = gr.Label(label="π CLIP")
|
| 81 |
+
out3 = gr.Label(label="π€ GPT-4o")
|
| 82 |
+
|
| 83 |
+
btn.click(classify_all, inputs=img_input, outputs=[out1, out2, out3])
|
| 84 |
+
gr.Examples(examples=examples, inputs=img_input)
|
| 85 |
+
|
| 86 |
+
demo.launch()
|
requrements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
openai
|
| 5 |
+
pillow
|