Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
3 |
+
|
4 |
+
# This should be the same as the first line of Python code in this Colab notebook
|
5 |
+
dataset = load_dataset('beans')
|
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 = feature_extractor(im, return_tensors='pt')
|
13 |
+
inp = model(**features)
|
14 |
+
logits = torch.nn.functional.softmax(inp.logits, dim=-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(fn=classify, inputs=gr.Image(shape=(224, 224)), outputs="text")
|
23 |
+
|
24 |
+
interface.launch(debug=True)
|