Skip to content

Instantly share code, notes, and snippets.

@imsmith
Created August 18, 2023 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imsmith/807c89c2d669ca35d05a21326be64403 to your computer and use it in GitHub Desktop.
Save imsmith/807c89c2d669ca35d05a21326be64403 to your computer and use it in GitHub Desktop.
GPT-4 prompt generator for DALL-E Space Images
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.jobstores.base import JobLookupError
from app import write_log
from models import Prompt, Image
import random
from flask import Flask
import openai
import models
from extensions import db
import time
art_styles = [
"Photorealistic",
"3D render",
"Oil painting",
"Digital painting",
"Concept art",
"Photorealistic",
]
space_themes = [
"Exoplanets", "Aliens", "Astronauts", "Spaceships", "Nebulae", "Galaxies", "Black holes",
"Supernovae", "Meteor showers", "Comets", "Moons", "Space stations", "Pulsars", "Quasars",
"Star clusters", "Wormholes", "Space colonies", "Satellite",
"Space telescopes", "Aurora borealis in space", "Solar flares",
"Alien civilizations", "Dyson spheres", "Space battles",
"Martian landscapes", "Interstellar travel", "Star births",
"Alien fauna and flora", "Distant sunsets", "Gas giants", "Cosmic ice structures",
"Alien architecture", "Advanced propulsion systems", "Outer space exploration",
"Gamma-ray bursts", "White dwarfs", "Red giants",
"Starship fleets", "Intergalactic stations", "Space elevators",
"Celestial dragons", "Space mysteries", "Alien artifacts", "Astronomical phenomena",
"Solar systems", "Rings of planets", "Space anomalies",
"Cosmic gardens", "Deep space phenomena", "Space pioneers",
"Alien societies", "Distant galaxies colliding", "Space oases",
"Milky way vistas", "Galaxy clusters",
"Stellar nurseries", "Interstellar clouds",
"Galactic cores", "Alien deserts",
"Cosmic paradises", "Alien monuments", "Celestial storms",
"Alien paradises", "Intergalactic events"
]
def generate_prompt(num_themes=1):
# Randomly select themes from the list
chosen_style = random.choice(art_styles)
selected_themes = random.sample(space_themes, num_themes)
sentence_limit = "Please keep it to 1 medium-length sentence."
instructions = ("The prompt should start with something like,"
" 'Write a DALL-E image prompt..'")
main_prompt = ""
if num_themes == 1:
main_prompt = ("Write a DALL-E prompt to generate an image with"
" the theme: {}. The style should be {}."
" {} {}").format(chosen_style, sentence_limit, instructions)
# Insert the selected themes into the main prompt
return main_prompt.format(*selected_themes)
def generate_prompt_prompts(sapp):
with sapp.app_context():
theme_count = 1 # random.choice([1,2,3])
chosen_prompt = generate_prompt(theme_count)
write_log("generate_prompt_prompts " + chosen_prompt)
gpt_response = openai.ChatCompletion.create(
model="gpt-4-0314",
messages=[
{"role": "system", "content": "You are a space and sci-fi nerd."},
{"role": "user", "content": chosen_prompt},
]
)
# Get the generated text from GPT's response
gpt_prompt = gpt_response.choices[0].message['content']
write_log("prompt generator " + gpt_prompt)
token_usage = gpt_response.usage
write_log(token_usage)
new_prompt = models.Prompt(prompt=gpt_prompt, prompt_source=chosen_prompt)
write_log("added prompt")
db.session.add(new_prompt)
db.session.commit()
def start_scheduler(sapp):
scheduler = BackgroundScheduler()
try:
write_log("prompt generator trying to get job")
job = scheduler.get_job('generate_prompt_prompts')
print(job)
except JobLookupError:
write_log("prompt generator job not found, adding it")
scheduler.add_job(generate_prompt_prompts, 'interval', minutes=30, id='generate_prompt_prompts', args=[sapp])
if not scheduler.running:
write_log("prompt generator not running, starting it")
scheduler.start()
scheduler.print_jobs()
else:
write_log("prompt generator already running, not starting")
write_log("going to create app prompt generator")
sapp = create_app()
# Keep the script running
while True:
# write_log("scheduler waiting")
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment