I am creating a simple shiny app for a dog breed classifier. The user would be able to upload a picture of a dog, and then have the shiny app display the predicted breed.
A simplified version of the app code is as follows:
from shiny import App, Inputs, Outputs, Session, reactive, render, ui
import keras
import tensorflow as tf
import numpy as np
import pickle
app_ui = ui.page_fluid(
ui.input_file("dog_pic", "Upload your dog picture!", accept=[".jpeg"], multiple=False),
ui.output_text("txt")
)
def server(input, output, session):
# PROCESSING IMAGE:
@reactive.Calc
def parsed_file():
image = tf.keras.preprocessing.image.load_img(input.dog_pic, target_size=(224, 224))
input_arr = tf.keras.preprocessing.image.img_to_array(image)
input_arr = np.array([input_arr])
input_arr = input_arr.astype('float32') / 255.
return input_arr
# PARSING IMAGE THROUGH TO MODEL
@output
@render.text
def txt():
model = tf.keras.models.load_model('model')
dog_prediction = model.predict(parsed_file())
return f"Predicted dog breed is {dog_prediction}!"
app = App(app_ui, server)
What does this mean? Thanks!

Your functions
parsed_file()andtxt()are called as soon as you start the shiny app. But at that time your file is not yet loaded. You have to add something like "if file available then ... else ...".