Skip to content

Commit 96c2e44

Browse files
committed
update
1 parent 1ac980e commit 96c2e44

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

Diff for: example.env

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
DEEPAI_API_KEY=""
2+
OPENAI_API_KEY=""
3+
OPENAI_ORG_ID=""
4+
AZURE_OPENAI_API_KEY=""
5+
AZURE_OPENAI_ENDPOINT=""
6+
AZURE_OPENAI_DEPLOYMENT_NAME=""

Diff for: openai-text-2-img.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import requests
22
import json
33
import os
4+
import openai
45
from dotenv import load_dotenv
56
load_dotenv()
67

78
openai_api_key = os.getenv("OPENAI_API_KEY")
9+
openai_org_id = os.getenv("OPENAI_ORG_ID")
10+
openai_api_base = "https://api.openai.com/v1"
11+
azure_openai_api_key = os.getenv("AZURE_OPENAI_API_KEY")
12+
azure_openai_api_base = os.getenv("AZURE_OPENAI_ENDPOINT")
13+
deployment_name = os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME")
14+
use_azure = False
815

916
def text_to_image(prompt: str):
1017
authorization = f"Bearer {openai_api_key}"
@@ -14,10 +21,29 @@ def text_to_image(prompt: str):
1421
response = requests.post(url, data=json.dumps(data), headers=headers)
1522
return response.json()['data'][0]['url']
1623

24+
def create_image_from_prompt(prompt: str, imagesize: str ="256x256", num_images: int = 1):
25+
if use_azure:
26+
openai.api_type = "azure"
27+
openai.api_version = "2023-05-15"
28+
openai.api_base = azure_openai_api_base
29+
openai.deployment_name = deployment_name
30+
openai.api_key = azure_openai_api_key
31+
else:
32+
openai.api_type = "openai"
33+
openai.api_version = '2020-11-07'
34+
openai.api_key = openai_api_key
35+
if openai_org_id:
36+
openai.organization = openai_org_id
37+
response = openai.Image.create(
38+
prompt=prompt,
39+
n=num_images,
40+
size=imagesize)
41+
image_urls = [data['url'] for data in response['data']]
42+
return image_urls
1743

1844
def main():
1945
prompt = 'While traveling through a dense forest, you stumble upon an ancient, overgrown path veering off from the main trail. Do you dare to explore its mysteries?'
20-
image_url = text_to_image(prompt)
46+
image_url = create_image_from_prompt(prompt)
2147
print(image_url)
2248

2349
if __name__ == '__main__':

0 commit comments

Comments
 (0)