Spaces:
Runtime error
Runtime error
Upload hf-space/app_template.py
Browse files- hf-space/app_template.py +30 -0
hf-space/app_template.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 import keras
|
| 5 |
+
from tensorflow.keras.applications import resnet50
|
| 6 |
+
|
| 7 |
+
def load_model(tar_file: str='outputs/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 keras.models.load_model(model_path)
|
| 14 |
+
|
| 15 |
+
hf_hub_download(repo_id='$MODEL_REPO_ID', 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(10)}
|
| 25 |
+
return confidences
|
| 26 |
+
|
| 27 |
+
iface = gr.Interface(fn=classify_image,
|
| 28 |
+
inputs=gr.inputs.Image(shape=(224, 224)),
|
| 29 |
+
outputs=gr.outputs.Label(num_top_classes=3)).launch()
|
| 30 |
+
iface.launch()
|