Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,53 @@
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import json
|
| 4 |
-
from io import BytesIO
|
| 5 |
|
| 6 |
-
from flask import Flask, jsonify, render_template, request, send_file
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
# https://huggingface.co/settings/tokens
|
| 12 |
-
# https://huggingface.co/spaces/{username}/{space}/settings
|
| 13 |
-
API_TOKEN = os.getenv("BIG_GAN_TOKEN")
|
| 14 |
|
| 15 |
-
app = Flask(__name__)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
return render_template("index.html")
|
| 21 |
|
| 22 |
|
| 23 |
-
@app.route("/infer_biggan")
|
| 24 |
-
def biggan():
|
| 25 |
-
input = request.args.get("input")
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
data=json.dumps(input),
|
| 32 |
-
)
|
| 33 |
|
| 34 |
-
return send_file(BytesIO(output.content), mimetype="image/png")
|
| 35 |
|
|
|
|
| 36 |
|
| 37 |
-
@app.route("/infer_t5")
|
| 38 |
-
def t5():
|
| 39 |
-
input = request.args.get("input")
|
| 40 |
|
| 41 |
-
output = infer_t5(input)
|
| 42 |
|
| 43 |
-
return jsonify({"output": output})
|
| 44 |
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
|
| 56 |
-
return jsonify({"output": output})
|
| 57 |
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request
|
| 2 |
+
import config
|
| 3 |
import os
|
| 4 |
+
import openai
|
|
|
|
|
|
|
| 5 |
|
|
|
|
| 6 |
|
| 7 |
+
#creating the 404 page (Optional)
|
| 8 |
+
def page_not_found(e):
|
| 9 |
+
return render_template('404.html'), 404
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
|
|
|
|
| 12 |
|
| 13 |
+
##Initialising FLAK
|
| 14 |
+
app = Flask(__name__)
|
| 15 |
+
app.config.from_object(config.config['development'])
|
| 16 |
+
app.register_error_handler(404, page_not_found)
|
| 17 |
|
| 18 |
+
### Initialise the OPENAI library with the key saved in the CONFIG file
|
| 19 |
+
openai.api_key = app.config['OPENAI_KEY']
|
|
|
|
| 20 |
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
#####----------START FUNCTIONS--------------------------------------------------------------------
|
| 24 |
+
def createImageFromPrompt(prompt):
|
| 25 |
+
response = openai.Image.create(prompt=prompt, n=2, size="512x512")
|
| 26 |
+
return response['data']
|
|
|
|
|
|
|
| 27 |
|
|
|
|
| 28 |
|
| 29 |
+
#####----------END FUNCTIONS--------------------------------------------------------------------
|
| 30 |
|
|
|
|
|
|
|
|
|
|
| 31 |
|
|
|
|
| 32 |
|
|
|
|
| 33 |
|
| 34 |
+
##View Functions
|
| 35 |
+
@app.route('/', methods=["GET", "POST"])
|
| 36 |
+
def index():
|
| 37 |
|
| 38 |
+
if request.method == 'POST':
|
| 39 |
+
images = []
|
| 40 |
+
prompt = request.form['prompt']
|
| 41 |
+
res = createImageFromPrompt(prompt)
|
| 42 |
|
| 43 |
+
if len(res) > 0:
|
| 44 |
+
for img in res:
|
| 45 |
+
images.append(img['url'])
|
| 46 |
|
| 47 |
+
return render_template('index.html', **locals())
|
| 48 |
|
|
|
|
| 49 |
|
| 50 |
|
| 51 |
+
#Run Flask
|
| 52 |
+
if __name__ == '__main__':
|
| 53 |
+
app.run(host='0.0.0.0', port='8000')
|