|
import os |
|
from flask import Flask, request, jsonify |
|
import openai |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
openai.api_key = os.getenv("OPENAI_API_KEY") |
|
|
|
@app.route('/', methods=["GET", "POST"]) |
|
def generate_image_api(): |
|
|
|
auth_key = request.headers.get('Authorization') |
|
if auth_key != f"Bearer {os.getenv('API_KEY')}": |
|
return jsonify({"error": "Unauthorized"}), 401 |
|
|
|
|
|
description = request.json.get('description') |
|
if not description: |
|
return jsonify({"error": "No description provided"}), 400 |
|
|
|
|
|
response = openai.Image.create(prompt=description, n=1) |
|
image_url = response['data'][0]['url'] |
|
if request.method == "POST": |
|
return jsonify({"image_url": image_url}) |
|
return render_template("app.py", user_input=[prompt_input, auth_input], response=image_output) |
|
|
|
if __name__ == '__main__': |
|
app.run(debug=True) |