dipta007 commited on
Commit
c4e381e
β€’
1 Parent(s): 3ceccf2
Files changed (4) hide show
  1. .streamlit/secrets.toml +1 -0
  2. __pycache__/utils.cpython-310.pyc +0 -0
  3. app.py +52 -0
  4. utils.py +119 -0
.streamlit/secrets.toml ADDED
@@ -0,0 +1 @@
 
 
1
+ OPENAI_KEY="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"
__pycache__/utils.cpython-310.pyc ADDED
Binary file (3.76 kB). View file
 
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ from utils import get_gpt4V_response
4
+
5
+
6
+ st.set_page_config(page_title="GPT-4V Demo", page_icon="🧠", layout="wide")
7
+
8
+ with st.sidebar:
9
+ st.title("Parameters")
10
+ st.write("This is a demo of GPT-4V model. It takes a story, goal, entity and an image as input and generates a response.")
11
+
12
+ st.subheader("Sampling Temperature")
13
+ temperature = st.slider(label="", min_value=0.1, max_value=1.0, value=0.5, step=0.1)
14
+ st.write("The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.")
15
+
16
+ st.subheader("Entity?")
17
+ entity_opt = st.radio(label="With or Without", options=[1, 0], format_func=lambda x: ["Without", "With"][x])
18
+
19
+
20
+ def main():
21
+ global temperature, entity
22
+ st.title('What can go wrong?')
23
+
24
+ col1, col2 = st.columns(2)
25
+
26
+ with col1:
27
+ story = st.text_area("Story", placeholder="Enter the story here")
28
+
29
+ goal = st.text_input("Goal", placeholder="Enter the goal here")
30
+
31
+ entity = None
32
+ if entity_opt:
33
+ entity = st.text_input("Entity", placeholder="Enter the entity here")
34
+
35
+ image = st.file_uploader("Upload Image", type=['jpg', 'png'])
36
+
37
+ if image:
38
+ cols = st.columns(3)
39
+ with cols[1]:
40
+ st.image(image, caption="Uploaded Image", use_column_width=True)
41
+
42
+
43
+ if st.button("Submit"):
44
+ if not story or not goal or (entity_opt and not entity) or not image:
45
+ st.error("Please fill all the fields")
46
+ return
47
+ with col2:
48
+ with st.status("Generating response...", expanded=True):
49
+ response = get_gpt4V_response(story, goal, entity, image, temperature=temperature)
50
+ st.write(response)
51
+
52
+ main()
utils.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+ import streamlit as st
3
+ from PIL import Image
4
+ from io import BytesIO
5
+ import base64
6
+
7
+ # Convert Image to Base64
8
+ def im_2_b64(image):
9
+ image = Image.open(image)
10
+ buff = BytesIO()
11
+ image.save(buff, format="JPEG")
12
+ img_str = base64.b64encode(buff.getvalue())
13
+ return img_str
14
+
15
+
16
+ RANDOM_SEED = 42
17
+ client = OpenAI(api_key=st.secrets["OPENAI_KEY"])
18
+
19
+
20
+ with_prompt = """
21
+ 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.
22
+
23
+ Conditions for the output:
24
+ 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.
25
+
26
+ 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.
27
+
28
+ Output in a python dictionary where it should have the following keys: 'condition', 'alternate_condition'.
29
+
30
+ Story: {story}
31
+
32
+ Entity: {entity}
33
+
34
+ Goal: {goal}
35
+ """
36
+
37
+ wo_prompt = """
38
+ 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.
39
+
40
+ Conditions for the output:
41
+ 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.
42
+
43
+ 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.
44
+
45
+ 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.
46
+
47
+ Output in a python dictionary where it should have the following keys: 'entity', 'condition', 'alternate_condition'.
48
+
49
+ Story: {story}
50
+
51
+ Goal: {goal}
52
+ """
53
+
54
+ data = {
55
+ "Story id": [],
56
+ "Prompt": [],
57
+ "entity": [],
58
+ "agent": [],
59
+ "story": [],
60
+ "Image1": [],
61
+ "Image2": [],
62
+ "Image3": [],
63
+ "GPT-4 Output": [],
64
+ }
65
+
66
+
67
+ def get_gpt4V_response(story, goal, entity, image, temperature=0.5):
68
+ # Convert image to base64
69
+ image_b64 = im_2_b64(image)
70
+ image_url = f"data:image/jpeg;base64,{image_b64.decode('utf-8')}"
71
+ st.write("βœ… Image converted")
72
+
73
+ if entity:
74
+ prompt = with_prompt
75
+ now_prompt = prompt.format(story=story, goal=goal, entity=entity)
76
+ else:
77
+ prompt = wo_prompt
78
+ now_prompt = prompt.format(story=story, goal=goal)
79
+
80
+ content = [
81
+ {"type": "text", "text": now_prompt},
82
+ ]
83
+
84
+ st.write("βœ… Prompt created")
85
+
86
+ content.append({
87
+ "type": "image_url",
88
+ "image_url": {
89
+ "url": image_url,
90
+ },
91
+ })
92
+
93
+ st.write("πŸš€ Getting Response from GPT4V")
94
+ response = client.chat.completions.create(
95
+ model="gpt-4-vision-preview",
96
+ seed=RANDOM_SEED,
97
+ messages=[
98
+ {
99
+ "role": "user",
100
+ "content": content
101
+ }
102
+ ],
103
+ temperature=temperature,
104
+ max_tokens=256,
105
+ # top_p=1,
106
+ # frequency_penalty=0,
107
+ # presence_penalty=0,
108
+ )
109
+ print(response)
110
+ print("Prompt:")
111
+ print(now_prompt)
112
+ out = response.choices[0].message.content
113
+ print("OUTPUT:", out)
114
+ print("====================================")
115
+ print()
116
+
117
+ st.write("βœ… Response generated")
118
+
119
+ return out