-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupscalecreateimages.py
139 lines (120 loc) · 4.75 KB
/
upscalecreateimages.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
import openai
import pandas as pd
from tqdm import tqdm
from PIL import Image
import requests
import io
# Set OpenAI and Stability.ai API keys
openai.api_key = 'YOUR_OPEN_AI_KEY'
stability_ai_key = 'YOUR_STABILITY_AI_KEY'
# Set Stability API key for image upscaling
api_key = 'YOUR_STABILIT_AI_KEY'
if api_key is None:
raise Exception("Missing Stability API key.")
api_host = os.getenv("API_HOST", "https://api.stability.ai")
def generate_clickable_title(detail):
prompt = f"Generate a catchy and clickable title for a Cork Back Coaster with the theme: '{detail}'. Maximum 50 characters. At the end of each title write Cork Back Coaster"
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
]
)
clickable_title = response['choices'][0]['message']['content'].strip()
clickable_title = clickable_title.replace('"', '') # Remove double quotes
return clickable_title
def generate_description(detail):
prompt = f"Generate a compelling description for a Cork Back Coaster with the theme: '{detail}'. Maximum 150 characters."
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
]
)
description = response['choices'][0]['message']['content'].strip()
description = description.replace('"', '') # Remove double quotes
description += """
<p>This personalized photo coaster helps you market yourself the right way or uplift any table with a custom touch. Each round and square coaster is made with a high-gloss, polyester-coated hardboard top, and features a genuine cork bottom to prevent sliding. </p>
<p>.: Material: Genuine cork bottom finished with a glossy white top made of polyester-coated hardboard<br />.: Size: 3.75″ x 3.75″ (Square) and 4″ x 4″ (Round)<br />.: High-gloss top<br />.: NB! One coaster per listing<br />.: Assembled in the USA from globally sourced parts</p>
"""
return description
def generate_tags(detail):
prompt = f"Generate relevant tags for a Cork Back Coaster with the theme: '{detail}'. Separate the tags with commas."
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
]
)
tag = response['choices'][0]['message']['content'].strip()
tag = tag.replace('"', '') # Remove double quotes
return tag
csv_path = "input.csv"
df = pd.read_csv(csv_path)
file_names = []
local_paths = []
titles = []
descriptions = []
tags = []
for idx, row in tqdm(df.iterrows(), total=df.shape[0]):
detail = row['details']
title = generate_clickable_title(detail)
description = generate_description(detail)
tag = generate_tags(detail)
# Use the detail from the CSV as the image prompt
image_prompt = detail
# Generate the image using stable diffusion
url = "https://api.stability.ai/v1/generation/stable-diffusion-v1-5/text-to-image"
headers = {"Authorization": f"Bearer {stability_ai_key}", "Accept": "image/png"}
data = {
"width": 512,
"height": 512,
"text_prompts": [
{
"text": image_prompt,
"weight": 0.5,
}
]
}
response = requests.post(url, headers=headers, json=data)
image_data = response.content
image = Image.open(io.BytesIO(image_data))
file_name = f"image_{idx}.png"
local_path = f"{file_name}"
image.save(local_path)
# Upscale the image using the "esrgan-v1-x2plus" engine
response = requests.post(
f"{api_host}/v1/generation/esrgan-v1-x2plus/image-to-image/upscale",
headers={
"Accept": "image/png",
"Authorization": f"Bearer {api_key}"
},
files={
"image": open(local_path, "rb")
},
data={
"width": 2048,
}
)
if response.status_code != 200:
raise Exception("Non-200 response: " + str(response.text))
upscaled_path = f"upscaled_{file_name}"
with open(upscaled_path, "wb") as f:
f.write(response.content)
local_paths.append(upscaled_path)
file_names.append(file_name)
titles.append(title)
descriptions.append(description)
tags.append(tag)
output_df = pd.DataFrame({
"file_name": file_names,
"local_path": local_paths,
"title": titles,
"description": descriptions,
"tags": tags
})
output_df.to_csv("product_information.csv", index=False)