MetaMysteries8 commited on
Commit
21be2de
·
1 Parent(s): 6e41f0a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torchvision.transforms as transforms
3
+ from torchvision import models
4
+ from PIL import Image
5
+
6
+ # Load a pre-trained ResNet model
7
+ model = models.resnet50(pretrained=True)
8
+ model.eval()
9
+ transform = transforms.Compose([transforms.Resize(256),
10
+ transforms.CenterCrop(224),
11
+ transforms.ToTensor(),
12
+ transforms.Normalize(mean=[0.485, 0.456, 0.406],
13
+ std=[0.229, 0.224, 0.225])])
14
+
15
+ # Define a function to classify an image
16
+ def classify_image(input_image):
17
+ img = Image.open(input_image)
18
+ img = transform(img).unsqueeze(0)
19
+ with torch.no_grad():
20
+ outputs = model(img)
21
+ _, predicted_class = outputs.max(1)
22
+ return class_names[predicted_class.item()]
23
+
24
+ # Create a Gradio interface
25
+ iface = gr.Interface(
26
+ fn=classify_image,
27
+ inputs=gr.inputs.Image(type="file", label="Upload an Image"),
28
+ outputs=gr.outputs.Textbox(label="Predicted Class"),
29
+ live=True,
30
+ theme="default",
31
+ title="Image Classification with ResNet",
32
+ )
33
+
34
+ # Launch the Gradio interface
35
+ iface.launch()
36
+