Spaces:
Build error
Build error
Commit
·
a89dfce
1
Parent(s):
40669a5
initial commit
Browse files- app.py +37 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Load model and processor
|
7 |
+
model_name = "facebook/deit-base-distilled-patch16-224"
|
8 |
+
extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
9 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
10 |
+
|
11 |
+
cat_keywords = ["cat", "kitten", "feline", "tabby", "siamese", "persian", "egyptian cat"]
|
12 |
+
|
13 |
+
def detect_cat(img):
|
14 |
+
inputs = extractor(images=img, return_tensors="pt")
|
15 |
+
with torch.no_grad():
|
16 |
+
outputs = model(**inputs).logits
|
17 |
+
|
18 |
+
probs = torch.softmax(outputs, dim=-1)
|
19 |
+
predicted_class = torch.argmax(probs).item()
|
20 |
+
confidence = probs[0][predicted_class].item()
|
21 |
+
label = model.config.id2label[predicted_class].lower()
|
22 |
+
|
23 |
+
if confidence < 0.60:
|
24 |
+
return f"🤔 Not confident it's a cat. ({label}, {confidence:.2f})"
|
25 |
+
elif any(k in label for k in cat_keywords):
|
26 |
+
return f"😺 Yes, it's a cat! ({label}, confidence: {confidence:.2f})"
|
27 |
+
else:
|
28 |
+
return f"🐶 Nope, not a cat. ({label}, confidence: {confidence:.2f})"
|
29 |
+
|
30 |
+
gr.Interface(
|
31 |
+
fn=detect_cat,
|
32 |
+
inputs=gr.Image(type="pil", label="Upload your image 🖼️", height=300),
|
33 |
+
outputs=gr.Textbox(label="🐾 Result"),
|
34 |
+
title="😼 Is It a Cat?",
|
35 |
+
description="Upload an image to check if there's a cat in it.",
|
36 |
+
theme="default"
|
37 |
+
).launch()ß
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
transformers
|
3 |
+
torch
|
4 |
+
pillow
|