capjamesg commited on
Commit
023c2b7
1 Parent(s): f26a358
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tempfile
2
+
3
+ import gradio as gr
4
+ from autodistill_fastvit import FASTVIT_IMAGENET_1K_CLASSES, FastViT
5
+ from PIL import Image
6
+
7
+ base_model = FastViT(None)
8
+
9
+
10
+ def infer(image):
11
+ with tempfile.NamedTemporaryFile(suffix=".jpg") as temp:
12
+ image = Image.fromarray(image.astype("uint8"), "RGB")
13
+
14
+ image.save(temp.name)
15
+
16
+ predictions = base_model.predict(temp.name, confidence=0.1)
17
+
18
+ labels = [FASTVIT_IMAGENET_1K_CLASSES[i] for i in predictions.class_id.tolist()]
19
+ confidences = predictions.confidence.tolist()
20
+
21
+ # divide by 100 to convert to percentage
22
+ confidences = [c / 100 for c in confidences]
23
+
24
+ return {
25
+ k: v
26
+ for k, v in zip(labels, confidences)
27
+ }
28
+
29
+
30
+ iface = gr.Interface(
31
+ fn=infer,
32
+ inputs="image",
33
+ outputs="label",
34
+ allow_flagging=False,
35
+ title="FastViT",
36
+ description="[FastViT](https://github.com/apple/ml-fastvit) is a fast Vision Transformer developed by Apple. FastViT was trained on the ImageNet-1k dataset.\n\nUse the space below to test FastViT on your own images.\n\nThis space uses [Autodistill FastViT](https://github.com/autodistill/autodistill-fastvit) for inference.",
37
+ )
38
+ iface.launch()