How to download large model files to the sharing app?

Of course! I don’t know DropBox, but my Google Drive solution should be similar that you can adapt for it. This is what I used in my skyAR app:

@st.cache
def load_model():

    save_dest = Path('model')
    save_dest.mkdir(exist_ok=True)
    
    f_checkpoint = Path("model/skyAR_coord_resnet50.pt")

    if not f_checkpoint.exists():
        with st.spinner("Downloading model... this may take awhile! \n Don't stop it!"):
            from GD_download import download_file_from_google_drive
            download_file_from_google_drive(cloud_model_location, f_checkpoint)
    
    model = torch.load(f_checkpoint, map_location=device)
    model.eval()
    return model

Basically, check if the weights exist, if not download them from the cloud. Because streamlit sharing uses a semi-permanent instance you only have to download once for all users. st.cache is clutch here, for each session you only need to load it once.

4 Likes