-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdalle_app.py
70 lines (55 loc) · 1.91 KB
/
dalle_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 time
import requests
from decouple import config
import streamlit as st
# Read API key and API URL from .env file
api_key = config('API_KEY')
api_base = config('API_BASE')
st.title("Dalle -E OpenAI Image Generator (Streamlit)")
# Step 1: Submit the image generation job
def post_call(prompt):
url = f"{api_base}/openai/images/generations:submit?api-version=2023-06-01-preview"
headers = {
'Content-Type': 'application/json',
'api-key': f'{api_key}',
}
data = {
'prompt': prompt,
'size': "512x512",
'n': 1,
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 202:
return response.json()["id"] # returns image id
else:
return None
# Step 2: Check the job status and get the result
def get_call(operation_id):
url = f"{api_base}/openai/operations/images/{operation_id}?api-version=2023-06-01-preview"
headers = {
'api-key': f'{api_key}'
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()["result"]["data"][0]["url"]
else:
return None
# Streamlit UI
prompt = st.text_input("Enter your prompt:")
generate_button = st.button("Generate Image")
if generate_button:
st.write("Generating image...")
operation_id = post_call(prompt)
if operation_id:
progress_bar = st.empty() # Create a placeholder for the progress bar
for i in range(101):
progress_bar.progress(i)
time.sleep(1)
# Step 2: Check the job status and get the result
generated_image_url = get_call(operation_id)
if generated_image_url:
st.image(generated_image_url, caption="Generated Image", use_column_width=True)
else:
st.error("Failed to retrieve the generated image.")
else:
st.error("Failed to submit the image generation job.")