simonza commited on
Commit
bc13c1f
1 Parent(s): 40948e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -90
app.py CHANGED
@@ -6,7 +6,7 @@ from sentence_transformers import SentenceTransformer
6
  import transformers.models.clip.image_processing_clip
7
  import torch
8
  import gradio as gr
9
- from deep_translator import GoogleTranslator
10
  import shutil
11
  from PIL import Image
12
  import os
@@ -15,7 +15,6 @@ with open('pinecone_text.py' ,'w') as fb:
15
  fb.write(requests.get('https://storage.googleapis.com/gareth-pinecone-datasets/pinecone_text.py').text)
16
  import pinecone_text
17
 
18
-
19
  # init connection to pinecone
20
  pinecone.init(
21
  api_key="0898750a-ee05-44f1-ac8a-98c5fef92f4a", # app.pinecone.io
@@ -58,7 +57,6 @@ model = SentenceTransformer(
58
  device=device
59
  )
60
 
61
-
62
  def hybrid_scale(dense, sparse, alpha: float):
63
  if alpha < 0 or alpha > 1:
64
  raise ValueError("Alpha must be between 0 and 1")
@@ -101,7 +99,7 @@ def img_to_file_list(imgs):
101
 
102
  # Check whether the specified path exists or not
103
  isExist = os.path.exists('.'+'/'+path)
104
-
105
  if not isExist:
106
  print("Directory does not exists")
107
  # Create a new directory because it does not exist
@@ -110,7 +108,7 @@ def img_to_file_list(imgs):
110
 
111
  # Check whether the specified path exists or not
112
  isExist = os.path.exists(sub_path)
113
-
114
  if isExist:
115
  shutil.rmtree(sub_path)
116
 
@@ -118,7 +116,7 @@ def img_to_file_list(imgs):
118
 
119
  img_files = {'search'+str(counter["dir_num"]):[]}
120
  i = 0
121
-
122
  for img in imgs:
123
  img.save(sub_path+"/img_" + str(i) + ".png","PNG")
124
  img_files['search'+str(counter["dir_num"])].append(sub_path + '/' + 'img_'+ str(i) + ".png")
@@ -128,116 +126,55 @@ def img_to_file_list(imgs):
128
 
129
  return img_files['search'+str(counter["dir_num"]-1)]
130
 
 
 
131
 
 
132
 
133
- def hybrid_scale(dense, sparse, alpha: float):
134
- if alpha < 0 or alpha > 1:
135
- raise ValueError("Alpha must be between 0 and 1")
136
- # scale sparse and dense vectors to create hybrid search vecs
137
- hsparse = {
138
- 'indices': sparse['indices'],
139
- 'values': [v * (1 - alpha) for v in sparse['values']]
140
- }
141
- hdense = [v * alpha for v in dense]
142
- return hdense, hsparse
143
-
144
-
145
- def text_to_image(query, alpha, k_results):
146
- sparse = bm25.transform_query(query)
147
- dense = model.encode(query).tolist()
148
-
149
- # scale sparse and dense vectors
150
- hdense, hsparse = hybrid_scale(dense, sparse, alpha=alpha)
151
-
152
- # search
153
- result = index.query(
154
- top_k=k_results,
155
- vector=hdense,
156
- sparse_vector=hsparse,
157
- include_metadata=True
158
- )
159
- # used returned product ids to get images
160
- imgs = [images[int(r["id"])] for r in result["matches"]]
161
-
162
- description = []
163
- for x in result["matches"]:
164
- description.append( x["metadata"]['productDisplayName'] )
165
-
166
- return imgs, description
167
-
168
-
169
- def img_to_file_list(imgs):
170
- path = "searches"
171
- sub_path = './' + path + '/' + 'search' + '_' + str(counter["dir_num"])
172
-
173
- # Check whether the specified path exists or not
174
- isExist = os.path.exists('.'+'/'+path)
175
-
176
- if not isExist:
177
- print("Directory does not exists")
178
- # Create a new directory because it does not exist
179
- os.makedirs('.'+'/'+path, exist_ok = True)
180
- print("The new directory is created!")
181
-
182
- # Check whether the specified path exists or not
183
- isExist = os.path.exists(sub_path)
184
-
185
- if isExist:
186
- shutil.rmtree(sub_path)
187
-
188
- os.makedirs(sub_path, exist_ok = True)
189
-
190
- img_files = {'search'+str(counter["dir_num"]):[]}
191
- i = 0
192
-
193
- for img in imgs:
194
- img.save(sub_path+"/img_" + str(i) + ".png","PNG")
195
- img_files['search'+str(counter["dir_num"])].append(sub_path + '/' + 'img_'+ str(i) + ".png")
196
- i+=1
197
-
198
- counter["dir_num"]+=1
199
-
200
- return img_files['search'+str(counter["dir_num"]-1)]
201
 
 
 
 
202
 
 
 
203
 
204
- counter = {"dir_num": 1}
205
- img_files = {'x':[]}
206
 
207
- def fake_gan(text, alpha):
208
- text_eng=GoogleTranslator(source='iw', target='en').translate(text)
209
- imgs, descr = text_to_image(text_eng, alpha, 3)
210
  img_files = img_to_file_list(imgs)
211
  return img_files
212
 
213
  def fake_text(text, alpha):
214
  en_text = GoogleTranslator(source='iw', target='en').translate(text)
215
- img , descr = text_to_image(en_text, alpha, 3)
216
  return descr
217
 
218
 
219
- with gr.Blocks() as demo:
220
 
221
  with gr.Row():
222
 
223
- text = gr.Textbox(
224
- value = "ג'ינס כחול לגברים",
225
- label="Enter the product characteristics:",
226
- )
227
 
228
- alpha = gr.Slider(0, 1, step=0.01, label='Choose alpha:', value = 0.05)
229
 
230
  with gr.Row():
231
  btn = gr.Button("Generate image")
232
 
233
  with gr.Row():
234
  gallery = gr.Gallery(
235
- label="Generated images", show_label=False, elem_id="gallery"
236
- ).style(columns=[8], rows=[2], object_fit='scale-down', height='auto')
237
 
238
  with gr.Row():
239
  selected = gr.Textbox(label="Product description: ", interactive=False, value = "-----> Description <-------",placeholder="Selected")
240
 
 
 
 
241
  btn.click(fake_gan, inputs=[text, alpha], outputs=gallery)
242
 
243
  def get_select_index(evt: gr.SelectData,text,alpha):
@@ -245,7 +182,7 @@ with gr.Blocks() as demo:
245
  eng_text = fake_text(text, alpha)[evt.index]
246
  #heb_text = GoogleTranslator(source='en', target='iw').translate(eng_text)
247
  return eng_text
248
-
249
  gallery.select( fn=get_select_index, inputs=[text,alpha], outputs=selected )
250
 
251
- demo.launch(debug=True)
 
6
  import transformers.models.clip.image_processing_clip
7
  import torch
8
  import gradio as gr
9
+ from deep_translator import GoogleTranslator, single_detection
10
  import shutil
11
  from PIL import Image
12
  import os
 
15
  fb.write(requests.get('https://storage.googleapis.com/gareth-pinecone-datasets/pinecone_text.py').text)
16
  import pinecone_text
17
 
 
18
  # init connection to pinecone
19
  pinecone.init(
20
  api_key="0898750a-ee05-44f1-ac8a-98c5fef92f4a", # app.pinecone.io
 
57
  device=device
58
  )
59
 
 
60
  def hybrid_scale(dense, sparse, alpha: float):
61
  if alpha < 0 or alpha > 1:
62
  raise ValueError("Alpha must be between 0 and 1")
 
99
 
100
  # Check whether the specified path exists or not
101
  isExist = os.path.exists('.'+'/'+path)
102
+
103
  if not isExist:
104
  print("Directory does not exists")
105
  # Create a new directory because it does not exist
 
108
 
109
  # Check whether the specified path exists or not
110
  isExist = os.path.exists(sub_path)
111
+
112
  if isExist:
113
  shutil.rmtree(sub_path)
114
 
 
116
 
117
  img_files = {'search'+str(counter["dir_num"]):[]}
118
  i = 0
119
+
120
  for img in imgs:
121
  img.save(sub_path+"/img_" + str(i) + ".png","PNG")
122
  img_files['search'+str(counter["dir_num"])].append(sub_path + '/' + 'img_'+ str(i) + ".png")
 
126
 
127
  return img_files['search'+str(counter["dir_num"]-1)]
128
 
129
+ counter = {"dir_num": 1}
130
+ img_files = {'x':[]}
131
 
132
+ K = 5
133
 
134
+ def fake_gan(text, alpha):
135
+ detected_language = single_detection(text, api_key='d259a6dab3bb73b1d1c2bcc6fb62b9f4')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
+ if detected_language == 'iw':
138
+ text_eng=GoogleTranslator(source='iw', target='en').translate(text)
139
+ imgs, descr = text_to_image(text_eng, alpha, K)
140
 
141
+ elif detected_language == 'en':
142
+ imgs, descr = text_to_image(text, alpha, K)
143
 
 
 
144
 
 
 
 
145
  img_files = img_to_file_list(imgs)
146
  return img_files
147
 
148
  def fake_text(text, alpha):
149
  en_text = GoogleTranslator(source='iw', target='en').translate(text)
150
+ img , descr = text_to_image(en_text, alpha, K)
151
  return descr
152
 
153
 
154
+ with gr.Blocks(width = 300) as demo:
155
 
156
  with gr.Row():
157
 
158
+ text = gr.Textbox(
159
+ value = "blue jeans for men",
160
+ label="Enter the product characteristics:"
161
+ )
162
 
163
+ alpha = gr.Slider(0, 1, step=0.01, label='Choose alpha:', value = 0.05)
164
 
165
  with gr.Row():
166
  btn = gr.Button("Generate image")
167
 
168
  with gr.Row():
169
  gallery = gr.Gallery(
170
+ label="Generated images", show_label=False, elem_id="gallery", columns=[8], rows=[1], object_fit='scale-down', height=160)
 
171
 
172
  with gr.Row():
173
  selected = gr.Textbox(label="Product description: ", interactive=False, value = "-----> Description <-------",placeholder="Selected")
174
 
175
+ # show the results in gallery on enter key and button press
176
+
177
+ text.submit(fake_gan, inputs=[text, alpha], outputs=gallery)
178
  btn.click(fake_gan, inputs=[text, alpha], outputs=gallery)
179
 
180
  def get_select_index(evt: gr.SelectData,text,alpha):
 
182
  eng_text = fake_text(text, alpha)[evt.index]
183
  #heb_text = GoogleTranslator(source='en', target='iw').translate(eng_text)
184
  return eng_text
185
+
186
  gallery.select( fn=get_select_index, inputs=[text,alpha], outputs=selected )
187
 
188
+ demo.launch(width = 700)