Spaces:
Sleeping
Sleeping
Add app,py
Browse files
app.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
model = ViTForImageClassification.from_pretrained('sreeramajay/pollution')
|
| 7 |
+
transforms = ViTFeatureExtractor.from_pretrained('sreeramajay/pollution')
|
| 8 |
+
|
| 9 |
+
def predict(image):
|
| 10 |
+
labels = {0:"Air Pollution", 1: "Land Pollution" , 2: "Water Pollution"}
|
| 11 |
+
inputs = transforms(image, return_tensors='pt')
|
| 12 |
+
output = model(**inputs)
|
| 13 |
+
probability = output.logits.softmax(1)
|
| 14 |
+
values, indices = torch.topk(probability, k=3)
|
| 15 |
+
return {labels[i.item()]: v.item() for i, v in zip(indices.numpy()[0], values.detach().numpy()[0])}
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
gr.Interface(
|
| 19 |
+
predict,
|
| 20 |
+
inputs = gr.inputs.Image(type="pil", label="Chosen Image"),
|
| 21 |
+
outputs = 'label',
|
| 22 |
+
theme="seafoam",
|
| 23 |
+
).launch(debug=True)
|