Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
from transformers import pipeline, AutoImageProcessor, Swinv2ForImageClassification
|
| 3 |
from torchvision import transforms
|
| 4 |
import torch
|
|
|
|
| 5 |
|
| 6 |
# Ensure using GPU if available
|
| 7 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
@@ -17,12 +18,11 @@ class_names = ['artificial', 'real']
|
|
| 17 |
|
| 18 |
def predict_image(img, confidence_threshold):
|
| 19 |
# Convert the image to a PIL Image and resize it
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
img = transforms.ToTensor()(img).unsqueeze(0).to(device) # Add batch dimension and move to GPU
|
| 23 |
|
| 24 |
# Get the prediction
|
| 25 |
-
prediction = clf(
|
| 26 |
|
| 27 |
# Process the prediction to match the class names
|
| 28 |
result = {pred['label']: pred['score'] for pred in prediction}
|
|
@@ -41,7 +41,7 @@ def predict_image(img, confidence_threshold):
|
|
| 41 |
return "Uncertain Classification"
|
| 42 |
|
| 43 |
# Define the Gradio interface
|
| 44 |
-
image = gr.Image(label="Image to Analyze", sources=['upload'])
|
| 45 |
confidence_slider = gr.Slider(0.0, 1.0, value=0.5, step=0.01, label="Confidence Threshold")
|
| 46 |
label = gr.Label(num_top_classes=2)
|
| 47 |
|
|
|
|
| 2 |
from transformers import pipeline, AutoImageProcessor, Swinv2ForImageClassification
|
| 3 |
from torchvision import transforms
|
| 4 |
import torch
|
| 5 |
+
from PIL import Image
|
| 6 |
|
| 7 |
# Ensure using GPU if available
|
| 8 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
|
|
| 18 |
|
| 19 |
def predict_image(img, confidence_threshold):
|
| 20 |
# Convert the image to a PIL Image and resize it
|
| 21 |
+
img_pil = Image.fromarray(img).convert('RGB') # Convert NumPy array to PIL Image
|
| 22 |
+
img_pil = transforms.Resize((256, 256))(img_pil)
|
|
|
|
| 23 |
|
| 24 |
# Get the prediction
|
| 25 |
+
prediction = clf(img_pil)
|
| 26 |
|
| 27 |
# Process the prediction to match the class names
|
| 28 |
result = {pred['label']: pred['score'] for pred in prediction}
|
|
|
|
| 41 |
return "Uncertain Classification"
|
| 42 |
|
| 43 |
# Define the Gradio interface
|
| 44 |
+
image = gr.Image(label="Image to Analyze", sources=['upload'], type='pil') # Ensure the image type is PIL
|
| 45 |
confidence_slider = gr.Slider(0.0, 1.0, value=0.5, step=0.01, label="Confidence Threshold")
|
| 46 |
label = gr.Label(num_top_classes=2)
|
| 47 |
|