4rtemi5 commited on
Commit
6101644
β€’
1 Parent(s): e1f96cd

text improvements, CC is the default dataset, check if URLS can be loaded before displaying, small fixes

Browse files
Files changed (2) hide show
  1. image2text.py +7 -3
  2. text2image.py +25 -6
image2text.py CHANGED
@@ -21,7 +21,7 @@ def app():
21
 
22
  🀌 Italian mode on! 🀌
23
 
24
- For example, try to write "gatto" (cat) in the space for label1 and "dog" (cane) in the space for label2 and the run
25
  "classify"!
26
 
27
  """
@@ -62,7 +62,7 @@ def app():
62
  text_embeds.extend(text_encoder(c, model, tokenizer))
63
 
64
  text_embeds = jnp.array(text_embeds)
65
- image_raw = requests.get(image_url, stream=True).raw
66
 
67
  image = Image.open(image_raw).convert("RGB")
68
  transform = get_image_transform(model.config.vision_config.image_size)
@@ -81,5 +81,9 @@ def app():
81
 
82
  with col2:
83
  st.image(image)
84
-
85
  gc.collect()
 
 
 
 
 
 
21
 
22
  🀌 Italian mode on! 🀌
23
 
24
+ For example, try to write "gatto" (cat) in the space for label1 and "cane" (dog) in the space for label2 and the run
25
  "classify"!
26
 
27
  """
 
62
  text_embeds.extend(text_encoder(c, model, tokenizer))
63
 
64
  text_embeds = jnp.array(text_embeds)
65
+ image_raw = requests.get(image_url, stream=True,).raw
66
 
67
  image = Image.open(image_raw).convert("RGB")
68
  transform = get_image_transform(model.config.vision_config.image_size)
 
81
 
82
  with col2:
83
  st.image(image)
 
84
  gc.collect()
85
+
86
+ elif image_url:
87
+ image_raw = requests.get(image_url, stream=True,).raw
88
+ image = Image.open(image_raw).convert("RGB")
89
+ st.image(image)
text2image.py CHANGED
@@ -4,6 +4,8 @@ import requests
4
  import zipfile
5
  import natsort
6
  import gc
 
 
7
 
8
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
9
  from stqdm import stqdm
@@ -96,6 +98,13 @@ def get_image_transform(image_size):
96
  )
97
 
98
 
 
 
 
 
 
 
 
99
  def app():
100
 
101
  st.title("From Text to Image")
@@ -104,9 +113,9 @@ def app():
104
 
105
  ### πŸ‘‹ Ciao!
106
 
107
- Here you can search for images in the Unsplash 25k Photos dataset and the Conceptual Caption dataset.
108
- You will see most queries make sense. When you see errors, there might be two possibilities: the model is answering
109
- in a wrong way or the image you are looking for and the model is giving you the best answer it can get.
110
 
111
 
112
 
@@ -143,7 +152,7 @@ def app():
143
  with col1:
144
  query = st.text_input("... or insert an Italian query text")
145
  with col2:
146
- dataset_name = st.selectbox("IR dataset", ["Unsplash", "CC"])
147
 
148
  query = suggestions[sugg_idx] if sugg_idx > -1 else query if query else ""
149
 
@@ -167,11 +176,21 @@ def app():
167
  else:
168
  raise ValueError()
169
 
 
 
170
  image_paths = utils.find_image(
171
- query, model, dataset, tokenizer, image_features, 1, dataset_name
172
  )
173
 
174
- st.image(image_paths)
 
 
 
 
 
 
 
 
175
 
176
  gc.collect()
177
 
 
4
  import zipfile
5
  import natsort
6
  import gc
7
+ from PIL import Image
8
+ from PIL import UnidentifiedImageError
9
 
10
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
11
  from stqdm import stqdm
 
98
  )
99
 
100
 
101
+ headers = {
102
+ #'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
103
+ 'User-Agent': 'Googlebot-Image/1.0', # Pretend to be googlebot
104
+ 'X-Forwarded-For': '64.18.15.200'
105
+ }
106
+
107
+
108
  def app():
109
 
110
  st.title("From Text to Image")
 
113
 
114
  ### πŸ‘‹ Ciao!
115
 
116
+ Here you can search for ~150.000 images in the Conceptual Captions dataset (CC) or in the Unsplash 25k Photos dataset.
117
+ Even though we did not train on any of these images you will see most queries make sense. When you see errors, there might be two possibilities:
118
+ the model is answering in a wrong way or the image you are looking for are not in the dataset and the model is giving you the best answer it can get.
119
 
120
 
121
 
 
152
  with col1:
153
  query = st.text_input("... or insert an Italian query text")
154
  with col2:
155
+ dataset_name = st.selectbox("IR dataset", ["CC", "Unsplash"])
156
 
157
  query = suggestions[sugg_idx] if sugg_idx > -1 else query if query else ""
158
 
 
176
  else:
177
  raise ValueError()
178
 
179
+ N = 3
180
+
181
  image_paths = utils.find_image(
182
+ query, model, dataset, tokenizer, image_features, N, dataset_name
183
  )
184
 
185
+ for i, image_url in enumerate(image_paths):
186
+ try:
187
+ image_raw = requests.get(image_url, stream=True, allow_redirects=True, headers=headers).raw
188
+ image = Image.open(image_raw).convert("RGB")
189
+ st.image(image)
190
+ break
191
+ except (UnidentifiedImageError) as e:
192
+ if i == N - 1:
193
+ st.text(f'Tried to show {N} different image URLS but none of them were reachabele.\nMaybe try a different query?')
194
 
195
  gc.collect()
196