diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 73bd22b..b480e85 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -26,4 +26,4 @@ jobs: run: pip install requests - name: Run Python script - run: python main.py + run: python get_image.py diff --git a/generated_images/1716561851.png b/generated_images/1716561851.png new file mode 100644 index 0000000..5ec25b7 Binary files /dev/null and b/generated_images/1716561851.png differ diff --git a/get_image.py b/get_image.py new file mode 100644 index 0000000..0e10f14 --- /dev/null +++ b/get_image.py @@ -0,0 +1,57 @@ +import requests +import json +import os +from PIL import Image +from io import BytesIO + +api_key = os.getenv("MY_SECRET") +FILE_PATH = 'C:\\Users\\khart\\Desktop\\project\\DALLE-WZIM\\generated_images\\' + + +# Define the endpoint URL +url = 'https://api.openai.com/v1/images/generations' +# Set up headers for the request +headers = { + 'Content-Type': 'application/json', + 'Authorization': f'Bearer {api_key}' +} +# Define the payload with the prompt for image generation +data = { + 'prompt': "beautiful japanese sunset", # should integrate input() or smth similar here + 'n': 1, # Number of images to generate (up to 10) + 'size': '1024x1024' # Desired size of the generated image +} +# POST request to the DALL·E API +response = requests.post(url, headers=headers, data=json.dumps(data)) +# Check the response status code +if response.status_code == 200: + # Parse the JSON response + response_data = response.json() + # URL of the generated image + url = response_data['data'][0]['url'] +else: + # Print the error message + print('Error:', response.status_code, response.text) + + +def save_image_from_url(url, file_path): + try: + print(response_data["created"]) + # Send a GET request to the URL + response_url = requests.get(url) + # Check if the request was successful + response_url.raise_for_status() + + # Open the image from the response content + image = Image.open(BytesIO(response_url.content)) + + # Save the image to the specified file path + image.save(f"{file_path}" + f"{response_data['created']}.png") + print(f"Image saved to {file_path}") + except requests.exceptions.RequestException as e: + print(f"Error downloading image: {e}") + except IOError as e: + print(f"Error saving image: {e}") + + +save_image_from_url(url, FILE_PATH) \ No newline at end of file diff --git a/main.py b/main.py deleted file mode 100644 index d78a4fd..0000000 --- a/main.py +++ /dev/null @@ -1,38 +0,0 @@ -import requests -import json -import os - -# API key -api_key = os.getenv("MY_SECRET") - -# Define the endpoint URL -url = 'https://api.openai.com/v1/images/generations' - -# Set up headers for the request -headers = { - 'Content-Type': 'application/json', - 'Authorization': f'Bearer {api_key}' -} -print(f'The secret is : {os.getenv("API_KEY")}') - -# Define the payload with the prompt for image generation -data = { - 'prompt': "beautiful japanese sunset", - 'model': "dalle-e-3", - 'n': 1, # Number of images to generate - 'size': '1024x1024' # Desired size of the generated image -} - -print("123") -# POST request to the DALL·E API -response = requests.post(url, headers=headers, data=json.dumps(data)) - -# Check the response status code -if response.status_code == 200: - # Parse the JSON response - response_data = response.json() - # Print the URL of the generated image - print('Generated image URL:', response_data['data'][0]['url']) -else: - # Print the error message - print('Error:', response.status_code, response.text) \ No newline at end of file