Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,21 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
|
|
|
| 5 |
|
| 6 |
# Load the pre-trained model and preprocessor (feature extractor)
|
| 7 |
model_name = "jjuarez/Vit_waste_image_class"
|
| 8 |
-
model =
|
| 9 |
-
feature_extractor =
|
| 10 |
|
| 11 |
def classify_image(image):
|
| 12 |
-
# Convert PIL Image to
|
| 13 |
image = np.array(image)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 17 |
-
|
| 18 |
# Make prediction
|
| 19 |
with torch.no_grad():
|
| 20 |
outputs = model(**inputs)
|
|
@@ -28,9 +29,9 @@ def classify_image(image):
|
|
| 28 |
|
| 29 |
return label
|
| 30 |
|
| 31 |
-
# Create Gradio interface
|
| 32 |
-
iface = gr.Interface(fn=classify_image,
|
| 33 |
-
inputs=gr.Image(), #
|
| 34 |
outputs=gr.Label(),
|
| 35 |
title="Waste Classification with ViT",
|
| 36 |
description="Upload an image of waste, and the model will classify it.")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import ViTForImageClassification, ViTFeatureExtractor
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
# Load the pre-trained model and preprocessor (feature extractor)
|
| 8 |
model_name = "jjuarez/Vit_waste_image_class"
|
| 9 |
+
model = ViTForImageClassification.from_pretrained(model_name)
|
| 10 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
|
| 11 |
|
| 12 |
def classify_image(image):
|
| 13 |
+
# Convert the PIL Image to a format compatible with the feature extractor
|
| 14 |
image = np.array(image)
|
| 15 |
|
| 16 |
+
# Preprocess the image and prepare it for the model
|
| 17 |
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 18 |
+
|
| 19 |
# Make prediction
|
| 20 |
with torch.no_grad():
|
| 21 |
outputs = model(**inputs)
|
|
|
|
| 29 |
|
| 30 |
return label
|
| 31 |
|
| 32 |
+
# Create Gradio interface
|
| 33 |
+
iface = gr.Interface(fn=classify_image,
|
| 34 |
+
inputs=gr.Image(), # Accepts image of any size
|
| 35 |
outputs=gr.Label(),
|
| 36 |
title="Waste Classification with ViT",
|
| 37 |
description="Upload an image of waste, and the model will classify it.")
|