Daleef Rahman commited on
Commit
e88ba5b
·
1 Parent(s): 57ff6a4

more changes

Browse files
Files changed (2) hide show
  1. app.py +38 -14
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,23 +1,47 @@
1
- from fastai.vision.all import *
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import gradio as gr
3
 
4
- learn = load_learner('model.pkl')
5
- categories = ('Black', 'Grizzly', 'Teddy')
 
 
 
 
 
 
 
 
 
 
6
 
7
  def classify_image(img):
 
8
  pred, idx, probs = learn.predict(img)
9
- return dict(zip(categories, map(float, probs)))
10
-
11
- # Modern API (Gradio 4.x)
12
- image = gr.Image(type="pil", height=192, width=192, label="Upload an Image")
13
- label = gr.Label()
14
- examples = ['black.jpg', 'bear.jpg', 'teddy.jpg']
15
 
16
- intf = gr.Interface(
17
  fn=classify_image,
18
- inputs=image,
19
- outputs=label,
20
- examples=examples
 
 
 
21
  )
22
 
23
- intf.launch()
 
 
1
+ # app.py
2
+ import os, shutil
3
+
4
+ # keep all caches ephemeral
5
+ os.environ["HF_HOME"]="/tmp/hf"
6
+ os.environ["TRANSFORMERS_CACHE"]="/tmp/hf/transformers"
7
+ os.environ["TORCH_HOME"]="/tmp/torch"
8
+ os.environ["PIP_NO_CACHE_DIR"]="1"
9
+ for d in ["/home/user/.cache", "/home/user/.fastai", "/home/user/.torch"]:
10
+ shutil.rmtree(d, ignore_errors=True)
11
+
12
+ from functools import lru_cache
13
+ from huggingface_hub import hf_hub_download
14
+ from fastai.vision.all import load_learner
15
  import gradio as gr
16
 
17
+ REPO_ID = "daleef/my-fastai-bear" # your model repo
18
+ FILENAME = "model.pkl"
19
+
20
+ @lru_cache(maxsize=1)
21
+ def get_learner():
22
+ pkl_path = hf_hub_download(
23
+ repo_id=REPO_ID,
24
+ filename=FILENAME,
25
+ local_dir="/tmp/model",
26
+ local_dir_use_symlinks=False
27
+ )
28
+ return load_learner(pkl_path)
29
 
30
  def classify_image(img):
31
+ learn = get_learner()
32
  pred, idx, probs = learn.predict(img)
33
+ classes = learn.dls.vocab
34
+ return {c: float(probs[i]) for i, c in enumerate(classes)}
 
 
 
 
35
 
36
+ demo = gr.Interface(
37
  fn=classify_image,
38
+ inputs=gr.Image(type="pil", height=192, width=192, label="Upload an image"),
39
+ outputs=gr.Label(num_top_classes=3),
40
+ title="Fastai Bear Classifier",
41
+ description="Upload a bear image to classify."
42
+ # Tip: remove examples unless those files exist in the repo
43
+ # examples=["black.jpg","bear.jpg","teddy.jpg"]
44
  )
45
 
46
+ if __name__ == "__main__":
47
+ demo.launch()
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  fastai
2
- gradio
 
 
1
  fastai
2
+ gradio
3
+ huggingface_hub