Spaces:
Sleeping
Sleeping
Boilerplate
Browse files
app.py
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
gr.Interface.
|
|
|
|
| 1 |
+
from transformers import EfficientNetImageProcessor, EfficientNetForImageClassification
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
|
| 6 |
+
device = torch.device("cpu")
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
processor = EfficientNetImageProcessor.from_pretrained("models/dennisjooo/Birds-Classifier-EfficientNetB2")
|
| 10 |
+
model = EfficientNetForImageClassification.from_pretrained("models/dennisjooo/Birds-Classifier-EfficientNetB2").to(device)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def predict(image):
|
| 14 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 15 |
+
outputs = model(**inputs)
|
| 16 |
+
logits = outputs.logits
|
| 17 |
+
predicted_class_prob = F.softmax(logits, dim=-1).detach().cpu().numpy().max()
|
| 18 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 19 |
+
label = model.config.id2label[predicted_class_idx].split(",")[0]
|
| 20 |
+
return {label: float(predicted_class_prob)}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
import gradio as gr
|
| 24 |
|
| 25 |
+
gr.Interface(predict, gr.Image(type="pil"), "label").queue(concurrency_count=1).launch()
|