Spaces:
Runtime error
Runtime error
premputtegowda
commited on
Commit
•
df54842
1
Parent(s):
0d34e27
Updated from colab
Browse files- app.py +28 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
import torch
|
3 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
4 |
+
|
5 |
+
dataset = datasets.load_dataset('beans') # This should be the same as the first line of Python code in this Colab notebook
|
6 |
+
|
7 |
+
feature_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 = feature_extractor(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 |
+
import gradio as gr
|
21 |
+
|
22 |
+
interface = gr.Interface(
|
23 |
+
classify,
|
24 |
+
inputs='image',
|
25 |
+
outputs='label',
|
26 |
+
)
|
27 |
+
|
28 |
+
interface.launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|