Skip to content

Running generate.py from another python file "best practise" #142

Open
@vendablefall

Description

@vendablefall

I want to call generate.py from my python application, what is the "best" way to do this? i currently have two options:

This uses the shell to run another process, not ideal as I would like to stay in python, but its easy...

  import os
  vqgan_generate_dir = "/foo/baa"

  def generate(*, prompts=None):
      vqgan_arguments = []
  
      if prompts:
          vqgan_arguments.append("--prompts")
          vqgan_arguments.append(prompts)
      else:
          default_prompt="a nice default prompt"
          vqgan_arguments.append("--prompts")
          vqgan_arguments.append(default_prompt)
  
  
      vqan_argument_string = ' '.join(vqgan_arguments)
      # os.system(f"{vqgan_generate_dir}/generate.py {vqan_argument_string}")
      print(f"{vqgan_generate_dir}/generate.py {vqan_argument_string}")

This uses straight Python by modifying sys.argv before calling generate.py, this seems over convoluted to pass args:

  import sys
  import argparse
  import imp

  vqgan_generate_dir = "/foo/baa/"

  def generate(   prompts   ):

      vq_parser = argparse.ArgumentParser(description='Process some params.')
      vq_parser.add_argument("-p",    "--prompts", type=str, help="Text prompts", default=None, dest='prompts')
     
      sys.argv = ['generate.py'] # define your commandline arguments here
  
      if prompts:
          sys.argv.append("-p")
          sys.argv.append(prompts)
  
      args = vq_parser.parse_args()
      print (args)
  
      ############################################################################
      #Load & run the generate.py module
      ############################################################################
  
      try:
          fp, pathname, description = imp.find_module('generate', vqgan_generate_path)
          generate = imp.load_module("generate", fp, pathname, description)
      except ImportError:
          print(f"Could not import: {vqgan_generate_dir}generate.py")
          quit()
      finally:
          if fp:
              fp.close()

neither of these are tested yet as I'm just exploring concepts, what do you think is the best way? is there another way?

option two gets really long-winded once you want ALL args, and option one is overly simplistic and doesn't allow you to control the params going in well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions