m4gnu5 commited on
Commit
3f9313f
·
1 Parent(s): 94d26a2

Cats and dogs

Browse files
app.py CHANGED
@@ -1,7 +1,70 @@
 
1
  import gradio as gr
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastai.vision.all import *
2
  import gradio as gr
3
 
4
+ def is_cat(x):
5
+ return x[0].isupper()
6
 
7
+ learn = load_learner('model.pkl')
8
+
9
+ categories = ('Cat', 'Dog')
10
+
11
+ prompts = [
12
+ "# Definitely a {}!",
13
+ "# Well, that must be a {}!",
14
+ "# Oh, that's a {}!",
15
+ "# That's a {}!",
16
+ "# Looks like a {} to me!",
17
+ ]
18
+
19
+ failure_prompts = [
20
+ "# I'm not sure what that is.",
21
+ "# I don't know what that thing is.",
22
+ "# I've never seen that before.",
23
+ "# Looks familiar, but unsure.",
24
+ "# Something, something?",
25
+ "# Beats me.",
26
+ ]
27
+
28
+ def classify_image(img):
29
+ pred,idx,probs = learn.predict(img)
30
+ return dict(zip(categories, map(float,probs)))
31
+
32
+ def calculate(confidence_threshold, img):
33
+ classifications = classify_image(img)
34
+ classification = random.choice(failure_prompts)
35
+ for key, value in classifications.items():
36
+ if value > confidence_threshold:
37
+ classification = random.choice(prompts).format(key)
38
+ break
39
+
40
+ return [classification, classifications]
41
+
42
+
43
+ with gr.Blocks() as ui:
44
+
45
+ heading = gr.Markdown(" # Dog or Cat?", render=False)
46
+ results = gr.Label(value="Waiting to receive image.", label="Details", show_label=False, render=False)
47
+
48
+ with gr.Row(equal_height=True):
49
+
50
+ with gr.Column():
51
+ gr.Markdown("Upload an image of a cat or a dog.")
52
+
53
+ with gr.Group():
54
+ image = gr.Image(show_label=False, height=300)
55
+ confidence = gr.Slider(minimum=0.0, maximum=1.0, value=0.7, label="Confidence Threshold")
56
+ btn = gr.Button(value="Classify")
57
+ btn.click(calculate, inputs=[confidence, image], outputs=[heading, results])
58
+
59
+ with gr.Column():
60
+ gr.Markdown("Then wait for the magic to happen")
61
+ with gr.Group():
62
+ results.render()
63
+ heading.render()
64
+
65
+ gr.Markdown(" # Examples")
66
+ with gr.Group():
67
+ gr.Examples(inputs=image, examples=['images/cat1.jpeg', 'images/cat2.jpeg', 'images/cat3.jpeg', 'images/dog1.jpeg', 'images/dog2.jpeg', 'images/dog3.jpeg'])
68
+
69
+ if __name__ == "__main__":
70
+ ui.launch()
images/cat1.jpeg ADDED
images/cat2.jpeg ADDED
images/cat3.jpeg ADDED
images/dog1.jpeg ADDED
images/dog2.jpeg ADDED
images/dog3.jpeg ADDED
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ fastai
2
+ gradio