Download app.py from durovali/Block-Computer-Vision: direct link, hf CLI and curl.
- Browser
- Download file 3.26 kB
-
https://huggingface.co/durovali/Block-Computer-Vision/resolve/main/app.py
- Command line
-
hf download hf://durovali/Block-Computer-Vision/app.py
-
curl -L -o app.py https://huggingface.co/durovali/Block-Computer-Vision/resolve/main/app.py
3.26 kB
| import gradio as gr | |
| from transformers import pipeline, CLIPProcessor, CLIPModel | |
| from PIL import Image | |
| import torch | |
| import openai | |
| import base64 | |
| import io | |
| # ββ 1. DEIN MODELL (von Hugging Face) ββββββββββββββββββββββββββ | |
| MY_MODEL_ID = "DEIN-USERNAME/DEIN-MODELL" # β anpassen! | |
| my_classifier = pipeline("image-classification", model=MY_MODEL_ID) | |
| # ββ 2. CLIP (Open-Source) ββββββββββββββββββββββββββββββββββββββ | |
| clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32") | |
| clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32") | |
| # Deine Klassen (anpassen!) | |
| LABELS = ["cat", "dog", "bird"] # β deine eigenen Klassen | |
| def predict_my_model(image): | |
| results = my_classifier(image) | |
| return {r["label"]: r["score"] for r in results} | |
| def predict_clip(image): | |
| inputs = clip_processor( | |
| text=LABELS, images=image, return_tensors="pt", padding=True | |
| ) | |
| with torch.no_grad(): | |
| outputs = clip_model(**inputs) | |
| probs = outputs.logits_per_image.softmax(dim=1)[0] | |
| return {label: float(prob) for label, prob in zip(LABELS, probs)} | |
| def predict_openai(image): | |
| client = openai.OpenAI(api_key=openai.api_key) | |
| # Bild zu Base64 konvertieren | |
| buf = io.BytesIO() | |
| image.save(buf, format="JPEG") | |
| b64 = base64.b64encode(buf.getvalue()).decode() | |
| response = client.chat.completions.create( | |
| model="gpt-4o", | |
| messages=[{ | |
| "role": "user", | |
| "content": [ | |
| {"type": "image_url", | |
| "image_url": {"url": f"data:image/jpeg;base64,{b64}"}}, | |
| {"type": "text", | |
| "text": f"Classify this image as one of: {LABELS}. " | |
| f"Return only a JSON like: {{\"label\": score, ...}} " | |
| f"where scores sum to 1."} | |
| ] | |
| }], | |
| max_tokens=100 | |
| ) | |
| import json | |
| return json.loads(response.choices[0].message.content) | |
| def classify_all(image): | |
| r1 = predict_my_model(image) | |
| r2 = predict_clip(image) | |
| r3 = predict_openai(image) | |
| return r1, r2, r3 | |
| # ββ Beispielbilder βββββββββββββββββββββββββββββββββββββββββββββ | |
| examples = ["example1.jpg", "example2.jpg"] # β eigene Bilder | |
| # ββ Gradio Interface βββββββββββββββββββββββββββββββββββββββββββ | |
| with gr.Blocks(title="Image Classification Comparison") as demo: | |
| gr.Markdown("# πΌοΈ Image Classification β Model Comparison") | |
| gr.Markdown("Compare your custom model, CLIP, and GPT-4o Vision.") | |
| with gr.Row(): | |
| img_input = gr.Image(type="pil", label="Upload Image") | |
| btn = gr.Button("Classify!", variant="primary") | |
| with gr.Row(): | |
| out1 = gr.Label(label="π·οΈ My Model") | |
| out2 = gr.Label(label="π CLIP") | |
| out3 = gr.Label(label="π€ GPT-4o") | |
| btn.click(classify_all, inputs=img_input, outputs=[out1, out2, out3]) | |
| gr.Examples(examples=examples, inputs=img_input) | |
| demo.launch() |