Spaces:
Runtime error
Runtime error
File size: 1,924 Bytes
bc768f1 92b9152 bc768f1 92b9152 bc768f1 92b9152 bc768f1 92b9152 bc768f1 92b9152 c22ad84 bc768f1 92b9152 bc768f1 92b9152 bc768f1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
### -------------------------------- ###
### 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() |