Spaces:
Runtime error

MAPALO LUKASHI commited on
Commit
4c3f11e
·
1 Parent(s): 885bd7a
Files changed (1) hide show
  1. app.ipynb +127 -0
app.ipynb CHANGED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 8,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "!pip install -Uqq fastbook\n",
10
+ "from fastai.vision.all import *\n",
11
+ "from fastbook import *\n",
12
+ "import fastai\n",
13
+ "import torch\n",
14
+ "from pathlib import Path\n",
15
+ "from fastdownload import download_url\n",
16
+ "import requests\n",
17
+ "from PIL import Image\n",
18
+ "from io import BytesIO\n",
19
+ "import matplotlib.pyplot as plt"
20
+ ]
21
+ },
22
+ {
23
+ "cell_type": "code",
24
+ "execution_count": 9,
25
+ "metadata": {},
26
+ "outputs": [],
27
+ "source": [
28
+ "#!pip install -Uqq fastai\n",
29
+ "#!pip install -Uqq gradio\n",
30
+ "# app.py\n",
31
+ "from fastai.vision.all import *\n",
32
+ "import gradio as gr\n"
33
+ ]
34
+ },
35
+ {
36
+ "cell_type": "code",
37
+ "execution_count": 10,
38
+ "metadata": {},
39
+ "outputs": [
40
+ {
41
+ "ename": "NotImplementedError",
42
+ "evalue": "cannot instantiate 'PosixPath' on your system",
43
+ "output_type": "error",
44
+ "traceback": [
45
+ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
46
+ "\u001b[1;31mNotImplementedError\u001b[0m Traceback (most recent call last)",
47
+ "Cell \u001b[1;32mIn[10], line 6\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;66;03m# Adjust this path to where 'export.pkl' is located on your system\u001b[39;00m\n\u001b[0;32m 5\u001b[0m model_path \u001b[38;5;241m=\u001b[39m Path(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mC:/Users/samsung/Documents/GitHub/project3/export.pkl\u001b[39m\u001b[38;5;124m'\u001b[39m)\u001b[38;5;241m.\u001b[39mresolve()\n\u001b[1;32m----> 6\u001b[0m learn \u001b[38;5;241m=\u001b[39m \u001b[43mload_learner\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mstr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mmodel_path\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n",
48
+ "File \u001b[1;32mc:\\Users\\samsung\\Documents\\GitHub\\project3\\.conda\\Lib\\site-packages\\fastai\\learner.py:446\u001b[0m, in \u001b[0;36mload_learner\u001b[1;34m(fname, cpu, pickle_module)\u001b[0m\n\u001b[0;32m 444\u001b[0m distrib_barrier()\n\u001b[0;32m 445\u001b[0m map_loc \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mcpu\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m cpu \u001b[38;5;28;01melse\u001b[39;00m default_device()\n\u001b[1;32m--> 446\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m: res \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mload\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmap_location\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmap_loc\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpickle_module\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpickle_module\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 447\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mAttributeError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e: \n\u001b[0;32m 448\u001b[0m e\u001b[38;5;241m.\u001b[39margs \u001b[38;5;241m=\u001b[39m [\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCustom classes or functions exported with your `Learner` not available in namespace.\u001b[39m\u001b[38;5;124m\\\u001b[39m\u001b[38;5;124mRe-declare/import before loading:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\t\u001b[39;00m\u001b[38;5;132;01m{\u001b[39;00me\u001b[38;5;241m.\u001b[39margs[\u001b[38;5;241m0\u001b[39m]\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m]\n",
49
+ "File \u001b[1;32mc:\\Users\\samsung\\Documents\\GitHub\\project3\\.conda\\Lib\\site-packages\\torch\\serialization.py:1026\u001b[0m, in \u001b[0;36mload\u001b[1;34m(f, map_location, pickle_module, weights_only, mmap, **pickle_load_args)\u001b[0m\n\u001b[0;32m 1024\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m 1025\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m pickle\u001b[38;5;241m.\u001b[39mUnpicklingError(UNSAFE_MESSAGE \u001b[38;5;241m+\u001b[39m \u001b[38;5;28mstr\u001b[39m(e)) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m-> 1026\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_load\u001b[49m\u001b[43m(\u001b[49m\u001b[43mopened_zipfile\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 1027\u001b[0m \u001b[43m \u001b[49m\u001b[43mmap_location\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 1028\u001b[0m \u001b[43m \u001b[49m\u001b[43mpickle_module\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 1029\u001b[0m \u001b[43m \u001b[49m\u001b[43moverall_storage\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43moverall_storage\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 1030\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mpickle_load_args\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 1031\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m mmap:\n\u001b[0;32m 1032\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmmap can only be used with files saved with \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 1033\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m`torch.save(_use_new_zipfile_serialization=True), \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 1034\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mplease torch.save your checkpoint with this option in order to use mmap.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
50
+ "File \u001b[1;32mc:\\Users\\samsung\\Documents\\GitHub\\project3\\.conda\\Lib\\site-packages\\torch\\serialization.py:1438\u001b[0m, in \u001b[0;36m_load\u001b[1;34m(zip_file, map_location, pickle_module, pickle_file, overall_storage, **pickle_load_args)\u001b[0m\n\u001b[0;32m 1436\u001b[0m unpickler \u001b[38;5;241m=\u001b[39m UnpicklerWrapper(data_file, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mpickle_load_args)\n\u001b[0;32m 1437\u001b[0m unpickler\u001b[38;5;241m.\u001b[39mpersistent_load \u001b[38;5;241m=\u001b[39m persistent_load\n\u001b[1;32m-> 1438\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[43munpickler\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mload\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 1440\u001b[0m torch\u001b[38;5;241m.\u001b[39m_utils\u001b[38;5;241m.\u001b[39m_validate_loaded_sparse_tensors()\n\u001b[0;32m 1441\u001b[0m torch\u001b[38;5;241m.\u001b[39m_C\u001b[38;5;241m.\u001b[39m_log_api_usage_metadata(\n\u001b[0;32m 1442\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtorch.load.metadata\u001b[39m\u001b[38;5;124m\"\u001b[39m, {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mserialization_id\u001b[39m\u001b[38;5;124m\"\u001b[39m: zip_file\u001b[38;5;241m.\u001b[39mserialization_id()}\n\u001b[0;32m 1443\u001b[0m )\n",
51
+ "File \u001b[1;32mc:\\Users\\samsung\\Documents\\GitHub\\project3\\.conda\\Lib\\pathlib.py:1422\u001b[0m, in \u001b[0;36mPosixPath.__new__\u001b[1;34m(cls, *args, **kwargs)\u001b[0m\n\u001b[0;32m 1421\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__new__\u001b[39m(\u001b[38;5;28mcls\u001b[39m, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[1;32m-> 1422\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m(\n\u001b[0;32m 1423\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcannot instantiate \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m!r}\u001b[39;00m\u001b[38;5;124m on your system\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
52
+ "\u001b[1;31mNotImplementedError\u001b[0m: cannot instantiate 'PosixPath' on your system"
53
+ ]
54
+ }
55
+ ],
56
+ "source": [
57
+ "# Load the trained model\n",
58
+ "from pathlib import Path\n",
59
+ "\n",
60
+ "# Adjust this path to where 'export.pkl' is located on your system\n",
61
+ "model_path = Path('C:/Users/samsung/Documents/GitHub/project3/export.pkl').resolve()\n",
62
+ "learn = load_learner(str(model_path))\n"
63
+ ]
64
+ },
65
+ {
66
+ "cell_type": "code",
67
+ "execution_count": 11,
68
+ "metadata": {},
69
+ "outputs": [],
70
+ "source": [
71
+ "categories = ('tshirt', 'trousers')\n",
72
+ "\n",
73
+ "# Define the classification function that Gradio will use\n",
74
+ "def classify_image(img):\n",
75
+ " pred, idx, probs = learn.predict(img)\n",
76
+ " # Assuming your model's predictions are 'T-shirt' and 'Trousers'\n",
77
+ " return {pred: max(probs.tolist())}"
78
+ ]
79
+ },
80
+ {
81
+ "cell_type": "code",
82
+ "execution_count": 12,
83
+ "metadata": {},
84
+ "outputs": [
85
+ {
86
+ "ename": "NameError",
87
+ "evalue": "name 'image' is not defined",
88
+ "output_type": "error",
89
+ "traceback": [
90
+ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
91
+ "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
92
+ "Cell \u001b[1;32mIn[12], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Create the interface\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m intf \u001b[38;5;241m=\u001b[39m gr\u001b[38;5;241m.\u001b[39mInterface(fn\u001b[38;5;241m=\u001b[39mclassify_image, inputs\u001b[38;5;241m=\u001b[39m\u001b[43mimage\u001b[49m, outputs\u001b[38;5;241m=\u001b[39mlabel, examples\u001b[38;5;241m=\u001b[39mexamples)\n\u001b[0;32m 4\u001b[0m \u001b[38;5;66;03m# Launch the interface\u001b[39;00m\n\u001b[0;32m 5\u001b[0m intf\u001b[38;5;241m.\u001b[39mlaunch(share\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n",
93
+ "\u001b[1;31mNameError\u001b[0m: name 'image' is not defined"
94
+ ]
95
+ }
96
+ ],
97
+ "source": [
98
+ "# Create the interface\n",
99
+ "intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)\n",
100
+ "\n",
101
+ "# Launch the interface\n",
102
+ "intf.launch(share=True)"
103
+ ]
104
+ }
105
+ ],
106
+ "metadata": {
107
+ "kernelspec": {
108
+ "display_name": "Python 3",
109
+ "language": "python",
110
+ "name": "python3"
111
+ },
112
+ "language_info": {
113
+ "codemirror_mode": {
114
+ "name": "ipython",
115
+ "version": 3
116
+ },
117
+ "file_extension": ".py",
118
+ "mimetype": "text/x-python",
119
+ "name": "python",
120
+ "nbconvert_exporter": "python",
121
+ "pygments_lexer": "ipython3",
122
+ "version": "3.12.2"
123
+ }
124
+ },
125
+ "nbformat": 4,
126
+ "nbformat_minor": 2
127
+ }