-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_prompt.py
167 lines (137 loc) · 4.8 KB
/
generate_prompt.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import os
import json
import rich
import random
import base64
from openai import OpenAI
from anthropic import Anthropic
import google.generativeai as genai
from dotenv import load_dotenv
# import PIL.Image
# img = PIL.Image.open('image.png')
# img
load_dotenv()
client = OpenAI()
client.api_key = os.environ["OPENAI_API_KEY"]
anthroClient = Anthropic(
api_key=os.environ.get("ANTHROPIC_API_KEY")
)
genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
def compiledInput(maintext, subtext, placeOfEvent, timingOfEvent, contactDetails):
input = {
"title" : str(maintext),
"tagline": str(subtext),
"placeOfEvent": str(placeOfEvent),
"timingOfEvent": str(timingOfEvent),
"contactDetails": str(contactDetails)
}
return str(input)
def claudeCall(system_prompt, user_input = None):
prompt = system_prompt
messages = [
{
"role": "user",
"content": str(prompt)
}
]
if user_input != None:
messages.append(
{
"role": "user",
"content": str(user_input)
}
)
response = anthroClient.messages.create(
max_tokens= 2048,
messages = messages,
model = "claude-3-opus-20240229"
)
return response.content
def geminiCall(prompt):
model = genai.GenerativeModel('gemini-pro')
# model = genai.GenerativeModel('gemini-pro-vision')
response = model.generate_content(str(prompt))
return response.text
def gpt4call(system_prompt, user_input = None):
prompt = system_prompt
messages = [
{
"role": "system",
"content": str(prompt)
}
]
if user_input:
messages.append({
"role": "user",
"content": str(user_input)
})
response = client.chat.completions.create(
model = "gpt-4-0125-preview",
response_format= {'type': 'json_object'},
messages= messages,
temperature=0.9
)
text_response = json.loads(response.choices[0].message.content)
return text_response
# def midjourney(prompt, user_input):
# with open("prompts/midJourney.txt") as journeyText:
# prompt = journeyText.read()
def gpt4visioncall(img_url, maintext, subtext, placeOfEvent, timingOfEvent, contactDetails):
with open("prompts/imgDescription.txt") as fileVision:
prompt = fileVision.read()
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": str(prompt) #+ str(txt)
},
{
"type": "image_url",
"image_url":{
"url": str(img_url)
}
}
]
}
]
response = client.chat.completions.create(
model = "gpt-4-vision-preview",
messages= messages,
#response_format= {"type": "json_object"},
temperature = 0.9
)
vision_response = response.choices[0].message.content
return vision_response
user_input = input("Please input your request for the logo [press enter to choose random]: ")
if user_input == "":
with open("prompts/random_logos.txt") as random_logos:
logo_lines = random_logos.readlines()
user_input = random.choice(logo_lines).strip()
print(f"User input: {user_input}")
# sys_prompt_file = open("prompts/inputParsing.txt", "r")
sys_prompt_file = open("prompts/logo_input_parsing.txt", "r")
sys_prompt = sys_prompt_file.read()
event_details = gpt4call(sys_prompt, user_input)
sys_prompt_file.close()
with open("prompts/logo_llm_element_placer.txt") as design_prompt_file:
design_prompt = design_prompt_file.read()
rich.print_json(data = event_details)
design_prompt = design_prompt.replace(r'{title}', str(event_details["title"]))
design_prompt = design_prompt.replace(r'{tagline}', str(event_details["tagline"]))
print(design_prompt)
final_results = gpt4call(design_prompt)
###### Fix for missing 'Text_component_details_LIST' in final_results ######
if 'Text_component_details_LIST' not in final_results:
if 'Logo_Component' in final_results:
if 'Text_component_details_LIST' in final_results['Logo_Component']:
final_results['Text_component_details_LIST'] = final_results['Logo_Component']['Text_component_details_LIST']
del final_results['Logo_Component']
print("There was a missing 'Text_component_details_LIST' in GPT response, but it was found in Logo_Component - moved to top level")
else:
raise Exception("Text_component_details_LIST not found in GPT response")
with open('gpt4_response.json', 'w') as f:
json.dump(final_results, f, indent=4)
rich.print_json(data = final_results)
design_prompt_file.close()