Packages not accessible in clent side from server side

I am trying to import 2 packages speechrecognition & textblob in server side code and access them in client code. This is giving me an error :

anvil.server.SerializationError: Cannot serialize return value from function. Cannot serialize <class 'module'> object at msg['response'] at [Form1, line 8] 
def recorder1(x):
  
    sr = anvil.server.call('package1',)
    
    TextBlob = anvil.server.call('package2',)

called from [Form1, line 162] i.e.

a1 = recorder1(0)

All above is client side code.

Server side code is:

@anvil.server.callable
def package1():
  
  import speech_recognition as sr
  
  return sr


@anvil.server.callable
def package2():
  
  from textblob import TextBlob
  
  return TextBlob

You can’t send a package from the server to the client.

Instead consider writing the function you need on the server and calling it from the client.
The return values for a server function from the docs:

Valid arguments and return values

The arguments and return values of @anvil.server.callable functions must be very simple. They may only be strings, numbers, lists, dicts, datetime.date , datetime.datetime , None , Media objects, or rows from a data table. They may not be circular (for example, a dict may not contain itself).

An example of what you might do… just pseudo code…

client side

def convert_button_click(self, **event_args):
  # use the audio clip you recorded on the client side
  # this will be a media object
  text = anvil.server.call('convert_to_text', audio)

server side

import speech_recognition as sr

@anvil.server.callable
def convert_to_text(audio_from_client):
   # do some stuff with the sr module
   return converted_text

Good luck!

2 Likes

2 posts were merged into an existing topic: Speechrecognition in anvil webapp