hlydecker commited on
Commit
d20ea93
·
verified ·
1 Parent(s): f1162fd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -0
app.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import ViTImageProcessor, ViTForImageClassification
2
+ from PIL import Image
3
+ import torch
4
+ import torch.nn.functional as F
5
+ import time
6
+
7
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
8
+
9
+ processor = ViTImageProcessor.from_pretrained("LCZs_v2",local_files_only=True)
10
+ model = ViTForImageClassification.from_pretrained("LCZs_v2",local_files_only=True).to(device)
11
+
12
+ def predict(image):
13
+ inputs = processor(images=image, return_tensors="pt").to(device)
14
+ outputs = model(**inputs)
15
+ logits = outputs.logits
16
+ predicted_class_prob = F.softmax(logits, dim=-1).detach().cpu().numpy().max()
17
+ predicted_class_idx = logits.argmax(-1).item()
18
+ label = model.config.id2label[predicted_class_idx].split(",")[0]
19
+ time.sleep(2)
20
+ return {label: float(predicted_class_prob)}
21
+ import gradio as gr
22
+
23
+ gr.Interface(predict, gr.Image(type="pil"), "label").launch()