Pablogps commited on
Commit
618804a
·
verified ·
1 Parent(s): 1d538ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -38
app.py CHANGED
@@ -1,53 +1,20 @@
1
  import gradio as gr
2
- import traceback
3
  from fastai.vision.all import *
4
  from huggingface_hub import from_pretrained_fastai
5
  import torch, os
6
- import torchvision.transforms as T
7
 
8
  os.environ.setdefault("OMP_NUM_THREADS", "1")
9
  torch.set_num_threads(1)
10
 
11
  learn = from_pretrained_fastai("Pablogps/castle-classifier-25")
12
- try:
13
- learn.to_fp32()
14
- except Exception:
15
- pass
16
-
17
- for dl in learn.dls.loaders:
18
- try: dl.num_workers = 0
19
- except: pass
20
-
21
  labels = learn.dls.vocab
22
 
23
-
24
  def predict(img):
25
- try:
26
- print("Starting prediction...", flush=True)
27
- img = PILImage.create(img)
28
-
29
- # 2) Torchvision preprocessing:
30
- # Resize shorter side to 256, center-crop 224, ToTensor, ImageNet normalize
31
- tfm = T.Compose([
32
- T.Resize(256),
33
- T.CenterCrop(224),
34
- T.ToTensor(), # -> [C,H,W] in [0,1]
35
- T.Normalize(mean=[0.485, 0.456, 0.406],
36
- std=[0.229, 0.224, 0.225]),
37
- ])
38
- x = tfm(img).unsqueeze(0) # [1,3,224,224]
39
-
40
- # 3) Forward pass
41
- learn.model.eval()
42
- logits = learn.model(x) # [1, C]
43
- probs = torch.softmax(logits, dim=1)[0] # [C]
44
-
45
- # pred, pred_idx, probs = learn.predict(img)
46
- return {labels[i]: float(probs[i]) for i in range(len(labels))}
47
- except Exception as e:
48
- tb = traceback.format_exc()
49
- print(tb, flush=True) # goes to runtime logs even if you can't see them
50
- raise gr.Error(tb) # shows full traceback to you in the UI
51
 
52
  title = "Bad castle predictor"
53
  description = "A bad model that tries to identify the type of castle."
 
1
  import gradio as gr
 
2
  from fastai.vision.all import *
3
  from huggingface_hub import from_pretrained_fastai
4
  import torch, os
 
5
 
6
  os.environ.setdefault("OMP_NUM_THREADS", "1")
7
  torch.set_num_threads(1)
8
 
9
  learn = from_pretrained_fastai("Pablogps/castle-classifier-25")
10
+ try: learn.to_fp32()
11
+ except: pass
 
 
 
 
 
 
 
12
  labels = learn.dls.vocab
13
 
 
14
  def predict(img):
15
+ img = PILImage.create(img) # same flow as before
16
+ pred, pred_idx, probs = learn.predict(img)
17
+ return {labels[i]: float(probs[i]) for i in range(len(labels))}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  title = "Bad castle predictor"
20
  description = "A bad model that tries to identify the type of castle."