Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tarfile
|
2 |
+
import gradio as gr
|
3 |
+
from huggingface_hub import hf_hub_download
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
from tensorflow.keras.applications import resnet50
|
6 |
+
|
7 |
+
def load_model(tar_file: str='model.tar.gz'):
|
8 |
+
tar_file = tarfile.open(tar_file)
|
9 |
+
tar_file.extractall('./')
|
10 |
+
tar_file.close()
|
11 |
+
|
12 |
+
model_path = './model'
|
13 |
+
return load_model(model_path)
|
14 |
+
|
15 |
+
hf_hub_download(repo_id='chansung/', filename='outputs/model.tar.gz')
|
16 |
+
model = load_model()
|
17 |
+
labels = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
|
18 |
+
|
19 |
+
def classify_image(inp):
|
20 |
+
inp = inp.reshape((-1, 224, 224, 3))
|
21 |
+
inp = resnet50.preprocess_input(inp)
|
22 |
+
|
23 |
+
prediction = model.predict(inp).flatten()
|
24 |
+
confidences = {labels[i]: float(prediction[i]) for i in range(1000)}
|
25 |
+
return confidences
|
26 |
+
|
27 |
+
gr.Interface(fn=classify_image,
|
28 |
+
inputs=gr.inputs.Image(shape=(224, 224)),
|
29 |
+
outputs=gr.outputs.Label(num_top_classes=3),
|
30 |
+
examples=["banana.jpg", "car.jpg"]).launch()
|