Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
3 |
+
|
4 |
+
dataset = load_dataset("beans")
|
5 |
+
|
6 |
+
extractor = AutoFeatureExtractor.from_pretrained("saved_model_files")
|
7 |
+
model = AutoModelForImageClassification.from_pretrained("saved_model_files")
|
8 |
+
|
9 |
+
labels = dataset['train'].features['labels'].names
|
10 |
+
|
11 |
+
def classify(im):
|
12 |
+
features = image_processor(im, return_tensors='pt')
|
13 |
+
logits = model(features["pixel_values"])[-1]
|
14 |
+
probability = torch.nn.functional.softmax(logits, dim=-1)
|
15 |
+
probs = probability[0].detach().numpy()
|
16 |
+
confidences = {label: float(probs[i]) for i, label in enumerate(labels)}
|
17 |
+
return confidences
|
18 |
+
|
19 |
+
import gradio as gr
|
20 |
+
|
21 |
+
interface = gr.Interface(
|
22 |
+
fn = classify,
|
23 |
+
inputs = "image",
|
24 |
+
outputs = "label",
|
25 |
+
interpretation = "default",
|
26 |
+
# interpretation ="shap", Shapley didn't work for me but default does
|
27 |
+
# num_shap = 5,
|
28 |
+
title= "Bean Image Classifier",
|
29 |
+
description = "A simple image classifier for bean diseases. Upload an image of a bean leaf to get started.",
|
30 |
+
examples = [
|
31 |
+
["https://media.istockphoto.com/id/472954806/photo/bean-leaf-heart-shape.jpg?s=170667a&w=0&k=20&c=es-jmKQSZLwKLU8NtVZ8KyBVoMNx6rHhW7NBw93EqJw="],
|
32 |
+
["https://d3qz1qhhp9wxfa.cloudfront.net/growingproduce/wp-content/uploads/2020/11/common_bacterial_blight_of_beans_featured.jpg"],
|
33 |
+
["https://3.bp.blogspot.com/-1FSsbPueH5Y/U8IyG9LY3VI/AAAAAAAADu0/Y5HfcKuJ5-w/s1600/8040277776_036e43f2fd_z.jpg"],
|
34 |
+
["https://plantwiseplusknowledgebank.org/cms/10.1079/pwkb.species.40010/asset/dbf6c9b1-c370-4a46-85db-4da543a13e98/assets/graphic/angular%20leaf%20spot%20(phaeoisariopsis%20griseola)%20on%20pole%20bean%20%20leaves.jpg"]
|
35 |
+
|
36 |
+
]
|
37 |
+
|
38 |
+
)
|
39 |
+
|
40 |
+
interface.launch(debug=True)
|