Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,16 @@
|
|
1 |
-
import
|
2 |
-
from transformers import pipeline
|
3 |
from PIL import Image
|
|
|
4 |
|
5 |
-
|
|
|
6 |
|
7 |
-
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
col1.image(image, use_column_width=True)
|
16 |
-
predictions = pipeline(image)
|
17 |
-
|
18 |
-
col2.header("Probabilities")
|
19 |
-
for p in predictions:
|
20 |
-
col2.subheader(f"{ p['label'] }: { round(p['score'] * 100, 1)}%")
|
|
|
1 |
+
from transformers import ViTImageProcessor, ViTForImageClassification
|
|
|
2 |
from PIL import Image
|
3 |
+
import requests
|
4 |
|
5 |
+
url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
|
6 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
7 |
|
8 |
+
processor = ViTImageProcessor.from_pretrained('google/vit-base-patch16-224')
|
9 |
+
model = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224')
|
10 |
|
11 |
+
inputs = processor(images=image, return_tensors="pt")
|
12 |
+
outputs = model(**inputs)
|
13 |
+
logits = outputs.logits
|
14 |
+
# model predicts one of the 1000 ImageNet classes
|
15 |
+
predicted_class_idx = logits.argmax(-1).item()
|
16 |
+
print("Predicted class:", model.config.id2label[predicted_class_idx])
|
|
|
|
|
|
|
|
|
|
|
|