Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
from PIL import Image
|
4 |
+
from io import BytesIO
|
5 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
6 |
+
|
7 |
+
preprocessor = AutoImageProcessor.from_pretrained("hjay/autotrain-z7ygf-g8xy8")
|
8 |
+
model = AutoModelForImageClassification.from_pretrained("hjay/autotrain-z7ygf-g8xy8")
|
9 |
+
|
10 |
+
def predict(img_url):
|
11 |
+
response = requests.get(img_url)
|
12 |
+
input_img = Image.open(BytesIO(response.content))
|
13 |
+
|
14 |
+
inputs = preprocessor(images=input_img, return_tensors="pt")
|
15 |
+
outputs = model(**inputs)
|
16 |
+
|
17 |
+
logits = outputs.logits
|
18 |
+
predicted_class_idx = logits.argmax(-1).item()
|
19 |
+
|
20 |
+
return input_img, model.config.id2label[predicted_class_idx]
|
21 |
+
|
22 |
+
gradio_app = gr.Interface(
|
23 |
+
predict,
|
24 |
+
inputs="textbox",
|
25 |
+
outputs=[gr.Image(label="Processed Image"), gr.Label(label="Result", num_top_classes=2)],
|
26 |
+
title="Cat Or Dog?",
|
27 |
+
)
|
28 |
+
|
29 |
+
if __name__ == "__main__":
|
30 |
+
gradio_app.launch()
|