nerdyrodent / VQGAN-CLIP

Just playing with getting VQGAN+CLIP running locally, rather than having to use colab.
Other
2.59k stars 428 forks source link

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

Open vendablefall opened 2 years ago

vendablefall commented 2 years ago

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.