Spaces:
Sleeping
Sleeping
update the code with a custom_loead_learner
Browse files
app.py
CHANGED
|
@@ -1,8 +1,24 @@
|
|
| 1 |
from fastai.vision.all import *
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
def is_cat(x): return x[0].isupper()
|
| 4 |
|
| 5 |
-
learn =
|
| 6 |
|
| 7 |
categories = ('dog', 'cat')
|
| 8 |
|
|
|
|
| 1 |
from fastai.vision.all import *
|
| 2 |
import gradio as gr
|
| 3 |
+
import pathlib
|
| 4 |
+
import fastai.learner
|
| 5 |
+
|
| 6 |
+
def custom_load_learner(fname, cpu=True, pickle_module=pickle):
|
| 7 |
+
"""Load a Learner from file in `fname` and ensure it's using a platform-independent path."""
|
| 8 |
+
map_loc = None if torch.cuda.is_available() and not cpu else 'cpu'
|
| 9 |
+
try:
|
| 10 |
+
res = torch.load(fname, map_location=map_loc, pickle_module=pickle_module)
|
| 11 |
+
except ModuleNotFoundError as e:
|
| 12 |
+
raise ImportError(f"{e}. To load the model on a different device, you may need to install the fastai library.")
|
| 13 |
+
|
| 14 |
+
if 'WindowsPath' in str(type(res.path)):
|
| 15 |
+
res.path = pathlib.Path(res.path)
|
| 16 |
+
|
| 17 |
+
return res
|
| 18 |
+
|
| 19 |
def is_cat(x): return x[0].isupper()
|
| 20 |
|
| 21 |
+
learn = custom_load_learner('model.pkl')
|
| 22 |
|
| 23 |
categories = ('dog', 'cat')
|
| 24 |
|