-
Notifications
You must be signed in to change notification settings - Fork 0
/
5_chatgpt_gen.py
84 lines (67 loc) · 2.61 KB
/
5_chatgpt_gen.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
import os
from dotenv import load_dotenv
import json
from openai import OpenAI
load_dotenv()
f = open('basedata-raw.json')
combinations = json.load(f)
client = OpenAI(
api_key=os.getenv("OPENIA_APIKEY"),
)
chatgpt_dir = os.path.join("buttplugs", "chatgpt")
os.makedirs(chatgpt_dir, exist_ok=True)
def generate_background_story(properties, idx):
filename = f'000000{idx}.gif'[-8:]
image_url = f"https://bafybeidd62ezqvyyviibduxaz2wuuyexkpuwbdfo34wukucxtav7qh3cbe.ipfs.cf-ipfs.com/{filename}"
try:
traits = ""
for attribute in properties["attributes"]:
# Convert to lowercase
prop = properties['attributes'][attribute];
traits += f"{attribute}: {prop}\n"
response = client.chat.completions.create(
model="gpt-4-vision-preview",
messages=[
{
"role": "system",
"content": "Generate a name and a background story with this NFT who is part of WebtrES club, use the the traits, and also the attached image as reference. The expected output is in json format, { Name: [name], Description: [background] }"
},
{
"role": "user",
"content": [
{"type": "text", "text": traits},
{
"type": "image_url",
"image_url": {
"url": image_url,
"detail": "high"
},
},
]
}
],
temperature=0.8,
max_tokens=300,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
# Extract the generated text from the response
generated_text = response.choices[0].message.content
json_str = generated_text.replace('```json', '').replace("```", '')
return json.loads(json_str)
except Exception as e:
print("Error while generating background story:", e)
print(json_str)
return "" # Return an empty string in case of error
for idx, combination in combinations.items():
realid = int(idx) + 1
print(realid,"/ 1024")
output_file = os.path.join(chatgpt_dir, f'000000{realid}.json'[-9:])
# if file exist skip
if os.path.exists(output_file):
continue
data = generate_background_story(combination, realid)
if data:
with open(output_file, 'w') as json_file:
json.dump(data, json_file, indent=2)