marinap commited on
Commit
95cdb44
·
1 Parent(s): c31bace

added Gallery style

Browse files
Files changed (1) hide show
  1. app.py +4 -10
app.py CHANGED
@@ -25,20 +25,14 @@ def url2img(url, resize = False, fix_height = 200):
25
  data = requests.get(url, allow_redirects = True).content
26
  img = Image.open(io.BytesIO(data))
27
  if resize:
28
- width, height = img.size
29
- img = img.resize((resize_wh(width, height, fix_height)))
30
  return img
31
 
32
- def resize_wh(w, h, fix_height):
33
- ratio = w / h
34
- w_n = int(fix_height * ratio)
35
- return w_n, fix_height
36
-
37
  def find_topk(text):
38
 
39
  print('text', text)
40
 
41
- top_k = 10
42
 
43
  text_data = model_multi.preprocess_text(text)
44
  text_features, text_embedding = model_multi.encode_text(text_data, return_features=True)
@@ -53,7 +47,7 @@ def find_topk(text):
53
  print('top_k_urls', top_k_urls)
54
  print(datetime.now().strftime("%H:%M:%S"))
55
 
56
- images = [url2img(url, resize = True) for url in top_k_urls]
57
 
58
  print('got PIL images')
59
  print(datetime.now().strftime("%H:%M:%S"))
@@ -97,7 +91,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
97
  prompt_box = gr.Textbox(label = 'Enter your prompt', lines = 3)
98
  btn_search = gr.Button("Find images")
99
 
100
- gallery = gr.Gallery()
101
  btn_search.click(find_topk, inputs = prompt_box, outputs = gallery)
102
 
103
  if __name__ == "__main__":
 
25
  data = requests.get(url, allow_redirects = True).content
26
  img = Image.open(io.BytesIO(data))
27
  if resize:
28
+ img.thumbnail([fix_height, fix_height], Image.LANCZOS)
 
29
  return img
30
 
 
 
 
 
 
31
  def find_topk(text):
32
 
33
  print('text', text)
34
 
35
+ top_k = 20
36
 
37
  text_data = model_multi.preprocess_text(text)
38
  text_features, text_embedding = model_multi.encode_text(text_data, return_features=True)
 
47
  print('top_k_urls', top_k_urls)
48
  print(datetime.now().strftime("%H:%M:%S"))
49
 
50
+ images = [url2img(url) for url in top_k_urls]
51
 
52
  print('got PIL images')
53
  print(datetime.now().strftime("%H:%M:%S"))
 
91
  prompt_box = gr.Textbox(label = 'Enter your prompt', lines = 3)
92
  btn_search = gr.Button("Find images")
93
 
94
+ gallery = gr.Gallery().style(columns=[5], rows=[4], object_fit="contain", height="auto")
95
  btn_search.click(find_topk, inputs = prompt_box, outputs = gallery)
96
 
97
  if __name__ == "__main__":