Amitai commited on
Commit
67bd269
Β·
1 Parent(s): 4e92234

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from joblib import load
3
+ import torch
4
+ import clip
5
+ from PIL import Image
6
+ from sklearn.linear_model import LogisticRegression
7
+ from torch.utils.data import DataLoader
8
+ from tqdm import tqdm
9
+ import torchvision
10
+ import numpy as np
11
+
12
+ CLF_FILENAME = 'lr-model.pkl'
13
+ clf = load(CLF_FILENAME)
14
+
15
+ # Load the model
16
+ device = "cuda" if torch.cuda.is_available() else "cpu"
17
+ model, preprocess = clip.load('ViT-B/32', device)
18
+
19
+ def classify_image(img):
20
+ #inp = img.reshape((-1, 64, 64, 3))
21
+ im = Image.fromarray(img, mode="RGB")
22
+ image_pre_process = [preprocess(im)]
23
+ image_input = torch.tensor(np.stack(image_pre_process)).to(device)
24
+ with torch.no_grad():
25
+ image_features = model.encode_image(image_input)
26
+ image_data = image_features.cpu().numpy()
27
+
28
+ pred = clf.predict(image_data)
29
+ outputs = {0: '🌱 Biodegradable', 1: 'πŸ’€ Non-biodegradable'}
30
+ return outputs[int(pred >= 0.5)]
31
+
32
+ image = gr.inputs.Image(shape=(64,64))
33
+ iface = gr.Interface(fn=classify_image, inputs=image, outputs="text", interpretation="default", examples=["bananas.jpeg", "bio.jpeg", "nonbio.jpeg", "plastics.jpeg"])
34
+ iface.launch(debug=True)