-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
52 lines (40 loc) · 1.45 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
# App to display images from Dalle.
# don't forget to set the environement first:
# python3 -m venv venv
# source venv/bin/activate
# source .env
from flask import Flask, redirect, render_template, request, url_for
import openai, os, requests
app = Flask(__name__)
openai.api_key = os.getenv("OPENAI_API_KEY")
@app.route("/", methods=("GET", "POST"))
def index():
if request.method == "POST":
prompt = request.form["prompt"]
response = generate_image(prompt)
return redirect(url_for("index", result=response))
result = request.args.get("result")
return render_template("index.html", result=result)
def generate_image(text='generate a picture of a dalle-mini'):
# Set the API endpoint and the prompt
api_endpoint = 'https://api.openai.com/v1/images/generations'
# Set the headers and the payload
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {os.getenv("OPENAI_API_KEY")}'
}
payload = {
'model': 'image-alpha-001',
'prompt': text,
'num_images': 1,
'size': '256x256',
'response_format': 'url'
}
# Send the request and get the response
response = requests.post(api_endpoint, headers=headers, json=payload)
if response.status_code == 200:
image_url = response.json()['data'][0]['url']
print(f'Image URL: {image_url}')
else:
print(f'Error: {response.json()["error"]}')
return image_url