Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- .github/workflows/update_space.yml +28 -0
- README.md +2 -8
- app.py +115 -0
- examples/Beagle_56.jpg +0 -0
- examples/Boxer_30.jpg +0 -0
- examples/Bulldog_73.jpg +0 -0
- examples/Dachshund_43.jpg +0 -0
- examples/German Shepherd_57.jpg +0 -0
- examples/Golden Retriever_78.jpg +0 -0
- examples/Labrador Retriever_25.jpg +0 -0
- examples/Poodle_85.jpg +0 -0
- examples/Rottweiler_30.jpg +0 -0
- examples/Yorkshire Terrier_92.jpg +0 -0
- requirements.txt +3 -0
.github/workflows/update_space.yml
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: Run Python script
|
| 2 |
+
|
| 3 |
+
on:
|
| 4 |
+
push:
|
| 5 |
+
branches:
|
| 6 |
+
- main
|
| 7 |
+
|
| 8 |
+
jobs:
|
| 9 |
+
build:
|
| 10 |
+
runs-on: ubuntu-latest
|
| 11 |
+
|
| 12 |
+
steps:
|
| 13 |
+
- name: Checkout
|
| 14 |
+
uses: actions/checkout@v2
|
| 15 |
+
|
| 16 |
+
- name: Set up Python
|
| 17 |
+
uses: actions/setup-python@v2
|
| 18 |
+
with:
|
| 19 |
+
python-version: '3.9'
|
| 20 |
+
|
| 21 |
+
- name: Install Gradio
|
| 22 |
+
run: python -m pip install gradio
|
| 23 |
+
|
| 24 |
+
- name: Log in to Hugging Face
|
| 25 |
+
run: python -c 'import huggingface_hub; huggingface_hub.login(token="${{ secrets.hf_token }}")'
|
| 26 |
+
|
| 27 |
+
- name: Deploy to Spaces
|
| 28 |
+
run: gradio deploy
|
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title: Torchscript
|
| 3 |
-
|
| 4 |
-
colorFrom: gray
|
| 5 |
-
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.6.0
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Torchscript-Inference
|
| 3 |
+
app_file: app.py
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
sdk_version: 5.6.0
|
|
|
|
|
|
|
| 6 |
---
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torchvision.transforms as transforms
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
class DogBreedClassifier:
|
| 8 |
+
def __init__(self, model_path="traced_models/model_tracing.pt"):
|
| 9 |
+
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 10 |
+
|
| 11 |
+
# Load the traced model
|
| 12 |
+
self.model = torch.jit.load(model_path)
|
| 13 |
+
self.model = self.model.to(self.device)
|
| 14 |
+
self.model.eval()
|
| 15 |
+
|
| 16 |
+
# Define the same transforms used during training/testing
|
| 17 |
+
self.transform = transforms.Compose([
|
| 18 |
+
transforms.Resize((160, 160)),
|
| 19 |
+
transforms.ToTensor(),
|
| 20 |
+
transforms.Normalize(
|
| 21 |
+
mean=[0.485, 0.456, 0.406],
|
| 22 |
+
std=[0.229, 0.224, 0.225]
|
| 23 |
+
)
|
| 24 |
+
])
|
| 25 |
+
|
| 26 |
+
# Class labels
|
| 27 |
+
self.labels = ['Beagle', 'Boxer', 'Bulldog', 'Dachshund', 'German_Shepherd', 'Golden_Retriever', 'Labrador_Retriever', 'Poodle', 'Rottweiler', 'Yorkshire_Terrier']
|
| 28 |
+
|
| 29 |
+
# Add dog breed facts dictionary
|
| 30 |
+
self.breed_facts = {
|
| 31 |
+
'Beagle': "Beagles have approximately 220 million scent receptors, compared to a human's mere 5 million!",
|
| 32 |
+
'Boxer': "Boxers were among the first dogs to be employed as police dogs and were used as messenger dogs during wartime.",
|
| 33 |
+
'Bulldog': "Despite their tough appearance, Bulldogs were bred to be companion dogs and are known for being gentle and patient.",
|
| 34 |
+
'Dachshund': "Dachshunds were originally bred to hunt badgers - their name literally means 'badger dog' in German!",
|
| 35 |
+
'German_Shepherd': "German Shepherds can learn a new command in as little as 5 repetitions and obey it 95% of the time.",
|
| 36 |
+
'Golden_Retriever': "Golden Retrievers were originally bred as hunting dogs to retrieve waterfowl without damaging them.",
|
| 37 |
+
'Labrador_Retriever': "Labs have a special water-resistant coat and a unique otter-like tail that helps them swim efficiently.",
|
| 38 |
+
'Poodle': "Despite their elegant appearance, Poodles were originally water retrievers, and their fancy haircut had a practical purpose!",
|
| 39 |
+
'Rottweiler': "Rottweilers are descendants of Roman drover dogs and were used to herd livestock and pull carts for butchers.",
|
| 40 |
+
'Yorkshire_Terrier': "Yorkies were originally bred to catch rats in clothing mills. Despite their small size, they're true working dogs!"
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
@torch.no_grad()
|
| 44 |
+
def predict(self, image):
|
| 45 |
+
if image is None:
|
| 46 |
+
return None, None
|
| 47 |
+
|
| 48 |
+
# Convert to PIL Image if needed
|
| 49 |
+
if not isinstance(image, Image.Image):
|
| 50 |
+
image = Image.fromarray(image).convert('RGB')
|
| 51 |
+
|
| 52 |
+
# Preprocess image
|
| 53 |
+
img_tensor = self.transform(image).unsqueeze(0).to(self.device)
|
| 54 |
+
|
| 55 |
+
# Get prediction
|
| 56 |
+
output = self.model(img_tensor)
|
| 57 |
+
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
| 58 |
+
|
| 59 |
+
# Get the breed with highest probability
|
| 60 |
+
max_prob_idx = torch.argmax(probabilities).item()
|
| 61 |
+
predicted_breed = self.labels[max_prob_idx]
|
| 62 |
+
breed_fact = self.breed_facts[predicted_breed]
|
| 63 |
+
|
| 64 |
+
# Create prediction dictionary
|
| 65 |
+
predictions = {
|
| 66 |
+
self.labels[idx]: float(prob)
|
| 67 |
+
for idx, prob in enumerate(probabilities)
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
return predictions, breed_fact
|
| 71 |
+
|
| 72 |
+
classifier = DogBreedClassifier()
|
| 73 |
+
|
| 74 |
+
demo = gr.Interface(
|
| 75 |
+
fn=classifier.predict,
|
| 76 |
+
inputs=gr.Image(type="pil", label="Upload a dog image"),
|
| 77 |
+
outputs=[
|
| 78 |
+
gr.Label(num_top_classes=5, label="Breed Predictions"),
|
| 79 |
+
gr.Textbox(label="Fun Fact About This Breed!")
|
| 80 |
+
],
|
| 81 |
+
title="🐕 Dog Breed Classifier",
|
| 82 |
+
description="""
|
| 83 |
+
## Identify Your Dog's Breed!
|
| 84 |
+
Upload a clear photo of a dog, and I'll tell you its breed and share an interesting fact about it!
|
| 85 |
+
This model can identify 10 popular dog breeds with high accuracy.
|
| 86 |
+
|
| 87 |
+
### Supported Breeds:
|
| 88 |
+
Beagle, Boxer, Bulldog, Dachshund, German Shepherd, Golden Retriever, Labrador Retriever, Poodle, Rottweiler, Yorkshire Terrier
|
| 89 |
+
""",
|
| 90 |
+
article="""
|
| 91 |
+
### Tips for best results:
|
| 92 |
+
- Use clear, well-lit photos
|
| 93 |
+
- Ensure the dog's face is visible
|
| 94 |
+
- Avoid blurry or dark images
|
| 95 |
+
|
| 96 |
+
Created with PyTorch and Gradio | [GitHub](your_github_link)
|
| 97 |
+
""",
|
| 98 |
+
examples=[
|
| 99 |
+
["examples/Beagle_56.jpg"],
|
| 100 |
+
["examples/Boxer_30.jpg"],
|
| 101 |
+
["examples/Bulldog_73.jpg"],
|
| 102 |
+
["examples/Dachshund_43.jpg"],
|
| 103 |
+
["examples/German Shepherd_57.jpg"],
|
| 104 |
+
["examples/Golden Retriever_78.jpg"],
|
| 105 |
+
["examples/Labrador Retriever_25.jpg"],
|
| 106 |
+
["examples/Poodle_85.jpg"],
|
| 107 |
+
["examples/Rottweiler_30.jpg"],
|
| 108 |
+
["examples/Yorkshire Terrier_92.jpg"]
|
| 109 |
+
],
|
| 110 |
+
theme=gr.themes.Citrus(),
|
| 111 |
+
css="footer {display: none !important;}"
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
if __name__ == "__main__":
|
| 115 |
+
demo.launch()
|
examples/Beagle_56.jpg
ADDED
|
examples/Boxer_30.jpg
ADDED
|
examples/Bulldog_73.jpg
ADDED
|
examples/Dachshund_43.jpg
ADDED
|
examples/German Shepherd_57.jpg
ADDED
|
examples/Golden Retriever_78.jpg
ADDED
|
examples/Labrador Retriever_25.jpg
ADDED
|
examples/Poodle_85.jpg
ADDED
|
examples/Rottweiler_30.jpg
ADDED
|
examples/Yorkshire Terrier_92.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
gradio
|
| 3 |
+
torchvision
|