Create app.py
Browse files
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-200-birds-10-per-cent")
|
| 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 = "Bad castle predictor"
|
| 29 |
+
description = "A bad model that tries to identify the type of castle."
|
| 30 |
+
examples = ['american_crow.jpg', 'barn_swallow.jpg', '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)
|