File size: 878 Bytes
59078f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import torch
import torchvision.transforms as transforms
from torchvision.models import resnet50
from PIL import Image
import gradio as gr

# Load the pre-trained model (ResNet50)
model = resnet50(pretrained=True)
model.eval()

# Define the transforms
transform = transforms.Compose([
    transforms.Resize((224, 224)),  # Resize to the size expected by ResNet
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

# Define the prediction function
def predict(image):
    image = Image.fromarray(image.astype('uint8'), 'RGB')
    image = transform(image).unsqueeze(0)
    with torch.no_grad():
        outputs = model(image)
        _, predicted = torch.max(outputs, 1)
    return predicted.item()

# Create and launch the Gradio interface
iface = gr.Interface(fn=predict, inputs="image", outputs="label")
iface.launch()