Spaces:
Sleeping
Sleeping
File size: 6,222 Bytes
e7b1cac |
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
from openai import OpenAI
import os
import base64
import requests
from prompts import prompts
from constants import JSON_SCHEMA_FOR_GPT
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
client = OpenAI(api_key=OPENAI_API_KEY)
model = "gpt-4o"
title = "Caimera Mood board Expert"
def createAssistant(instruction_prompt):
instructions = instruction_prompt
assistant = client.beta.assistants.create(
name=title,
instructions=instructions,
model=model
)
return assistant.id
def saveFileOpenAI(location):
with open(location, "rb") as f:
file = client.files.create(file=f, purpose="vision")
os.remove(location)
return file.id
def startAssistantThread(file_id_enum, prompt_n, image_needed, json_mode_needed_or_not):
if json_mode_needed_or_not == "yes":
if image_needed == "yes":
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt_n
}
],
}
]
for file_id in file_id_enum:
messages[0]["content"].append({
"type": "image_file",
"image_file": {"file_id": file_id}
})
else:
messages = [
{
"role": "user",
"content": prompt_n}]
thread = client.beta.threads.create(messages=messages)
else:
if image_needed == "yes":
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt_n
}
],
}
]
for file_id in file_id_enum:
messages[0]["content"].append({
"type": "image_file",
"image_file": {"file_id": file_id}
})
else:
messages = [
{
"role": "user",
"content": prompt_n}]
thread = client.beta.threads.create(messages=messages)
return thread.id
def runAssistant(thread_id, assistant_id):
run = client.beta.threads.runs.create(thread_id=thread_id, assistant_id=assistant_id)
return run.id
def checkRunStatus(thread_id, run_id):
run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run_id)
return run.status
def retrieveThread(thread_id):
thread_messages = client.beta.threads.messages.list(thread_id)
list_messages = thread_messages.data
thread_messages = []
for message in list_messages:
obj = {}
obj['content'] = message.content[0].text.value
obj['role'] = message.role
thread_messages.append(obj)
return thread_messages[::-1]
def addMessageToThread(thread_id, prompt_n):
thread_message = client.beta.threads.messages.create(thread_id, role="user", content=prompt_n)
def create_chat_completion_request_open_ai_for_summary(prompt, json_mode, schema_name="",
json_schema="",
system_message="You are expert in Fashion "
"Shoots"):
import requests
if json_mode == "No":
url = "https://api.openai.com/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {OPENAI_API_KEY}"
}
data = {
"model": "gpt-4o",
"messages": [
{
"role": "system",
"content": system_message
},
{
"role": "user",
"content": prompt
}
]
}
response = requests.post(url, headers=headers, json=data)
json_response = response.json()
else:
url = "https://api.openai.com/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {OPENAI_API_KEY}"
}
data = {
"model": "gpt-4o",
"messages": [
{
"role": "system",
"content": "You are expert in creating prompts for Fashion Shoots."
},
{
"role": "user",
"content": prompt
}
],
"response_format": {"type": "json_schema", "json_schema": {"name": schema_name, "strict": True, "schema":
json_schema}}
}
response = requests.post(url, headers=headers, json=data)
json_response = response.json()
print(json_response)
return json_response["choices"][0]["message"]["content"]
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
def create_image_completion_request_gpt(image_path, prompt):
base64_image = encode_image(image_path)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {OPENAI_API_KEY}"
}
payload = {
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
}
response = requests.post("https://api.openai.com/v1/chat/completions",
headers=headers, json=payload)
json_resp = response.json()
return json_resp["choices"][0]["message"]["content"]
|