Skip to content

Commit

Permalink
reorganize src
Browse files Browse the repository at this point in the history
  • Loading branch information
sweetkane committed Mar 19, 2024
1 parent debb0f1 commit 59f8324
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/artscii/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,14 @@ def check_requirements():
print("ERR: See: https://platform.openai.com/docs/quickstart/step-2-set-up-your-api-key")
exit(1)


def main():
check_requirements()
args = setup_arg_parser()

# generate image
def generate(prompt):
print("Generating image...", end="\r")
headers = {
'Authorization': f"Bearer {OPENAI_API_KEY}",
}
json_data = {
'model': 'dall-e-3',
'prompt': args.prompt[0] + " (this image will be converted to ascii art)",
'prompt': prompt + " (this image will be converted to ascii art)",
'n': 1,
'size': '1024x1024',
}
Expand All @@ -72,21 +67,31 @@ def main():
json=json_data
).json()
print("", end="\r")
return response


def cache(response):
try:
with open('artscii.cache.json', 'r') as fp:
cachedic = json.loads(fp.read())
except:
cachedic = {"openai_responses": []}
with open('artscii.cache.json', 'w') as fp:
cachedic["openai_responses"].append(response)
json.dump(cachedic, fp)


def main():
check_requirements()
args = setup_arg_parser()

response = generate(args.prompt[0])

# convert to ascii
img_url = response["data"][0]["url"]
subprocess.call(["ascii-image-converter", img_url] + args.options)

# handle cache
if (args.cache):
try:
with open('artscii.cache.json', 'r') as fp:
cachedic = json.loads(fp.read())
except:
cachedic = {"openai_responses": []}
with open('artscii.cache.json', 'w') as fp:
cachedic["openai_responses"].append(response)
json.dump(cachedic, fp)
cache(response)


if __name__ == '__main__':
Expand Down

0 comments on commit 59f8324

Please sign in to comment.