Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"id": "af9bff8c",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"from fastai.vision.all import*\n",
|
11 |
+
"import gradio as gr\n",
|
12 |
+
"learn1 = load_learner('stage1.pkl')\n",
|
13 |
+
"learn2 = load_learner('stage2.pkl')\n",
|
14 |
+
"demo = gr.Blocks()\n",
|
15 |
+
"\n",
|
16 |
+
"categories1 = 'discarded clothing', 'food waste', 'plastic bags', 'recyc_no_scrap', 'scrap metal piece', 'wood scraps'\n",
|
17 |
+
"categories2 = 'HDPE container', 'PET plastic bottle', 'aluminium can', 'cardboard', 'glass', 'paper2D', 'paper3D', 'steel and tin cans'\n",
|
18 |
+
"categories1_str = \"Stage 1 categories: \"+\", \".join(categories1)\n",
|
19 |
+
"categories2_str = \"Stage 2 categories: \"+\", \".join(categories2)\n",
|
20 |
+
"placeholder_=\"Stages 1 and 2 of the Recycling Process\\n\"+categories1_str+\"\\n\"+categories2_str\n",
|
21 |
+
"\n",
|
22 |
+
"image1 = gr.inputs.Image(shape=(192,192))\n",
|
23 |
+
"label1 = gr.outputs.Label()\n",
|
24 |
+
"examples1 = ['stage1ex1_t.jpeg', 'stage1ex2_t.jpeg','stage1ex3_t.jpeg','stage1ex4_t.jpeg', 'stage1ex5_t.jpeg','stage1ex6_t.jpeg']\n",
|
25 |
+
"\n",
|
26 |
+
"\n",
|
27 |
+
"image2 = gr.inputs.Image(shape=(192,192))\n",
|
28 |
+
"label2 = gr.outputs.Label()\n",
|
29 |
+
"examples2 = ['stage2ex1_t.jpeg', 'stage2ex2_t.jpeg','stage2ex3_t.jpeg', 'stage2ex4_t.jpeg','stage2ex5_t.jpeg',\n",
|
30 |
+
" 'stage2ex6_tt.jpeg','stage2ex7_tt.jpeg','stage2ex8_t.jpeg']\n",
|
31 |
+
"\n",
|
32 |
+
"\n",
|
33 |
+
"def classify_stage1(img):\n",
|
34 |
+
" pred, idx, probs = learn1.predict(img)\n",
|
35 |
+
" return dict(zip(categories1, map(float,probs)))\n",
|
36 |
+
"def classify_stage2(img):\n",
|
37 |
+
" pred, idx, probs = learn2.predict(img)\n",
|
38 |
+
" return dict(zip(categories2, map(float,probs)))\n",
|
39 |
+
"\n",
|
40 |
+
"\n",
|
41 |
+
"\n",
|
42 |
+
"with demo:\n",
|
43 |
+
" gr.Markdown(placeholder_)\n",
|
44 |
+
" with gr.Tabs():\n",
|
45 |
+
" with gr.TabItem(\"Stage 1\"):\n",
|
46 |
+
" with gr.Row():\n",
|
47 |
+
" nxt1 = random.choice(examples1)\n",
|
48 |
+
" stage1_input = gr.Image(nxt1)\n",
|
49 |
+
" stage1_output = gr.Label()\n",
|
50 |
+
" \n",
|
51 |
+
" stage1_button = gr.Button(\"Categorize Stage 1 Item\")\n",
|
52 |
+
" \n",
|
53 |
+
" \n",
|
54 |
+
" \n",
|
55 |
+
" with gr.TabItem(\"Stage2\"):\n",
|
56 |
+
" with gr.Row():\n",
|
57 |
+
" stage2_input = gr.Image(random.choice(examples2))\n",
|
58 |
+
" stage2_output = gr.Label()\n",
|
59 |
+
" \n",
|
60 |
+
" stage2_button = gr.Button(\"Categorize Stage 2 Item\")\n",
|
61 |
+
"\n",
|
62 |
+
" stage1_button.click(classify_stage1, inputs=stage1_input, outputs=stage1_output)#, examples = examples1)\n",
|
63 |
+
" stage2_button.click(classify_stage2, inputs=stage2_input, outputs=stage2_output)#, examples = examples2)\n",
|
64 |
+
"\n",
|
65 |
+
"demo.launch()"
|
66 |
+
]
|
67 |
+
}
|
68 |
+
],
|
69 |
+
"metadata": {
|
70 |
+
"kernelspec": {
|
71 |
+
"display_name": "Python 3 (ipykernel)",
|
72 |
+
"language": "python",
|
73 |
+
"name": "python3"
|
74 |
+
},
|
75 |
+
"language_info": {
|
76 |
+
"codemirror_mode": {
|
77 |
+
"name": "ipython",
|
78 |
+
"version": 3
|
79 |
+
},
|
80 |
+
"file_extension": ".py",
|
81 |
+
"mimetype": "text/x-python",
|
82 |
+
"name": "python",
|
83 |
+
"nbconvert_exporter": "python",
|
84 |
+
"pygments_lexer": "ipython3",
|
85 |
+
"version": "3.10.4"
|
86 |
+
},
|
87 |
+
"toc": {
|
88 |
+
"base_numbering": 1,
|
89 |
+
"nav_menu": {},
|
90 |
+
"number_sections": true,
|
91 |
+
"sideBar": true,
|
92 |
+
"skip_h1_title": false,
|
93 |
+
"title_cell": "Table of Contents",
|
94 |
+
"title_sidebar": "Contents",
|
95 |
+
"toc_cell": false,
|
96 |
+
"toc_position": {},
|
97 |
+
"toc_section_display": true,
|
98 |
+
"toc_window_display": false
|
99 |
+
}
|
100 |
+
},
|
101 |
+
"nbformat": 4,
|
102 |
+
"nbformat_minor": 5
|
103 |
+
}
|