fmagot01 commited on
Commit
dec0f7f
·
1 Parent(s): 9d117f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py CHANGED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import pipeline
4
+ from timeit import default_timer as timer
5
+
6
+ username = "fmagot01" ## Complete your username
7
+ model_id = f"{username}/vit-base-beans"
8
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
9
+ pipe = pipeline("image-classification", model=model_id, device=device)
10
+
11
+ # def predict_trunc(filepath):
12
+ # preprocessed = pipe.preprocess(filepath)
13
+ # truncated = pipe.feature_extractor.pad(preprocessed,truncation=True, max_length = 16_000*30)
14
+ # model_outputs = pipe.forward(truncated)
15
+ # outputs = pipe.postprocess(model_outputs)
16
+
17
+ # return outputs
18
+
19
+
20
+ def classify_image(filepath):
21
+ """
22
+ Goes from
23
+ [{'score': 0.8339303731918335, 'label': 'healthy'},
24
+ {'score': 0.11914275586605072, 'label': 'bean_rust'},]
25
+ to
26
+ {"health": 0.8339303731918335, "bean_rust":0.11914275586605072}
27
+ """
28
+ start_time = timer()
29
+ preds = pipe(filepath)
30
+
31
+ outputs = {}
32
+ pred_time = round(timer() - start_time, 5)
33
+ for p in preds:
34
+ outputs[p["label"]] = p["score"]
35
+ return outputs, pred_time
36
+
37
+
38
+ title = "Classifier of Leaf Images"
39
+ description = """
40
+ This demo shows the application of the fintuned image classification model using [Beans](https://huggingface.co/datasets/beans). You can upload your own image or select an image from the examples below.
41
+
42
+ It will output 3 different labels: Healthy, Bean Rust and Angular leaf Spot. Bean rust is a type of disease that leaves can get. Angular leaf spot refers to irregular spots that a leaf can get (not a disease) and healthy leaves do not have any of these.
43
+ """
44
+
45
+ filenames = ['leaftest1.jpeg', "leaftest2.jpeg", "leaftest3.jpeg"]
46
+ filenames = [[f"./{f}"] for f in filenames]
47
+ demo = gr.Interface(
48
+ fn=classify_image,
49
+ inputs=gr.Image(type="filepath"),
50
+ outputs=[gr.outputs.Label(label="Predictions"),
51
+ gr.Number(label="Prediction time (s)")
52
+ ],
53
+ title=title,
54
+ description=description,
55
+ examples=filenames,
56
+ )
57
+ demo.launch()