Spaces:
Runtime error
Runtime error
| ### -------------------------------- ### | |
| ### libraries ### | |
| ### -------------------------------- ### | |
| import gradio as gr | |
| import numpy as np | |
| import os | |
| from tensorflow.keras.models import load_model | |
| from reader import get_article | |
| ### -------------------------------- ### | |
| ### model loading ### | |
| ### -------------------------------- ### | |
| model = load_model('model.h5') # single file model from colab | |
| ## --------------------------------- ### | |
| ### reading: categories.txt ### | |
| ### -------------------------------- ### | |
| labels = ['please upload categories.txt' for i in range(10)] # placeholder | |
| if os.path.isfile("categories.txt"): | |
| # open categories.txt in read mode | |
| categories = open("categories.txt", "r") | |
| labels = categories.readline().split() | |
| ## --------------------------------- ### | |
| ### reading: info.txt ### | |
| ### -------------------------------- ### | |
| # borrow file reading functionality from reader.py | |
| info = get_article() | |
| ### -------------------------------- ### | |
| ### interface creation ### | |
| ### -------------------------------- ### | |
| samples = ['pug.jpeg', 'cheetah.jpeg'] | |
| def preprocess(image): | |
| image = np.array(image) / 255 | |
| image = np.expand_dims(image, axis=0) | |
| return image | |
| def predict_image(image): | |
| pred = model.predict(preprocess(image)) | |
| results = {} | |
| for row in pred: | |
| for idx, item in enumerate(row): | |
| results[labels[idx]] = float(item) | |
| return results | |
| # generate img input and text label output | |
| image = gr.inputs.Image(shape=(300, 300), label="Upload Your Image Here") | |
| label = gr.outputs.Label(num_top_classes=len(labels)) | |
| # generate and launch interface | |
| interface = gr.Interface(fn=predict_image, inputs=image, outputs=label, article=info['article'], css=info['css'], theme='default', title=info['title'], allow_flagging='never', description=info['description'], examples=samples) | |
| interface.launch() |