Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torchvision.transforms as transforms
|
| 3 |
+
from torchvision.models import resnet50
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# Load the pre-trained model (ResNet50)
|
| 8 |
+
model = resnet50(pretrained=True)
|
| 9 |
+
model.eval()
|
| 10 |
+
|
| 11 |
+
# Define the transforms
|
| 12 |
+
transform = transforms.Compose([
|
| 13 |
+
transforms.Resize((224, 224)), # Resize to the size expected by ResNet
|
| 14 |
+
transforms.ToTensor(),
|
| 15 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 16 |
+
])
|
| 17 |
+
|
| 18 |
+
# Define the prediction function
|
| 19 |
+
def predict(image):
|
| 20 |
+
image = Image.fromarray(image.astype('uint8'), 'RGB')
|
| 21 |
+
image = transform(image).unsqueeze(0)
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
outputs = model(image)
|
| 24 |
+
_, predicted = torch.max(outputs, 1)
|
| 25 |
+
return predicted.item()
|
| 26 |
+
|
| 27 |
+
# Create and launch the Gradio interface
|
| 28 |
+
iface = gr.Interface(fn=predict, inputs="image", outputs="label")
|
| 29 |
+
iface.launch()
|