Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion make_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
RESUME = False


def load_key_file(key_file_path):
"""Load key file (one key per line).
Return Comma-separated string of multiple keys.
"""
if not os.path.isfile(key_file_path):
raise Exception(f"File '{key_file_path}' doesn't exist")
keys = []
with open(key_file_path, "r", encoding="utf-8") as fp:
keys.extend(line.strip() for line in fp)
return ",".join(keys)


class Base:
def __init__(self, key, language, api_base=None):
self.key = key
Expand Down Expand Up @@ -236,6 +248,13 @@ def save_progress(self):
help="openai api key,if you have more than one key,you can use comma"
" to split them and you can break through the limitation",
)
parser.add_argument(
"--openai_key_file",
dest="openai_key_file",
type=str,
default="",
help="The file contains openai api keys (each key per line).",
)
parser.add_argument(
"--no_limit",
dest="no_limit",
Expand Down Expand Up @@ -302,8 +321,11 @@ def save_progress(self):
if PROXY != "":
os.environ["http_proxy"] = PROXY
os.environ["https_proxy"] = PROXY

OPENAI_API_KEY = options.openai_key or env.get("OPENAI_API_KEY")
OPENAI_KEY_FILE = options.openai_key_file
if not OPENAI_API_KEY:
if OPENAI_KEY_FILE:
OPENAI_API_KEY = load_key_file(OPENAI_KEY_FILE)
RESUME = options.resume
Comment on lines 324 to 329
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's decide which is the higher priority key arg or key-file?

if not OPENAI_API_KEY:
raise Exception("Need openai API key, please google how to")
Expand Down