Skip to main content
Active ⭐️⭐️⭐️
Aug 16, 2022 12:52 PM UTC
Solved

Pickle File use in CDF function

I have one model.pkl file which is a pre-trained Data Science model. I want to load that file inside handler of CDF function and do some prediction. My first question is where to keep that file in CDF. Second is how to load that file inside handler ?

vwrocks8Answer
Practitioner ⭐️⭐️⭐️
Aug 18, 2022 3:53 PM UTC

I would suggest using this method to download the file to the /tmp directory from within the function. Once dowloaded you can treat it as a local file and load it.

pickled_model = pickle.load(open('/tmp/model.pkl', 'rb'))

Assuming this a python pickled model.

3 replies

aishwaryaAuthor
Active ⭐️⭐️⭐️
Aug 16, 2022 1:24 PM UTC

If I put the model.pkl file in “Files” in CDF. Don’t know how to load it and do the prediction, Tried with download_bytes.  Getting error 

 

vwrocks8Answer
Practitioner ⭐️⭐️⭐️
Aug 18, 2022 3:53 PM UTC

I would suggest using this method to download the file to the /tmp directory from within the function. Once dowloaded you can treat it as a local file and load it.

pickled_model = pickle.load(open('/tmp/model.pkl', 'rb'))

Assuming this a python pickled model.

kelvin
Expert ⭐️⭐️⭐️⭐️
Expert ⭐️⭐️⭐️⭐️
Aug 18, 2022 5:52 PM UTC

As @vwrocks8 suggests, the missing piece seems to be “unpickling” your file, e.g. pickle.load(...). You can do that directly from bytes too.