dipta007 commited on
Commit
f49ac62
Β·
1 Parent(s): 0204b93

added multiple images support

Browse files
Files changed (2) hide show
  1. app.py +10 -7
  2. utils.py +13 -9
app.py CHANGED
@@ -32,21 +32,24 @@ def main():
32
 
33
  goal = st.text_area("Goal", placeholder="Enter the goal 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
 
51
  try:
52
  response_json = get_str_to_json(response)
 
32
 
33
  goal = st.text_area("Goal", placeholder="Enter the goal here")
34
 
35
+ images = st.file_uploader("Upload Image", type=['jpg', 'png'], accept_multiple_files=True)
36
 
37
+ if images:
38
+ cols = st.columns(len(images))
39
+
40
+ for i, image in enumerate(images):
41
+ with cols[i]:
42
+ image = Image.open(image)
43
+ st.image(image, caption="Uploaded Image", use_column_width=True)
44
 
45
 
46
  if st.button("Submit"):
47
+ if not story or not goal or (entity_opt and not entity) or not images:
48
  st.error("Please fill all the fields")
49
  return
50
  with col2:
51
  with st.status("Generating response...", expanded=True):
52
+ response = get_gpt4V_response(story, goal, entity, images, temperature=temperature)
53
 
54
  try:
55
  response_json = get_str_to_json(response)
utils.py CHANGED
@@ -68,10 +68,13 @@ data = {
68
  }
69
 
70
 
71
- def get_gpt4V_response(story, goal, entity, image, temperature=0.5):
72
  # Convert image to base64
73
- image_b64 = im_2_b64(image)
74
- image_url = f"data:image/jpeg;base64,{image_b64.decode('utf-8')}"
 
 
 
75
  st.write("βœ… Image converted")
76
 
77
  if entity:
@@ -87,12 +90,13 @@ def get_gpt4V_response(story, goal, entity, image, temperature=0.5):
87
 
88
  st.write("βœ… Prompt created")
89
 
90
- content.append({
91
- "type": "image_url",
92
- "image_url": {
93
- "url": image_url,
94
- },
95
- })
 
96
 
97
  st.write("πŸš€ Getting Response from GPT4V")
98
  response = client.chat.completions.create(
 
68
  }
69
 
70
 
71
+ def get_gpt4V_response(story, goal, entity, images, temperature=0.5):
72
  # Convert image to base64
73
+ image_urls = []
74
+ for i, image in enumerate(images):
75
+ image_b64 = im_2_b64(image)
76
+ image_url = f"data:image/jpeg;base64,{image_b64.decode('utf-8')}"
77
+ image_urls.append(image_url)
78
  st.write("βœ… Image converted")
79
 
80
  if entity:
 
90
 
91
  st.write("βœ… Prompt created")
92
 
93
+ for image_url in image_urls:
94
+ content.append({
95
+ "type": "image_url",
96
+ "image_url": {
97
+ "url": image_url,
98
+ },
99
+ })
100
 
101
  st.write("πŸš€ Getting Response from GPT4V")
102
  response = client.chat.completions.create(