Spaces:
Sleeping
Sleeping
File size: 4,701 Bytes
c4e381e 0276c23 4b1f81a 0276c23 c4e381e 0276c23 c4e381e 0276c23 4b1f81a 0276c23 4b1f81a |
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 |
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
|