Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -18,74 +18,41 @@ class GeneratedPrompt(BaseModel):
|
|
| 18 |
prompt: str
|
| 19 |
|
| 20 |
def image_url_to_base64(url):
|
| 21 |
-
response = requests.get(url, stream=True)
|
| 22 |
response.raise_for_status()
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
encoded_string = base64.b64encode(response.content).decode('utf-8')
|
| 27 |
-
|
| 28 |
return f"data:{content_type};base64,{encoded_string}"
|
| 29 |
|
| 30 |
-
|
| 31 |
def generate_prompt(url):
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
messages = [
|
| 40 |
-
{
|
| 41 |
-
|
| 42 |
-
"
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
"text": system_prompt
|
| 46 |
-
}
|
| 47 |
-
]
|
| 48 |
-
},
|
| 49 |
-
{
|
| 50 |
-
"role": "user",
|
| 51 |
-
"content": [
|
| 52 |
-
{
|
| 53 |
-
"type": "text",
|
| 54 |
-
"text": "Generate the prompt for the given ad image."
|
| 55 |
-
},
|
| 56 |
-
{
|
| 57 |
-
"type": "image_url",
|
| 58 |
-
"image_url": {
|
| 59 |
-
"url": url
|
| 60 |
-
}
|
| 61 |
-
}
|
| 62 |
-
]
|
| 63 |
-
}
|
| 64 |
]
|
| 65 |
-
|
| 66 |
response = gpt_client.beta.chat.completions.parse(
|
| 67 |
model="gemini-2.0-flash",
|
| 68 |
messages=messages,
|
| 69 |
response_format=GeneratedPrompt,
|
| 70 |
-
|
| 71 |
)
|
| 72 |
-
|
| 73 |
-
final_prompt = response.choices[0].message.parsed
|
| 74 |
-
final_prompt = final_prompt.prompt
|
| 75 |
-
|
| 76 |
return final_prompt
|
| 77 |
|
| 78 |
def generate_images(prompt):
|
| 79 |
replicate_client = replicate.Client(api_token=os.getenv("REPLICATE_API_TOKEN"))
|
| 80 |
output = replicate_client.run(
|
| 81 |
"google/imagen-4-ultra",
|
| 82 |
-
input={
|
| 83 |
-
"prompt": prompt,
|
| 84 |
-
"aspect_ratio": "1:1"
|
| 85 |
-
}
|
| 86 |
)
|
| 87 |
-
|
| 88 |
-
print(output)
|
| 89 |
urls = []
|
| 90 |
if isinstance(output, list) and output:
|
| 91 |
first = output[0]
|
|
@@ -95,7 +62,6 @@ def generate_images(prompt):
|
|
| 95 |
urls = [output]
|
| 96 |
elif hasattr(output, "url"):
|
| 97 |
urls = [getattr(output, "url")]
|
| 98 |
-
|
| 99 |
return urls[0]
|
| 100 |
|
| 101 |
def fetch_image_bytes(url):
|
|
@@ -104,7 +70,6 @@ def fetch_image_bytes(url):
|
|
| 104 |
return r.content
|
| 105 |
|
| 106 |
def init_s3():
|
| 107 |
-
|
| 108 |
return boto3.client(
|
| 109 |
"s3",
|
| 110 |
endpoint_url=os.getenv("R2_ENDPOINT"),
|
|
@@ -115,7 +80,6 @@ def init_s3():
|
|
| 115 |
|
| 116 |
def upload_to_r2(image_bytes):
|
| 117 |
s3_client = init_s3()
|
| 118 |
-
|
| 119 |
filename = f"{uuid4().hex}.png"
|
| 120 |
file_key = f"infinityverse/{filename}"
|
| 121 |
s3_client.put_object(
|
|
@@ -124,11 +88,16 @@ def upload_to_r2(image_bytes):
|
|
| 124 |
Body=image_bytes,
|
| 125 |
ContentType="image/png",
|
| 126 |
)
|
| 127 |
-
|
| 128 |
-
return r2_url
|
| 129 |
|
| 130 |
def get_images(reference_images):
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
results = []
|
| 133 |
max_workers = min(32, (os.cpu_count() or 1) * 2)
|
| 134 |
|
|
@@ -137,7 +106,6 @@ def get_images(reference_images):
|
|
| 137 |
ref_url = image["ref_url"]
|
| 138 |
base = image_url_to_base64(ref_url)
|
| 139 |
urls = []
|
| 140 |
-
|
| 141 |
with ThreadPoolExecutor(max_workers=min(max_workers, max(1, num))) as ex:
|
| 142 |
futures = [
|
| 143 |
ex.submit(
|
|
@@ -156,7 +124,5 @@ def get_images(reference_images):
|
|
| 156 |
urls.append(f.result())
|
| 157 |
except Exception:
|
| 158 |
pass
|
| 159 |
-
|
| 160 |
results.append({"ref_url": ref_url, "urls": urls})
|
| 161 |
-
|
| 162 |
-
return results
|
|
|
|
| 18 |
prompt: str
|
| 19 |
|
| 20 |
def image_url_to_base64(url):
|
| 21 |
+
response = requests.get(url, stream=True, timeout=60)
|
| 22 |
response.raise_for_status()
|
| 23 |
+
content_type = response.headers.get("Content-Type", "image/png")
|
| 24 |
+
encoded_string = base64.b64encode(response.content).decode("utf-8")
|
|
|
|
|
|
|
|
|
|
| 25 |
return f"data:{content_type};base64,{encoded_string}"
|
| 26 |
|
|
|
|
| 27 |
def generate_prompt(url):
|
| 28 |
+
system_prompt = (
|
| 29 |
+
"You are a top-tier performance digital marketer and creative strategist with 15+ years of expertise in affiliate marketing. "
|
| 30 |
+
"Your objective is to analyze the provided winning ad image, deconstruct its concept, visual composition, and color scheme, and generate a fresh, conversion-focused ad visual tailored. "
|
| 31 |
+
"The new design should convey a same as original image sentiment. Create a visually compelling ad optimized for Facebook Ads that is scroll-stopping, pattern-interrupting, and designed to drive high CTR and Conversion Rate. "
|
| 32 |
+
"Utilize striking color combinations, dynamic contrast levels, and strategic layout compositions to command attention while aligning with the target audience avatar. "
|
| 33 |
+
"Make sure the images should be realistic, not be stocky at all and raw which should look like they are shot from an iPhone."
|
| 34 |
+
)
|
| 35 |
messages = [
|
| 36 |
+
{"role": "system", "content": [{"type": "text", "text": system_prompt}]},
|
| 37 |
+
{"role": "user", "content": [
|
| 38 |
+
{"type": "text", "text": "Generate the prompt for the given ad image."},
|
| 39 |
+
{"type": "image_url", "image_url": {"url": url}}
|
| 40 |
+
]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
]
|
|
|
|
| 42 |
response = gpt_client.beta.chat.completions.parse(
|
| 43 |
model="gemini-2.0-flash",
|
| 44 |
messages=messages,
|
| 45 |
response_format=GeneratedPrompt,
|
|
|
|
| 46 |
)
|
| 47 |
+
final_prompt = response.choices[0].message.parsed.prompt
|
|
|
|
|
|
|
|
|
|
| 48 |
return final_prompt
|
| 49 |
|
| 50 |
def generate_images(prompt):
|
| 51 |
replicate_client = replicate.Client(api_token=os.getenv("REPLICATE_API_TOKEN"))
|
| 52 |
output = replicate_client.run(
|
| 53 |
"google/imagen-4-ultra",
|
| 54 |
+
input={"prompt": prompt, "aspect_ratio": "1:1"}
|
|
|
|
|
|
|
|
|
|
| 55 |
)
|
|
|
|
|
|
|
| 56 |
urls = []
|
| 57 |
if isinstance(output, list) and output:
|
| 58 |
first = output[0]
|
|
|
|
| 62 |
urls = [output]
|
| 63 |
elif hasattr(output, "url"):
|
| 64 |
urls = [getattr(output, "url")]
|
|
|
|
| 65 |
return urls[0]
|
| 66 |
|
| 67 |
def fetch_image_bytes(url):
|
|
|
|
| 70 |
return r.content
|
| 71 |
|
| 72 |
def init_s3():
|
|
|
|
| 73 |
return boto3.client(
|
| 74 |
"s3",
|
| 75 |
endpoint_url=os.getenv("R2_ENDPOINT"),
|
|
|
|
| 80 |
|
| 81 |
def upload_to_r2(image_bytes):
|
| 82 |
s3_client = init_s3()
|
|
|
|
| 83 |
filename = f"{uuid4().hex}.png"
|
| 84 |
file_key = f"infinityverse/{filename}"
|
| 85 |
s3_client.put_object(
|
|
|
|
| 88 |
Body=image_bytes,
|
| 89 |
ContentType="image/png",
|
| 90 |
)
|
| 91 |
+
return f'{os.getenv("NEW_BASE").rstrip("/")}/{file_key}'
|
|
|
|
| 92 |
|
| 93 |
def get_images(reference_images):
|
| 94 |
+
if isinstance(reference_images, list):
|
| 95 |
+
items = reference_images
|
| 96 |
+
elif isinstance(reference_images, dict):
|
| 97 |
+
items = reference_images.get("items", [])
|
| 98 |
+
else:
|
| 99 |
+
items = []
|
| 100 |
+
|
| 101 |
results = []
|
| 102 |
max_workers = min(32, (os.cpu_count() or 1) * 2)
|
| 103 |
|
|
|
|
| 106 |
ref_url = image["ref_url"]
|
| 107 |
base = image_url_to_base64(ref_url)
|
| 108 |
urls = []
|
|
|
|
| 109 |
with ThreadPoolExecutor(max_workers=min(max_workers, max(1, num))) as ex:
|
| 110 |
futures = [
|
| 111 |
ex.submit(
|
|
|
|
| 124 |
urls.append(f.result())
|
| 125 |
except Exception:
|
| 126 |
pass
|
|
|
|
| 127 |
results.append({"ref_url": ref_url, "urls": urls})
|
| 128 |
+
return results
|
|
|