Hi, let's say I have a custom_recipe.py file, in which I have this recipe:
# do stuff
Is it possible to call this recipe from Python (instead of command line)?
By calling from python, I mean as textcat_custom_model(*args), i.e. excluding operations equivalent to command line such as subprocess.run() or Jupyter magic.
The main reason I would like such usage is so that I can pass Python objects (dynamically generated callables, for instance) as arguments of the recipe.
Hi! I think what you're looking for is the
prodigy.serve function: https://prodi.gy/docs/api-components#serve It looks like a command-line command for consistency, but it's not actually calling a command in a subprocess – it gets the wrapped recipe function, parses the arguments, calls the function, uses it to initialize the controller and then starts the Prodigy web server.
Here's how it looks under the hood:
from prodigy import get_recipe
from prodigy.app import server
def serve(command, *recipe_args, **config):
cli_args = shlex.split(command)
recipe_name = cli_args.pop(0)
loaded_recipe = get_recipe(recipe_name)
controller = loaded_recipe(*cli_args, use_plac=True)
Thank you Ines! It worked.
Here's what I did, for those who might find this post relevant:
(this was using Prodigy 1.8.5, with Python 3.6, on 64-bit Linux)
@prodigy.recipe('custom.textcat' # sparing the args)
A custom recipe to be called directly in Python.
# do stuff
In another script that uses this recipe:
# the imports below let prodigy recognize the custom.textcat recipe by name
from custom_recipe import custom_textcat
args =  # fill in arguments here