durovali commited on
Commit
ea4eaf4
Β·
1 Parent(s): 26fdfd7

init comit

Browse files
Files changed (3) hide show
  1. README.md +0 -0
  2. app.py +86 -0
  3. 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