Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
from transformers import AutoFeatureExtractor
|
4 |
from PIL import Image
|
5 |
import numpy as np
|
6 |
import logging
|
@@ -9,10 +9,10 @@ import logging
|
|
9 |
logging.basicConfig(level=logging.DEBUG)
|
10 |
|
11 |
# Load the pre-trained model and feature extractor
|
12 |
-
model_name = "
|
13 |
logging.info("Loading image processor and model...")
|
14 |
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
15 |
-
model =
|
16 |
|
17 |
# Define the prediction function
|
18 |
def predict(image):
|
@@ -29,15 +29,15 @@ def predict(image):
|
|
29 |
logging.debug("Converting NumPy array to PIL image...")
|
30 |
image = Image.fromarray(image, 'RGBA').convert('RGB')
|
31 |
logging.debug("Image converted successfully.")
|
32 |
-
|
33 |
-
# Process the image for the model
|
34 |
-
inputs = feature_extractor(images=image, return_tensors="np")
|
35 |
-
pixel_values = inputs['pixel_values'][0]
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
41 |
top_classes = [model.config.id2label[idx] for idx in top_idxs]
|
42 |
result = {top_classes[i]: float(top_probs[i]) for i in range(3)}
|
43 |
logging.info("Prediction successful.")
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
4 |
from PIL import Image
|
5 |
import numpy as np
|
6 |
import logging
|
|
|
9 |
logging.basicConfig(level=logging.DEBUG)
|
10 |
|
11 |
# Load the pre-trained model and feature extractor
|
12 |
+
model_name = "microsoft/quickdraw"
|
13 |
logging.info("Loading image processor and model...")
|
14 |
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
15 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
16 |
|
17 |
# Define the prediction function
|
18 |
def predict(image):
|
|
|
29 |
logging.debug("Converting NumPy array to PIL image...")
|
30 |
image = Image.fromarray(image, 'RGBA').convert('RGB')
|
31 |
logging.debug("Image converted successfully.")
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
logging.info("Processing image...")
|
34 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
35 |
+
outputs = model(**inputs)
|
36 |
+
logits = outputs.logits
|
37 |
+
probs = torch.nn.functional.softmax(logits, dim=-1)
|
38 |
+
top_probs, top_idxs = probs.topk(3, dim=-1)
|
39 |
+
top_probs = top_probs.detach().numpy()[0]
|
40 |
+
top_idxs = top_idxs.detach().numpy()[0]
|
41 |
top_classes = [model.config.id2label[idx] for idx in top_idxs]
|
42 |
result = {top_classes[i]: float(top_probs[i]) for i in range(3)}
|
43 |
logging.info("Prediction successful.")
|