Spaces:
Runtime error
Runtime error
paschalc24 commited on
Commit ·
8121a75
1
Parent(s): 0cb1f5e
Add ImageRecognitionDemo App
Browse files
app.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification, AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
image_model = AutoModelForImageClassification.from_pretrained("google/vit-base-patch16-224")
|
| 6 |
+
extractor = AutoFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
|
| 7 |
+
|
| 8 |
+
image_pipe = pipeline("image-classification", model=image_model, feature_extractor=extractor)
|
| 9 |
+
|
| 10 |
+
# classify the image and returns the results
|
| 11 |
+
def classify_image(inp):
|
| 12 |
+
results = image_pipe(inp)
|
| 13 |
+
return {result["label"]: result["score"] for result in results}
|
| 14 |
+
|
| 15 |
+
gr.Interface(fn=classify_image,
|
| 16 |
+
inputs=gr.Image(type="pil"),
|
| 17 |
+
outputs=gr.Label(num_top_classes=5)).launch()
|