xota1999 commited on
Commit
21a5756
1 Parent(s): 47f8bd1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -42
app.py CHANGED
@@ -1,43 +1,26 @@
 
 
 
 
 
1
  import gradio as gr
2
- import pickle
3
- import numpy as np
4
- from PIL import Image
5
- import torch
6
- from torchvision import transforms
7
-
8
- # Load the pre-trained model
9
- with open('model.pkl', 'rb') as f:
10
- model = pickle.load(f)
11
-
12
- # Define the image transformations
13
- transform = transforms.Compose([
14
- transforms.Resize((224, 224)),
15
- transforms.ToTensor(),
16
- transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
17
- ])
18
-
19
- # Define the prediction function
20
- def predict(image):
21
- # Apply the transformations to the input image
22
- img = transform(image).unsqueeze(0)
23
- model.eval()
24
- with torch.no_grad():
25
- output = model(img)
26
- prediction = torch.argmax(output, dim=1).item()
27
-
28
- # Define class names
29
- class_names = ["Not Katy Perry", "Katy Perry"]
30
-
31
- return class_names[prediction]
32
-
33
- # Set up the Gradio interface
34
- iface = gr.Interface(
35
- fn=predict,
36
- inputs=gr.inputs.Image(type="pil"),
37
- outputs="text",
38
- title="Katy Perry Recognition",
39
- description="Upload an image to find out if it's Katy Perry or not.",
40
- )
41
-
42
- if __name__ == "__main__":
43
- iface.launch()
 
1
+
2
+ __all__ = ['is_katy', 'learn', 'classify_image', 'categories', 'image', 'label', 'examples', 'intf']
3
+
4
+ # Cell
5
+ from fastai.vision.all import *
6
  import gradio as gr
7
+
8
+ def is_katy(x): return x[0].isupper()
9
+
10
+ # Cell
11
+ learn = load_learner('model.pkl')
12
+
13
+ # Cell
14
+ categories = ('Katy', 'Zooey')
15
+
16
+ def classify_image(img):
17
+ pred,idx,probs = learn.predict(img)
18
+ return dict(zip(categories, map(float,probs)))
19
+
20
+ # Cell
21
+ image = gr.inputs.Image(shape=(192, 192))
22
+ label = gr.outputs.Label()
23
+ examples = ['katy.jpg', 'katy2.jpg','zooey1.jpg','zooey2.jpg','zooey3.jpg','dunno.jpg']
24
+
25
+ intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
26
+ intf.launch(inline=False)