File size: 1,230 Bytes
f83e84d db6e2f8 f83e84d db6e2f8 f83e84d db6e2f8 f83e84d db6e2f8 f83e84d db6e2f8 f83e84d db6e2f8 f83e84d db6e2f8 f83e84d db6e2f8 f83e84d db6e2f8 f83e84d db6e2f8 f83e84d db6e2f8 f83e84d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
from flask import Flask, render_template, request
import config
import os
import openai
#creating the 404 page (Optional)
def page_not_found(e):
return render_template('404.html'), 404
##Initialising FLAK
app = Flask(__name__)
app.config.from_object(config.config['development'])
app.register_error_handler(404, page_not_found)
### Initialise the OPENAI library with the key saved in the CONFIG file
openai.api_key = app.config['OPENAI_KEY']
#####----------START FUNCTIONS--------------------------------------------------------------------
def createImageFromPrompt(prompt):
response = openai.Image.create(prompt=prompt, n=2, size="512x512")
return response['data']
#####----------END FUNCTIONS--------------------------------------------------------------------
##View Functions
@app.route('/', methods=["GET", "POST"])
def index():
if request.method == 'POST':
images = []
prompt = request.form['prompt']
res = createImageFromPrompt(prompt)
if len(res) > 0:
for img in res:
images.append(img['url'])
return render_template('index.html', **locals())
#Run Flask
if __name__ == '__main__':
app.run(host='0.0.0.0', port='8000')
|