Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torchvision.transforms as transforms
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import requests
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 8 |
+
import json
|
| 9 |
+
|
| 10 |
+
# Load ImageNet class labels
|
| 11 |
+
LABELS_URL = "https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json"
|
| 12 |
+
response = requests.get(LABELS_URL)
|
| 13 |
+
labels = json.loads(response.text)
|
| 14 |
+
|
| 15 |
+
def load_model():
|
| 16 |
+
"""
|
| 17 |
+
Load model and processor from Hugging Face Hub
|
| 18 |
+
"""
|
| 19 |
+
model_id = "jatingocodeo/ImageNet" # Updated model repository ID
|
| 20 |
+
model = AutoModelForImageClassification.from_pretrained(model_id)
|
| 21 |
+
processor = AutoImageProcessor.from_pretrained(model_id)
|
| 22 |
+
return model, processor
|
| 23 |
+
|
| 24 |
+
def predict(image):
|
| 25 |
+
"""
|
| 26 |
+
Make prediction on input image
|
| 27 |
+
"""
|
| 28 |
+
if image is None:
|
| 29 |
+
return None
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
# Load model and processor (with caching)
|
| 33 |
+
model, processor = load_model()
|
| 34 |
+
model.eval()
|
| 35 |
+
|
| 36 |
+
# Process image
|
| 37 |
+
inputs = processor(image, return_tensors="pt")
|
| 38 |
+
|
| 39 |
+
# Get predictions
|
| 40 |
+
with torch.no_grad():
|
| 41 |
+
outputs = model(**inputs)
|
| 42 |
+
logits = outputs.logits
|
| 43 |
+
|
| 44 |
+
# Get probabilities and classes
|
| 45 |
+
probs = torch.nn.functional.softmax(logits, dim=1)[0]
|
| 46 |
+
top_probs, top_indices = torch.topk(probs, k=5)
|
| 47 |
+
|
| 48 |
+
# Format results
|
| 49 |
+
results = {}
|
| 50 |
+
for prob, idx in zip(top_probs, top_indices):
|
| 51 |
+
label = labels[idx.item()]
|
| 52 |
+
confidence = prob.item() * 100
|
| 53 |
+
results[label] = confidence
|
| 54 |
+
|
| 55 |
+
return results
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return f"Error processing image: {str(e)}"
|
| 58 |
+
|
| 59 |
+
# Create Gradio interface
|
| 60 |
+
title = "ImageNet Classifier"
|
| 61 |
+
description = """
|
| 62 |
+
## ResNet50 ImageNet Classifier
|
| 63 |
+
|
| 64 |
+
This model classifies images into 1000 ImageNet categories. Upload an image or use one of the example images to get predictions.
|
| 65 |
+
|
| 66 |
+
### Instructions:
|
| 67 |
+
1. Upload an image using the input box below
|
| 68 |
+
2. The model will predict the top 5 classes for the image
|
| 69 |
+
3. Results show class names and confidence scores
|
| 70 |
+
|
| 71 |
+
### Model Details:
|
| 72 |
+
- Architecture: ResNet50
|
| 73 |
+
- Dataset: ImageNet
|
| 74 |
+
- Input Size: 224x224
|
| 75 |
+
- Number of Classes: 1000
|
| 76 |
+
"""
|
| 77 |
+
|
| 78 |
+
# Example images
|
| 79 |
+
examples = [
|
| 80 |
+
"examples/dog.jpg",
|
| 81 |
+
"examples/cat.jpg",
|
| 82 |
+
"examples/bird.jpg",
|
| 83 |
+
"examples/car.jpg",
|
| 84 |
+
"examples/flower.jpg"
|
| 85 |
+
]
|
| 86 |
+
|
| 87 |
+
# Create the interface
|
| 88 |
+
iface = gr.Interface(
|
| 89 |
+
fn=predict,
|
| 90 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
| 91 |
+
outputs=gr.Label(num_top_classes=5, label="Predictions"),
|
| 92 |
+
title=title,
|
| 93 |
+
description=description,
|
| 94 |
+
examples=examples,
|
| 95 |
+
theme=gr.themes.Soft(),
|
| 96 |
+
allow_flagging="never"
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# Launch the app
|
| 100 |
+
iface.launch()
|