Pablogps commited on
Commit
0ee54bb
·
verified ·
1 Parent(s): 01a4eb2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from fastai.vision.all import *
3
+ from huggingface_hub import from_pretrained_fastai
4
+ import torch, os
5
+ import traceback
6
+
7
+
8
+ os.environ.setdefault("OMP_NUM_THREADS", "1")
9
+ torch.set_num_threads(1)
10
+
11
+ learn = from_pretrained_fastai("Pablogps/kedar-15-birds")
12
+ try:
13
+ learn.to_fp32()
14
+ except:
15
+ pass
16
+ labels = learn.dls.vocab
17
+
18
+ def predict(img):
19
+ try:
20
+ # img = PILImage.create(img)
21
+ pred, pred_idx, probs = learn.predict(img)
22
+ return {labels[i]: float(probs[i]) for i in range(len(labels))}
23
+ except Exception as e:
24
+ tb = traceback.format_exc()
25
+ print(tb, flush=True)
26
+ raise gr.Error(f"{type(e).__name__}: {e}")
27
+
28
+ title = "Bird classifier"
29
+ description = "A small model that classifies birds, trained quickly with a small subset of kedar 200 using 15 classes."
30
+ examples = ['examples_american_crow.jpg', 'examples_barn_swallow.jpg', 'examples_pied_kingfisher.jpg']
31
+
32
+ demo = gr.Interface(
33
+ fn=predict,
34
+ inputs=gr.Image(type="pil"),
35
+ outputs=gr.Label(num_top_classes=3),
36
+ title=title,
37
+ description=description,
38
+ examples=examples,
39
+ cache_examples=False, # <-- don’t pre-run at startup
40
+ )
41
+
42
+ demo.queue(max_size=8).launch(show_error=True, debug=True)