jafdxc commited on
Commit
4a9c73f
1 Parent(s): a8c84af

Application

Browse files
Files changed (2) hide show
  1. app.py +25 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from datasets import load_dataset
3
+ from transformers import AutoFeatureExtractor, AutoModelForImageClassification
4
+
5
+ # This should be the same as the first line of Python code in this Colab notebook
6
+ dataset = load_dataset('beans')
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 = feature_extractor(im, return_tensors='pt')
14
+ inp = model(**features)
15
+ logits = torch.nn.functional.softmax(inp.logits, dim=-1)
16
+ probability = torch.nn.functional.softmax(logits, dim=-1)
17
+ probs = probability[0].detach().numpy()
18
+ confidences = {label: float(probs[i]) for i, label in enumerate(labels)}
19
+ return confidences
20
+
21
+ import gradio as gr
22
+
23
+ interface = gr.Interface(fn=classify, inputs=gr.Image(shape=(224, 224)), outputs="text")
24
+
25
+ interface.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
1
+ torch
2
+ transformers