Spaces:
Sleeping
Sleeping
from openai import OpenAI | |
import streamlit as st | |
from PIL import Image | |
from io import BytesIO | |
import base64 | |
import json | |
import re | |
# Convert Image to Base64 | |
def im_2_b64(image): | |
image = Image.open(image) | |
buff = BytesIO() | |
image.save(buff, format="JPEG") | |
img_str = base64.b64encode(buff.getvalue()) | |
return img_str | |
RANDOM_SEED = 42 | |
client = OpenAI(api_key=st.secrets["OPENAI_KEY"]) | |
with_prompt = """ | |
In the following task, you will be presented with a image and a story that is, in some manner, related to that goal. You will be given a specific goal and entity (generally, person or object), and be asked to identify condition necessary for that goal and the alternate condition that could prevent that goal. | |
Conditions for the output: | |
1. Condition: The condition is the necessary condition for the goal to be achieved. If the condition is not met, the goal cannot be achieved. The condition must be related to the entity. | |
2. Alternate Condition: The alternate condition is a different version of the condition that would prevent the goal from being achieved. It is likely that this alternate condition will contradict information provided in the images and/or story. | |
Output in a python dictionary where it should have the following keys: 'condition', 'alternate_condition'. | |
Story: {story} | |
Entity: {entity} | |
Goal: {goal} | |
""" | |
wo_prompt = """ | |
In the following task, you will be presented with a image and a story that is, in some manner, related to that goal. You will be given a specific goal, and be asked to identify an entity (person or object), condition necessary for that goal and the alternate condition that could prevent that goal. | |
Conditions for the output: | |
1. Entity: The entity is the person or object that the goal is related to. The entity should be a crucial part for achieving the goal. | |
2. Condition: The condition is the necessary condition for the goal to be achieved. If the condition is not met, the goal cannot be achieved. The condition must be related to the entity. | |
3. Alternate Condition: The alternate condition is a different version of the condition that would prevent the goal from being achieved. It is likely that this alternate condition will contradict information provided in the images and/or story. | |
Output in a python dictionary where it should have the following keys: 'entity', 'condition', 'alternate_condition'. | |
Story: {story} | |
Goal: {goal} | |
""" | |
data = { | |
"Story id": [], | |
"Prompt": [], | |
"entity": [], | |
"agent": [], | |
"story": [], | |
"Image1": [], | |
"Image2": [], | |
"Image3": [], | |
"GPT-4 Output": [], | |
} | |
def get_gpt4V_response(story, goal, entity, image, temperature=0.5): | |
# Convert image to base64 | |
image_b64 = im_2_b64(image) | |
image_url = f"data:image/jpeg;base64,{image_b64.decode('utf-8')}" | |
st.write("β Image converted") | |
if entity: | |
prompt = with_prompt | |
now_prompt = prompt.format(story=story, goal=goal, entity=entity) | |
else: | |
prompt = wo_prompt | |
now_prompt = prompt.format(story=story, goal=goal) | |
content = [ | |
{"type": "text", "text": now_prompt}, | |
] | |
st.write("β Prompt created") | |
content.append({ | |
"type": "image_url", | |
"image_url": { | |
"url": image_url, | |
}, | |
}) | |
st.write("π Getting Response from GPT4V") | |
response = client.chat.completions.create( | |
model="gpt-4-vision-preview", | |
seed=RANDOM_SEED, | |
messages=[ | |
{ | |
"role": "user", | |
"content": content | |
} | |
], | |
temperature=temperature, | |
max_tokens=1024, | |
# top_p=1, | |
# frequency_penalty=0, | |
# presence_penalty=0, | |
) | |
print(response) | |
print("Prompt:") | |
print(now_prompt) | |
out = response.choices[0].message.content | |
print("OUTPUT:", out) | |
print("====================================") | |
print() | |
st.write("β Response generated") | |
return out | |
def get_str_to_json(st): | |
st = re.sub(r"```python", "", st) | |
st = re.sub(r"```", "", st) | |
st = re.sub(r"'(.*)': '(.*)'", r'"\1": "\2"', st) | |
st = re.sub(r"'(.*)': (.*)", r'"\1": \2', st) | |
st = re.sub(r"(.*): '(.*)'", r'\1: "\2"', st) | |
st = st.replace("\\'", "'") | |
st = st.replace('\\"', '"') | |
st = st.replace("True", "true") | |
st = st.replace("False", "false") | |
st = st.replace("None", "null") | |
st = st.replace("nan", "null") | |
st = st.replace("inf", "null") | |
st = st.replace("-inf", "null") | |
st = st.replace(",,", ",null,") | |
st = st.replace(",]", ",null]") | |
st = st.replace(",}", ",null}") | |
st = st.replace(",,", ",null,") | |
st = st.strip() | |
st = json.loads(st) | |
return st | |