Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vendablefall opened this issue Mar 14, 2022 · 0 comments
Open

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

vendablefall opened this issue Mar 14, 2022 · 0 comments

Comments

@vendablefall
Copy link

vendablefall commented Mar 14, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant