-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
70 lines (50 loc) · 1.69 KB
/
app.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import logging
import openai
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__, static_folder="static")
CORS(app, resources={
r"/*": {"origins": ["http://localhost:5173", "http://127.0.0.1:5173"]}})
# Static Files
@app.route("/")
def index():
return app.send_static_file("index.html")
@app.route("/favicon.ico")
def favicon():
return app.send_static_file('favicon.ico')
@app.route("/assets/<path:path>")
def assets(path):
return send_from_directory("static/assets", path)
# AOAI Integration Settings
AZURE_OPENAI_RESOURCE = os.environ.get("AZURE_OPENAI_RESOURCE")
AZURE_OPENAI_KEY = os.environ.get("AZURE_OPENAI_KEY")
AZURE_OPENAI_PREVIEW_API_VERSION = os.environ.get(
"AZURE_OPENAI_PREVIEW_API_VERSION", "2023-06-01-preview")
def image_create(request):
openai.api_type = "azure"
openai.api_base = f"https://{AZURE_OPENAI_RESOURCE}.openai.azure.com/"
openai.api_version = AZURE_OPENAI_PREVIEW_API_VERSION
openai.api_key = AZURE_OPENAI_KEY
request_messages = request.json["messages"]
response = openai.Image.create(
prompt=request_messages,
size='512x512',
n=1
)
response_obj = {
"created": response.created,
"data": response.data
}
return jsonify(response_obj), 200
@app.route("/generate_img", methods=["GET", "POST"])
def generate_img():
try:
return image_create(request)
except Exception as e:
logging.exception("Exception in /generate_img")
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(host="127.0.0.1", port=8000, debug=True)