From 22dc9e7275484137daefbaa3f3f57c0c1fde10a0 Mon Sep 17 00:00:00 2001 From: Rodolfo Barcelli Jo Date: Wed, 17 Sep 2025 20:47:14 +0800 Subject: [PATCH] Add: input image field for create_image.py --- README.md | 64 ++++++++++++++++++++++++------------------------- create_image.py | 42 +++++++++++++++++++++++++------- 2 files changed, 65 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 5cb8c91..1c422a0 100644 --- a/README.md +++ b/README.md @@ -23,37 +23,37 @@ You can manipulate the main abstractions of the OpenAI model: * Each script has a number of mandatory parameters in strict order: - | Script | Parameter 1 | Parameter 2 | Parameter 3 | Purpose | - | --------------------------------------- | ----------------- | --------------- | --------------- | -------------------------------------------------------- | - | `cancel_model_response.py` | `response_id` | | | Cancel a Response running in the background | - | `create_conversation.py` | | | | Create a new Conversation | - | `create_image.py` | `model_id` | `prompt` | | Create an image based on the prompt description | - | `create_model_response.py` | `conversation_id` | `model_id` | `input_message` | Create a Response within a Conversation | - | `create_text_item.py` | `conversation_id` | `input_message` | | Create a user input or instruction within a Conversation | - | `create_vector_store.py` | | | | Create a Vector Store | - | `create_vector_store_file.py` | `vector_store_id` | `file_id` | | Create a File in a Vector Store | - | `delete_conversation.py` | `conversation_id` | | | Delete a Conversation | - | `delete_file.py` | `file_id` | | | Delete a File | - | `delete_item.py` | `conversation_id` | `item_id` | | Delete an Item (input or Response) from a Conversation | - | `delete_model_response.py` | `response_id` | | | Delete a Response | - | `delete_vector_store.py` | `vector_store_id` | | | Delete a Vector Store | - | `delete_vector_store_file.py` | `vector_store_id` | `file_id` | | Delete a File from a Vector Store | - | `list_files.py` | | | | List all uploaded Files | - | `list_input_items.py` | `response_id` | | | List all input Items used to generate a Response | - | `list_items.py` | `conversation_id` | | | List all Items (inputs or Responses) in a Conversation | - | `list_vector_store_files.py` | `vector_store_id` | | | List all Files in a Vector Store | - | `list_vector_stores.py` | | | | List all Vector Stores | - | `modify_vector_store.py` | `vector_store_id` | `metadata` | | Update a Vector Store metadata | - | `retrieve_conversation.py` | `conversation_id` | | | Retrieve a Conversation | - | `retrieve_file.py` | `file_id` | | | Retrieve a File | - | `retrieve_file_content.py` | `file_id` | | | Retrieve the content of File | - | `retrieve_item.py` | `conversation_id` | `item_id` | | Retrieve an Item from a Conversation | - | `retrieve_model_response.py` | `response_id` | | | Retrieve a model Response | - | `retrieve_vector_store.py` | `vector_store_id` | | | Retrieve a Vector Store | - | `retrieve_vector_store_file.py` | `vector_store_id` | `file_id` | | Retrieve a File from a Vector Store | - | `retrieve_vector_store_file_content.py` | `vector_store_id` | `file_id` | | Retrieve the content of a File from a Vector Store | - | `update_conversation.py` | `conversation_id` | `metadata` | | Update a Conversation metadata | - | `update_vector_store_file.py` | `vector_store_id` | `file_id` | `attributes` | Update a File attributes | - | `upload_file.py` | `filename` | `path-to-file` | | Upload a File to the OpenAI platform | +| | Script | Parameter 1 | Parameter 2 | Parameter 3 | Purpose | +| --- | --------------------------------------- | ----------------- | --------------- | --------------- | ------------------------------------------------------------------------- | +| | `cancel_model_response.py` | `response_id` | | | Cancel a Response running in the background | +| | `create_conversation.py` | | | | Create a new Conversation | +| | `create_image.py` | `model_id` | `prompt` | `image.png` | Create an image based on the prompt description and/or the image provided | +| | `create_model_response.py` | `conversation_id` | `model_id` | `input_message` | Create a Response within a Conversation | +| | `create_text_item.py` | `conversation_id` | `input_message` | | Create a user input or instruction within a Conversation | +| | `create_vector_store.py` | | | | Create a Vector Store | +| | `create_vector_store_file.py` | `vector_store_id` | `file_id` | | Create a File in a Vector Store | +| | `delete_conversation.py` | `conversation_id` | | | Delete a Conversation | +| | `delete_file.py` | `file_id` | | | Delete a File | +| | `delete_item.py` | `conversation_id` | `item_id` | | Delete an Item (input or Response) from a Conversation | +| | `delete_model_response.py` | `response_id` | | | Delete a Response | +| | `delete_vector_store.py` | `vector_store_id` | | | Delete a Vector Store | +| | `delete_vector_store_file.py` | `vector_store_id` | `file_id` | | Delete a File from a Vector Store | +| | `list_files.py` | | | | List all uploaded Files | +| | `list_input_items.py` | `response_id` | | | List all input Items used to generate a Response | +| | `list_items.py` | `conversation_id` | | | List all Items (inputs or Responses) in a Conversation | +| | `list_vector_store_files.py` | `vector_store_id` | | | List all Files in a Vector Store | +| | `list_vector_stores.py` | | | | List all Vector Stores | +| | `modify_vector_store.py` | `vector_store_id` | `metadata` | | Update a Vector Store metadata | +| | `retrieve_conversation.py` | `conversation_id` | | | Retrieve a Conversation | +| | `retrieve_file.py` | `file_id` | | | Retrieve a File | +| | `retrieve_file_content.py` | `file_id` | | | Retrieve the content of File | +| | `retrieve_item.py` | `conversation_id` | `item_id` | | Retrieve an Item from a Conversation | +| | `retrieve_model_response.py` | `response_id` | | | Retrieve a model Response | +| | `retrieve_vector_store.py` | `vector_store_id` | | | Retrieve a Vector Store | +| | `retrieve_vector_store_file.py` | `vector_store_id` | `file_id` | | Retrieve a File from a Vector Store | +| | `retrieve_vector_store_file_content.py` | `vector_store_id` | `file_id` | | Retrieve the content of a File from a Vector Store | +| | `update_conversation.py` | `conversation_id` | `metadata` | | Update a Conversation metadata | +| | `update_vector_store_file.py` | `vector_store_id` | `file_id` | `attributes` | Update a File attributes | +| | `upload_file.py` | `filename` | `path-to-file` | | Upload a File to the OpenAI platform | * To execute these scripts run `python3 ...`. diff --git a/create_image.py b/create_image.py index 28211c2..3275da1 100644 --- a/create_image.py +++ b/create_image.py @@ -7,20 +7,40 @@ from PIL import Image from io import BytesIO from datetime import datetime -def create_image(model,prompt): - url = base_url+"images/generations" +def create_image(model,prompt,image_path=None): + # choose endpoint depending on whether an image is provided + if image_path: + url = base_url+"images/edits" + else: + url = base_url+"images/generations" - payload = json.dumps({ - "prompt": prompt, - "model": model - }) headers = { - 'Content-Type': 'application/json', 'Authorization': 'Bearer '+secret_key, } date_time_string = datetime.now().strftime("%Y-%m-%d-%H-%M-%S") - response = requests.request("POST", url, headers=headers, data=payload) + if image_path: + # For edits, use multipart/form-data + data = { + "prompt": prompt, + "model": model + } + files = { + "image": open(image_path, "rb") + } + response = requests.post(url, headers=headers, data=data, files=files) + else: + # For generations, use JSON + payload = json.dumps({ + "prompt": prompt, + "model": model + }) + headers['Content-Type'] = 'application/json' + response = requests.post(url, headers=headers, data=payload) + if response.status_code != 200: + print(f"Error: {response.status_code} - {response.text}") + sys.exit(1) + # handle response data: both generation and edits return b64_json in data[0] decoded_bytes = base64.b64decode(response.json()["data"][0]["b64_json"]) byte_stream = BytesIO(decoded_bytes) image = Image.open(byte_stream) @@ -30,4 +50,8 @@ def create_image(model,prompt): if __name__ == '__main__': - create_image(str(sys.argv[1]),str(sys.argv[2])) + # usage: python create_image.py [image_path] + model = str(sys.argv[1]) if len(sys.argv) > 1 else '' + prompt = str(sys.argv[2]) if len(sys.argv) > 2 else '' + image_path = str(sys.argv[3]) if len(sys.argv) > 3 else None + create_image(model,prompt,image_path)