Spaces:
Sleeping
Sleeping
import gradio as gr | |
import numpy as np | |
import tensorflow as tf | |
def softmax(vector): | |
e = np.exp(vector) | |
return e / e.sum() | |
def image_to_output (input_img): | |
gr_img = [] | |
gr_img.append(input_img) | |
img2 = tf.image.resize(tf.cast(gr_img, tf.float32)/255. , [224, 224]) | |
# print(img2) | |
x_test = np.asarray(img2) | |
prediction = model2.predict(x_test,batch_size=1).flatten() | |
prediction = softmax(prediction) | |
confidences = {labels[i]: float(prediction[i]) for i in range(102)} | |
# confidences = {labels[i]:float(top[i]) for i in range(num_predictions)} | |
return confidences | |
# Download the model checkpoint | |
import os | |
import requests | |
pretrained_repo = 'pretrained_model' | |
model_repo_link = 'https://huggingface.co/qmjnh/FlowerClassification/resolve/main' | |
for item in [ | |
'variables/variables.data-00000-of-00001', | |
'variables/variables.index', | |
'keras_metadata.pb', | |
'saved_model.pb', | |
]: | |
params = requests.get(model_repo_link+item) | |
output_file = os.path.join(pretrained_repo, item) | |
if not os.path.exists(os.path.dirname(output_file)): | |
os.makedirs(os.path.dirname(output_file)) | |
with open(output_file, 'wb') as f: | |
print(f'Downloading from {model_repo_link+item} to {output_file}') | |
f.write(params.content) | |
# Load the model | |
model2=tf.keras.models.load_model(pretrained_repo) | |
# Read the labels | |
with open('flower_names.txt') as f: | |
labels = f.readlines() | |
# Run gradio | |
UI=gr.Interface(fn=image_to_output, | |
inputs=gr.inputs.Image(shape=(224,224)), | |
outputs=gr.outputs.Label(num_top_classes=5), | |
interpretation="default" | |
) | |
UI.launch() |