Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
| 4 |
-
from torchvision import
|
| 5 |
from PIL import Image
|
| 6 |
|
|
|
|
| 7 |
class CustomEfficientNet(nn.Module):
|
| 8 |
def __init__(self, num_classes, num_layers, neurons_per_layer):
|
| 9 |
super(CustomEfficientNet, self).__init__()
|
|
@@ -28,6 +29,7 @@ class CustomEfficientNet(nn.Module):
|
|
| 28 |
x = self.custom_classifier(x)
|
| 29 |
return x
|
| 30 |
|
|
|
|
| 31 |
def create_model(num_classes, num_layers, neurons_per_layer):
|
| 32 |
model = CustomEfficientNet(num_classes, num_layers, neurons_per_layer)
|
| 33 |
return model
|
|
@@ -62,6 +64,7 @@ class_names = ['Coeur 1', 'Coeur 10', 'Coeur 2', 'Coeur 3', 'Coeur 4', 'Coeur 5'
|
|
| 62 |
'Trefle Roi', 'Trefle Valet', 'carreau 1', 'carreau 10', 'carreau 2', 'carreau 3', 'carreau 4', 'carreau 5',
|
| 63 |
'carreau 6', 'carreau 7', 'carreau 8', 'carreau 9', 'carreau Dame', 'carreau Roi', 'carreau Valet']
|
| 64 |
|
|
|
|
| 65 |
def predict(image):
|
| 66 |
image = transform(image).unsqueeze(0)
|
| 67 |
with torch.no_grad():
|
|
@@ -69,12 +72,21 @@ def predict(image):
|
|
| 69 |
_, predicted = torch.max(outputs, 1)
|
| 70 |
return class_names[predicted[0]]
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
# Create the Gradio interface
|
| 73 |
iface = gr.Interface(
|
| 74 |
fn=predict,
|
| 75 |
inputs=gr.Image(type="pil"),
|
| 76 |
outputs="label",
|
| 77 |
-
description="Upload an image to classify"
|
|
|
|
| 78 |
)
|
| 79 |
|
|
|
|
| 80 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
| 4 |
+
from torchvision import transforms, models
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
+
# Define the model class
|
| 8 |
class CustomEfficientNet(nn.Module):
|
| 9 |
def __init__(self, num_classes, num_layers, neurons_per_layer):
|
| 10 |
super(CustomEfficientNet, self).__init__()
|
|
|
|
| 29 |
x = self.custom_classifier(x)
|
| 30 |
return x
|
| 31 |
|
| 32 |
+
# Function to create and load the model
|
| 33 |
def create_model(num_classes, num_layers, neurons_per_layer):
|
| 34 |
model = CustomEfficientNet(num_classes, num_layers, neurons_per_layer)
|
| 35 |
return model
|
|
|
|
| 64 |
'Trefle Roi', 'Trefle Valet', 'carreau 1', 'carreau 10', 'carreau 2', 'carreau 3', 'carreau 4', 'carreau 5',
|
| 65 |
'carreau 6', 'carreau 7', 'carreau 8', 'carreau 9', 'carreau Dame', 'carreau Roi', 'carreau Valet']
|
| 66 |
|
| 67 |
+
# Define the prediction function
|
| 68 |
def predict(image):
|
| 69 |
image = transform(image).unsqueeze(0)
|
| 70 |
with torch.no_grad():
|
|
|
|
| 72 |
_, predicted = torch.max(outputs, 1)
|
| 73 |
return class_names[predicted[0]]
|
| 74 |
|
| 75 |
+
# Example images
|
| 76 |
+
examples = [
|
| 77 |
+
['trefledame.jpg'],
|
| 78 |
+
['coeurroi.jpg'],
|
| 79 |
+
['coeur3.jpg']
|
| 80 |
+
]
|
| 81 |
+
|
| 82 |
# Create the Gradio interface
|
| 83 |
iface = gr.Interface(
|
| 84 |
fn=predict,
|
| 85 |
inputs=gr.Image(type="pil"),
|
| 86 |
outputs="label",
|
| 87 |
+
description="Upload an image to classify",
|
| 88 |
+
examples=examples
|
| 89 |
)
|
| 90 |
|
| 91 |
+
# Launch the interface
|
| 92 |
iface.launch()
|