digitalocean / sample-functions-python-jokes

34 stars 66 forks source link

Use for machine learning API #5

Closed alphaprojects1 closed 2 years ago

alphaprojects1 commented 2 years ago

We are having problems using functions for the purpose of deploying a machine learning API.

In particular we have two issues:

Reduced code (to minimum) for illustration:


import pickle
from fastapi import FastAPI

def main(args):
      name = args.get("name", "stranger")
      greeting = "Hello " + name + "!"
      print(greeting)

      with open('model.pkl', 'rb') as file:  
        model = pickle.load(file)     

      return {"body": greeting}

results in error (for fastapi):

4:
"2022-06-13T19:09:25.300896817Z stderr: from fastapi import FastAPI"
5:
"2022-06-13T19:09:25.300938849Z stderr: ModuleNotFoundError: No module named 'fastapi'"

and separately we get error for file:

Error: While deploying action 'sample/hello': 413 Payload Too Large

Can you help us on both questions, i.e. when/where to install python modules, why do we get error on file size even though it is only 0.3GB.

rabbah commented 2 years ago

Did you add the library to requirements.txt and use a remote build? Here's a patch to install fastapi for the function. As to the 413 payload error, it could be due to the size of the function zip file after bundling both the library and the model exceeds the current limit of 48MB. A workaround would be to store the model in Spaces and download it into the function directly.

diff --git a/packages/joke/joke/__main__.py b/packages/joke/joke/__main__.py
index 88a16b2..ed3adab 100644
--- a/packages/joke/joke/__main__.py
+++ b/packages/joke/joke/__main__.py
@@ -1,10 +1,10 @@
-import pyjokes
+import pickle
+from fastapi import FastAPI

 def main(args):
-  joke = pyjokes.get_joke()
   return {
     'body': {
       'response_type': 'in_channel',
-      'text': joke
+      'text': '???'
     }
   }
diff --git a/packages/joke/joke/requirements.txt b/packages/joke/joke/requirements.txt
index 846fd67..1e77ba6 100644
--- a/packages/joke/joke/requirements.txt
+++ b/packages/joke/joke/requirements.txt
@@ -1 +1 @@
-pyjokes==0.6.0 
+fastapi==0.78.0