Amalfa-Photo-Shoot / image_gen.py
userIdc2024's picture
Update image_gen.py
3dcb50d verified
import base64
from openai import OpenAI
from dotenv import load_dotenv
import os
from helpers import encode_image, generate_rep_image
load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
def get_image(product, wear, product_category, user_inputs, dropdown_list):
analyses = client.responses.create(
model="gpt-4o",
input=[
{
"role": "user",
"content":[
{
"type": "input_text",
"text": f"Analyse the design and wearing way for the given {product_category}"
},
{
"type": "input_image",
"image_url": f"data:image/jpeg;base64,{encode_image(product)}"
},
{
"type": "input_image",
"image_url": f"data:image/jpeg;base64,{encode_image(wear)}"
}
]
}
]
)
imgs = []
for u_input, d_input in zip(user_inputs, dropdown_list):
prompt_gen = client.responses.create(
previous_response_id=analyses.id,
model="gpt-4o",
input=[
{
"role": "user",
"content": [
{
"type": "input_text",
"text": f"Generate me a prompt for the {product_category}, based on the user request for the given {product_category} which should include following things:"
f"User Request: {u_input},"
"Things to include:"
f"""[1] CORE ─ Subject + Action + Product
<Describe who/what, what they’re doing, and the product’s key look>
[2] CAMERA ─ Angle / POV / Lens / Crop
[3] MOOD ─ Lighting / Colour Grade / Backdrop
[4] QUALITY ─ Resolution / Realism / Photographic Style
[5] EXTRAS ─ Skin Texture / Makeup / Wardrobe / Atmosphere / Aspect Ratio
Make sure you only return the prompt and should be in single paragraph.
Don't write anything about the design of {product_category} in the prompt.
Where ever you need to mention about the {product_category}, only write '{product_category} in reference image' or similar to this."""
}
],
},
]
)
prompt = prompt_gen.output[0].content[0].text
print(prompt)
if d_input == "On Model":
rep_image = generate_rep_image(wear, prompt)
image_bytes = base64.b64decode(rep_image)
imgs.append(image_bytes)
image_gen = client.responses.create(
previous_response_id=prompt_gen.id,
model="gpt-4o",
input=[
{
"role": "user",
"content": [
{
"type": "input_text",
"text": f"""Create Image for the following request:
{prompt}"""
},
# {
# "type": "input_image",
# "image_url": f"data:image/jpeg;base64,{encode_image(product)}"
# },
# {
# "type": "input_image",
# "image_url": f"data:image/jpeg;base64,{encode_image(wear)}"
# }
]
}
],
tools=[
{
"type": "image_generation",
"quality": "high",
"size": "1024x1024"
}
]
)
rep_image = generate_rep_image(wear, prompt)
image_bytes = base64.b64decode(rep_image)
imgs.append(image_bytes)
else:
image_gen = client.responses.create(
previous_response_id=prompt_gen.id,
model="gpt-4o",
input=[
{
"role": "user",
"content": [
{
"type": "input_text",
"text": f"""Create Image for the following request:
{prompt}
Ensuring the {product_category} remains in natural size and detail, not magnified. The focus is realistic: not zoomed-in or stylized"""
},
# {
# "type": "input_image",
# "image_url": f"data:image/jpeg;base64,{encode_image(product)}"
# }
]
}
],
tools=[
{
"type": "image_generation",
"quality": "high",
"size": "1024x1024"
}
]
)
image_generation_calls = [
output
for output in image_gen.output
if output.type == "image_generation_call"
]
image_data = [output.result for output in image_generation_calls]
if image_data:
image_base64 = image_data[0]
image_bytes = base64.b64decode(image_base64)
imgs.append(image_bytes)
print("Image Generated")
else:
print("No image generated")
return imgs