Convert string to byte for pytorch loader
The method of downloading a pytorch model path is not in my control and I am trying to figure out a way to convert downloaded string data to byte data.  The code below downloads my saved model from Dropbox and uses bytes with utf-8 encoding to encode the string. The problem is when I use torch.load with BytesIO I get a UnpicklingError with invalid load key, '&lt;'.

    data = bytes(self.Download("https://www.dropbox.com/s/exampleurl/checkpoint.pth?dl=1"), 'utf-8')

    self.agent.local.load_state_dict(torch.load(BytesIO(data ), map_location=lambda storage, loc: storage))


The code below worked perfectly until requests was disabled and I am now trying to use the method above.

    dropbox_url = "https://www.dropbox.com/s/exampleurl/checkpoint.pth?dl=1"

    data = requests.get(dropbox_url )

    self.agent.local.load_state_dict(torch.load(BytesIO(data.content), map_location=lambda storage, loc: storage))


I just need to figure out a way to convert the string to bytes data the correct way.

I had to convert the byte data to base64 and save the file in that format.  Once I uploaded on Dropbox and downloaded using the built in method, I converted the base64 file back to bytes and it worked!

import base64
from io import BytesIO

with open("checkpoint.pth", "rb") as f:
    byte = f.read(1)

# Base64 Encode the bytes
data_e = base64.b64encode(byte)

filename ='base64_checkpoint.pth'

with open(filename, "wb") as output:
    output.write(data_e)

# Save file to Dropbox

# Download file on server
b64_str= self.Download('url')

# String Encode to bytes
byte_data = b64_str.encode("UTF-8")

# Decoding the Base64 bytes
str_decoded = base64.b64decode(byte_data)

# String Encode to bytes
byte_decoded = str_decoded.encode("UTF-8")

# Decoding the Base64 bytes
decoded = base64.b64decode(byte_decoded)

torch.load(BytesIO(decoded))

