Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import from_pretrained_keras
|
2 |
+
import tensorflow as tf
|
3 |
+
from tensorflow_addons.optimizers import AdamW
|
4 |
+
import numpy as np
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
tf.keras.optimizers.AdamW = AdamW
|
8 |
+
model = from_pretrained_keras("keras-io/vit-small-ds")
|
9 |
+
|
10 |
+
IMAGE_SIZE = 72
|
11 |
+
|
12 |
+
def softmax(x):
|
13 |
+
f_x = np.exp(x) / np.sum(np.exp(x))
|
14 |
+
return f_x
|
15 |
+
|
16 |
+
labels = ["apple", "aquarium_fish", "baby", "bear", "beaver", "bed", "bee", "beetle", "bicycle", "bottle", "bowl", "boy", "bridge", "bus", "butterfly", "camel", "can", "castle", "caterpillar", "cattle", "chair", "chimpanzee", "clock", "cloud", "cockroach", "couch", "cra", "crocodile", "cup", "dinosaur", "dolphin", "elephant", "flatfish", "forest", "fox", "girl", "hamster", "house", "kangaroo", "keyboard", "lamp", "lawn_mower", "leopard", "lion", "lizard", "lobster", "man", "maple_tree", "motorcycle", "mountain", "mouse", "mushroom", "oak_tree", "orange", "orchid", "otter", "palm_tree", "pear", "pickup_truck", "pine_tree", "plain", "plate", "poppy", "porcupine", "possum", "rabbit", "raccoon", "ray", "road", "rocket", "rose", "sea", "seal", "shark", "shrew", "skunk", "skyscraper", "snail", "snake", "spider", "squirrel", "streetcar", "sunflower", "sweet_pepper", "table", "tank", "telephone", "television", "tiger", "tractor", "train", "trout", "tulip", "turtle", "wardrobe", "whale", "willow_tree", "wolf", "woman", "worm"]
|
17 |
+
|
18 |
+
def classify_image(image):
|
19 |
+
#inp = inp.reshape((-1, 299, 299, 3))
|
20 |
+
resized_image = tf.image.resize(
|
21 |
+
tf.convert_to_tensor([image]), size=(IMAGE_SIZE, IMAGE_SIZE))
|
22 |
+
pred = model.predict(resized_image)
|
23 |
+
prediction = softmax(pred)
|
24 |
+
return {labels[i]: float(prediction[i]) for i in range(100)}
|
25 |
+
|
26 |
+
image = gr.inputs.Image()
|
27 |
+
label = gr.outputs.Label(num_top_classes=3)
|
28 |
+
|
29 |
+
iface = gr.Interface(classify_image,image,label,
|
30 |
+
#outputs=[
|
31 |
+
# gr.outputs.Textbox(label="Engine issue"),
|
32 |
+
# gr.outputs.Textbox(label="Engine issue score")],
|
33 |
+
examples=["sample.csv","sample2.csv"],
|
34 |
+
#, title="Classification of Ford Motor data",
|
35 |
+
# description = "Model for predicting issues in Ford engines.",
|
36 |
+
article = "Author: <a href=\"https://huggingface.co/joheras\">Jónathan Heras</a>"
|
37 |
+
# examples = ["sample.csv"],
|
38 |
+
)
|
39 |
+
|
40 |
+
|
41 |
+
iface.launch()
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
|