Spaces:
Runtime error
Runtime error
macadeliccc
commited on
Commit
•
4c200b6
1
Parent(s):
81d5625
init
Browse files- README.md +4 -3
- app.py +45 -0
- requirements.txt +7 -0
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
title: Large Dog Breed Classifier
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.1.2
|
8 |
app_file: app.py
|
@@ -11,3 +11,4 @@ license: apache-2.0
|
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
1 |
---
|
2 |
title: Large Dog Breed Classifier
|
3 |
+
emoji: 🐶
|
4 |
+
colorFrom: indigo
|
5 |
+
colorTo: blue
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.1.2
|
8 |
app_file: app.py
|
|
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
14 |
+
|
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
4 |
+
import torch
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# Load the fine-tuned model
|
8 |
+
model = AutoModelForImageClassification.from_pretrained("Pavarissy/ConvNextV2-large-DogBreed")
|
9 |
+
|
10 |
+
# Initialize the image processor
|
11 |
+
preprocessor = AutoImageProcessor.from_pretrained("Pavarissy/ConvNextV2-large-DogBreed")
|
12 |
+
|
13 |
+
def classify_image(image):
|
14 |
+
# Preprocess the image
|
15 |
+
inputs = preprocessor(images=image, return_tensors="pt")
|
16 |
+
|
17 |
+
# Model prediction
|
18 |
+
with torch.no_grad():
|
19 |
+
logits = model(**inputs).logits
|
20 |
+
|
21 |
+
# Convert logits to probabilities
|
22 |
+
probs = logits.softmax(dim=-1)
|
23 |
+
|
24 |
+
# Extract top 5 predictions
|
25 |
+
top_5_probs, top_5_labels = torch.topk(probs, 5)
|
26 |
+
top_5_probs = top_5_probs.squeeze().tolist()
|
27 |
+
top_5_labels = top_5_labels.squeeze().tolist()
|
28 |
+
|
29 |
+
# Map labels to their names
|
30 |
+
labels = model.config.id2label
|
31 |
+
predicted_labels = [labels[label] for label in top_5_labels]
|
32 |
+
|
33 |
+
return dict(zip(predicted_labels, top_5_probs))
|
34 |
+
|
35 |
+
# Create a Gradio interface
|
36 |
+
iface = gr.Interface(
|
37 |
+
fn=classify_image,
|
38 |
+
inputs=gr.Image(type="pil"),
|
39 |
+
outputs=gr.Label(num_top_classes=5),
|
40 |
+
title="Dog Breed Classifier",
|
41 |
+
description="Upload an image of a dog, and the model will predict the breed."
|
42 |
+
)
|
43 |
+
|
44 |
+
# Launch the interface
|
45 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
git+https://github.com/huggingface/transformers.git
|
2 |
+
--extra-index-url https://download.pytorch.org/whl/cu113
|
3 |
+
torch
|
4 |
+
datasets
|
5 |
+
accelerate
|
6 |
+
numpy
|
7 |
+
ochat
|