Spaces:
Sleeping
Sleeping
Commit
·
3ecd966
1
Parent(s):
5243167
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
4 |
+
|
5 |
+
dataset = load_dataset('beans') # This should be the same as the first line of Python code in this Colab notebook
|
6 |
+
|
7 |
+
extractor = AutoFeatureExtractor.from_pretrained("saved_model_files")
|
8 |
+
model = AutoModelForImageClassification.from_pretrained("saved_model_files")
|
9 |
+
|
10 |
+
labels = dataset['train'].features['labels'].names
|
11 |
+
|
12 |
+
def classify(im):
|
13 |
+
features = image_processor(im, return_tensors='pt')
|
14 |
+
logits = model(features["pixel_values"])[-1]
|
15 |
+
probability = torch.nn.functional.softmax(logits, dim=-1)
|
16 |
+
probs = probability[0].detach().numpy()
|
17 |
+
confidences = {label: float(probs[i]) for i, label in enumerate(labels)}
|
18 |
+
return confidences
|
19 |
+
|
20 |
+
# Run the Gradio interface for the app
|
21 |
+
interface = gr.Interface(
|
22 |
+
fn=classify,
|
23 |
+
inputs=["image"],
|
24 |
+
outputs=["label"],
|
25 |
+
title="Leaf disaease classifier",
|
26 |
+
description="A pre-trained vit model for classifying leaf diseases"
|
27 |
+
)
|
28 |
+
|
29 |
+
interface.launch(debug=True)
|