Spaces:
Runtime error
Runtime error
Commit
·
12ac53e
1
Parent(s):
27cac51
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,11 @@
|
|
| 1 |
import requests
|
| 2 |
import json
|
|
|
|
| 3 |
from PIL import Image
|
|
|
|
| 4 |
|
| 5 |
# Replace YOUR_API_KEY with your OpenAI API key
|
| 6 |
-
API_KEY = '
|
| 7 |
ENDPOINT = 'https://api.openai.com/v1/images/generations'
|
| 8 |
|
| 9 |
def generate_image(prompt):
|
|
@@ -23,7 +25,8 @@ def generate_image(prompt):
|
|
| 23 |
response = requests.post(url=ENDPOINT, headers=headers, data=json.dumps(data))
|
| 24 |
if response.status_code == 200:
|
| 25 |
result_url = response.json()['data'][0]['url']
|
| 26 |
-
|
|
|
|
| 27 |
return image
|
| 28 |
else:
|
| 29 |
return None
|
|
@@ -32,29 +35,7 @@ def generate_image(prompt):
|
|
| 32 |
prompt = "a cat sitting on a windowsill looking outside"
|
| 33 |
image = generate_image(prompt)
|
| 34 |
if image:
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
print("Error generating image")
|
| 38 |
-
# A red sports car driving on a winding road
|
| 39 |
-
prompt = "a red sports car driving on a winding road"
|
| 40 |
-
image = generate_image(prompt)
|
| 41 |
-
if image:
|
| 42 |
-
image.show()
|
| 43 |
-
else:
|
| 44 |
-
print("Error generating image")
|
| 45 |
-
|
| 46 |
-
# A house in a forest with a river nearby
|
| 47 |
-
prompt = "a house in a forest with a river nearby"
|
| 48 |
-
image = generate_image(prompt)
|
| 49 |
-
if image:
|
| 50 |
-
image.show()
|
| 51 |
-
else:
|
| 52 |
-
print("Error generating image")
|
| 53 |
-
|
| 54 |
-
# A slice of pepperoni pizza on a plate
|
| 55 |
-
prompt = "a slice of pepperoni pizza on a plate"
|
| 56 |
-
image = generate_image(prompt)
|
| 57 |
-
if image:
|
| 58 |
-
image.show()
|
| 59 |
else:
|
| 60 |
print("Error generating image")
|
|
|
|
| 1 |
import requests
|
| 2 |
import json
|
| 3 |
+
from io import BytesIO
|
| 4 |
from PIL import Image
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
|
| 7 |
# Replace YOUR_API_KEY with your OpenAI API key
|
| 8 |
+
API_KEY = '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'
|
| 9 |
ENDPOINT = 'https://api.openai.com/v1/images/generations'
|
| 10 |
|
| 11 |
def generate_image(prompt):
|
|
|
|
| 25 |
response = requests.post(url=ENDPOINT, headers=headers, data=json.dumps(data))
|
| 26 |
if response.status_code == 200:
|
| 27 |
result_url = response.json()['data'][0]['url']
|
| 28 |
+
image_bytes = requests.get(result_url).content
|
| 29 |
+
image = Image.open(BytesIO(image_bytes))
|
| 30 |
return image
|
| 31 |
else:
|
| 32 |
return None
|
|
|
|
| 35 |
prompt = "a cat sitting on a windowsill looking outside"
|
| 36 |
image = generate_image(prompt)
|
| 37 |
if image:
|
| 38 |
+
plt.imshow(image)
|
| 39 |
+
plt.show()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
else:
|
| 41 |
print("Error generating image")
|