Skip to content

Instantly share code, notes, and snippets.

@Krewn
Created January 4, 2023 18:44
Show Gist options
  • Save Krewn/d4296dc546362095a2b9ff28fb96f233 to your computer and use it in GitHub Desktop.
Save Krewn/d4296dc546362095a2b9ff28fb96f233 to your computer and use it in GitHub Desktop.
You will need to set your OPENAI_API_KEY system environmental variable for this to work for you.
#importing needed libraries
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config
import openai
import json
#creating a data structure for our application
responses = []
#function for the home page
def home(request):
html = "<html><head><title>OpenAI Image Generation</title></head><body>"
#loop through the responses and add them to the page
if len(responses) == 0:
html = html + "<form action='/create' method='post'><label for='prompt'>Prompt:</label><br><textarea type='text' rows='21' cols='65' name='prompt' id='prompt'></textarea><select name='size'><option value='256x256'>256x256</option><option value='512x512'>512x512</option><option value='1024x1024'>1024x1024</option></select><select name='count'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select><input type='submit'></form></body></html>"
first = True
for response in responses[::-1]:
html = html + f"<div><h2>{response['prompt']}</h2>"+"".join([f"<img src={response['image']['data'][n]['url']}>" for n in range(len(response['image']['data']))])+"</div>".format()
if first:
#adding the form to the page
html = html + "<form action='/create' method='post'><label for='prompt'>Prompt:</label><br><textarea type='text' rows='21' cols='65' name='prompt' id='prompt'></textarea><select name='size'><option value='256x256'>256x256</option><option value='512x512'>512x512</option><option value='1024x1024'>1024x1024</option></select><select name='count'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select><input type='submit'></form></body></html>"
first = False
#html = html + "<script>window.scrollTo(0, document.body.scrollHeight);</script>"
return Response(html)
#function for the create page
#@app.route('/create',method='POST')
@view_config(route_name='create')
def create(request):
#getting the prompt and size from the form
prompt = request.params['prompt']
size = request.params['size']
count = int(request.params['count'])
#generating the image
image = openai.Image.create(prompt = prompt, n = count, size = size)
#adding the response to our list
responses.append({'prompt':prompt, 'image':image})
with open("openAiImageBetaResponses.txt","a") as op:
op.write(json.dumps({'prompt':prompt, 'size': size, 'image':image}))
#redirecting to the home page
return Response(status=302,location='/')
if __name__ == '__main__':
with Configurator() as config:
config.add_route('home', r'/')
config.add_view(home, route_name='home')
config.add_route('create', '/create')
config.scan('__main__')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 6411, app)
server.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment