ARB Security Solutions, LLC.

SharePoint Online to Store and Retrieve PyTorch Models

This example uses SharePoint Online to store and retrieve PyTorch models. It first defines a PyTorch model (in this case, a ResNet18 model), and then defines two functions to save and load PyTorch models to and from SharePoint Online. The save_model_to_sharepoint function takes a PyTorch model and a filename as inputs, and saves the model to a SharePoint Online list called “Models”. The load_model_from_sharepoint function takes a filename as input, retrieves the corresponding model from SharePoint Online, loads the model into a PyTorch ImageClassifier, and returns the model. The example then defines a Flask app that has two routes: a GET route that displays an HTML form where users can upload an image, and a POST route that processes the uploaded image and returns the predicted label (either “Cat” or “Dog”) using the PyTorch model stored in SharePoint Online. The POST route first reads the uploaded image, preprocesses it using standard ImageNet preprocessing, and feeds it into the PyTorch model. It then returns the predicted label as a JSON object. Finally, the example saves the PyTorch model to SharePoint Online using the save_model_to_sharepoint function, and runs the Flask app. When the Flask app is running, users can upload images to the app and get predictions from the PyTorch model stored in SharePoint Online.

# Import required libraries
import io
import torch
import torch.nn as nn
import torchvision.models as models
import torchvision.transforms as transforms
from PIL import Image
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext

# Set up SharePoint Online connection
url = 'https://yourcompany.sharepoint.com/sites/sitename'
username = '[email protected]'
password = 'yourpassword'
auth_context = AuthenticationContext(url)
auth_context.acquire_token_for_user(username, password)
ctx = ClientContext(url, auth_context)

# Define your PyTorch model
class ImageClassifier(nn.Module):
    def __init__(self, num_classes):
        super(ImageClassifier, self).__init__()
        self.model = models.resnet18(pretrained=True)
        self.model.fc = nn.Linear(self.model.fc.in_features, num_classes)

    def forward(self, x):
        x = self.model(x)
        return x

# Define functions to save and load PyTorch models to/from SharePoint Online
def save_model_to_sharepoint(model, filename):
    model_bytes = io.BytesIO()
    torch.save(model.state_dict(), model_bytes)
    model_bytes.seek(0)
    file_content = model_bytes.getvalue()
    list_title = 'Models'
    folder_url = 'Shared%20Documents/Models'
    ctx.web.lists.get_by_title(list_title).root_folder.upload_file(filename, file_content, overwrite=True)

def load_model_from_sharepoint(filename):
    list_title = 'Models'
    folder_url = 'Shared%20Documents/Models'
    file_url = f"{folder_url}/{filename}"
    file_content = ctx.web.get_file_by_server_relative_url(file_url).read()
    model_bytes = io.BytesIO(file_content)
    model = ImageClassifier(num_classes=2)
    model.load_state_dict(torch.load(model_bytes))
    model.eval()
    return model

# Define your Flask app
app = Flask(__name__)

@app.route('/', methods=['GET'])
def index():
    return '''
    <form method="POST" enctype="multipart/form-data">
        <input type="file" name="file">
        <input type="submit">
    </form>
    '''

@app.route('/', methods=['POST'])
def upload_file():
    file = request.files['file']
    img_bytes = file.read()
    img = Image.open(io.BytesIO(img_bytes))
    img = transforms.Resize(256)(img)
    img = transforms.CenterCrop(224)(img)
    img = transforms.ToTensor()(img)
    img = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(img)
    img = img.unsqueeze(0)
    model_filename = 'my_model.pt'
    model = load_model_from_sharepoint(model_filename)
    with torch.no_grad():
        output = model(img)
    result = output.argmax().item()
    if result == 0:
        label = 'Cat'
    else:
        label = 'Dog'
    return jsonify({'result': label})

if __name__ == '__main__':
    model_filename = 'my_model.pt'
    num_classes = 2
    model = ImageClassifier(num_classes)
    save_model_to_sharepoint(model, model_filename)
    app.run()

Comments are closed.