That1BrainCell commited on
Commit
7bb3862
1 Parent(s): 276fa4f
Files changed (1) hide show
  1. app.py +125 -117
app.py CHANGED
@@ -251,124 +251,132 @@ with st.sidebar:
251
 
252
  top_similar_count = st.number_input("Top Similarities to be Displayed", value=3, min_value=1, step=1, format="%i")
253
 
254
- if st.button('Check for Infringement'):
255
- global log_output
256
 
257
- tab1, tab2 = st.tabs(["📊 Output", "🖥️ Console"])
258
-
259
- with tab2:
260
- log_output = st.empty()
261
-
262
- with tab1:
263
- with st.spinner('Processing...'):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
 
265
- for path in os.listdir('/home/user/app/embeddings'):
266
- print(path)
267
- if os.path.exists('/home/user/app/embeddings'):
268
- download_db()
269
- print("\u2713 Downloaded Database\n\n")
270
-
271
- with StreamCapture() as logger:
272
- top_similar_values = score(main_product, main_url, product_count, link_count, search_method, logger, log_output)
273
-
274
- st.success('✅ Processing complete!')
275
-
276
- st.subheader("📈 Cosine Similarity Scores")
277
-
278
- for main_text, main_vector, response, _ in top_similar_values:
279
- product_name = response['metadatas'][0][0]['product_name']
280
- link = response['metadatas'][0][0]['url']
281
- similar_text = response['metadatas'][0][0]['text']
282
- # similar_text_refined = imporve_text(similar_text)
283
- # main_text_refined = imporve_text(main_text)
284
-
285
- cosine_score = cosine_similarity([main_vector], response['embeddings'][0])[0][0]
286
-
287
- # Display the product information
288
- with st.expander(f"### Product: {product_name} - Score: {cosine_score:.4f}"):
289
- link = link.replace(" ","%20")
290
- st.markdown(f"[View Product Manual]({link})")
291
- tab1, tab2 = st.tabs(["Raw Text", "Refined Text"])
292
- with tab2:
293
- col1, col2 = st.columns(2)
294
- with col1:
295
- st.markdown(f"*Main Text:\n* {imporve_text(main_text)}")
296
- with col2:
297
- st.markdown(f"*Similar Text\n:* {imporve_text(similar_text)}")
298
-
299
- with tab1:
300
- col1, col2 = st.columns(2)
301
- with col1:
302
- st.markdown(f"*Main Text:* {main_text}")
303
- with col2:
304
- st.markdown(f"*Similar Text:* {similar_text}")
305
-
306
- if need_image == 'True':
307
- with st.spinner('Processing Images...'):
308
- emb_main , main_prod_imgs = get_image_embeddings(main_product)
309
- similar_prod = extract_similar_products(main_product)[0]
310
- emb_similar , similar_prod_imgs = get_image_embeddings(similar_prod)
311
-
312
- similarity_matrix = np.zeros((5, 5))
313
- for i in range(5):
314
- for j in range(5):
315
- similarity_matrix[i][j] = cosine_similarity([emb_main[i]], [emb_similar[j]])[0][0]
316
-
317
- st.subheader("Image Similarity")
318
- # Create an interactive heatmap
319
- fig = px.imshow(similarity_matrix,
320
- labels=dict(x=f"{similar_prod} Images", y=f"{main_product} Images", color="Similarity"),
321
- x=[f"Image {i+1}" for i in range(5)],
322
- y=[f"Image {i+1}" for i in range(5)],
323
- color_continuous_scale="Viridis")
324
-
325
- # Add title to the heatmap
326
- fig.update_layout(title="Image Similarity Heatmap")
327
-
328
- # Display the interactive heatmap
329
- st.plotly_chart(fig)
330
-
331
-
332
-
333
- @st.experimental_fragment
334
- def image_viewer():
335
- # Form to handle image selection
336
-
337
- st.subheader("Image Viewer")
338
-
339
- selected_row = st.selectbox('Select a row (Main Product Image)', [f'Image {i+1}' for i in range(5)])
340
- selected_col = st.selectbox('Select a column (Similar Product Image)', [f'Image {i+1}' for i in range(5)])
341
-
342
- # Get the selected indices from session state
343
- row_idx = int(selected_row.split()[1]) - 1
344
- col_idx = int(selected_col.split()[1]) - 1
345
-
346
- col1, col2 = st.columns(2)
347
-
348
- with col1:
349
- st.image(main_prod_imgs[row_idx], caption=f'Main Product Image {row_idx+1}', use_column_width=True)
350
- with col2:
351
- st.image(similar_prod_imgs[col_idx], caption=f'Similar Product Image {col_idx+1}', use_column_width=True)
352
-
353
- # Call the fragment
354
- image_viewer()
355
-
356
- def zip_folder(folder_path, zip_name):
357
- # Create a zip file from the folder
358
- shutil.make_archive(zip_name, 'zip', folder_path)
359
- return zip_name + '.zip'
360
-
361
- folder_path = '/home/user/app/embeddings'
362
- zip_name = 'embedding'
363
-
364
- if st.button("Download"):
365
- zip_file = zip_folder(folder_path, zip_name)
366
- with open(zip_file, "rb") as f:
367
- st.download_button(
368
- label="Download ZIP",
369
- data=f,
370
- file_name=zip_file,
371
- mime="application/zip"
372
- )
373
 
374
 
 
251
 
252
  top_similar_count = st.number_input("Top Similarities to be Displayed", value=3, min_value=1, step=1, format="%i")
253
 
 
 
254
 
255
+ col1,col2 = st.columns([7,3])
256
+
257
+ with col1:
258
+ run_streamlit = st.button('Check for Infringement')
259
+
260
+
261
+ if run_streamlit:
262
+ global log_output
263
+
264
+ tab1, tab2 = st.tabs(["📊 Output", "🖥️ Console"])
265
+
266
+ with tab2:
267
+ log_output = st.empty()
268
+
269
+ with tab1:
270
+ with st.spinner('Processing...'):
271
+
272
+ if len(os.listdir('/home/user/app/embeddings'))<2:
273
+ download_db()
274
+ print("\u2713 Downloaded Database\n\n")
275
+
276
+ with StreamCapture() as logger:
277
+ top_similar_values = score(main_product, main_url, product_count, link_count, search_method, logger, log_output)
278
+
279
+ st.success('✅ Processing complete!')
280
+
281
+ st.subheader("📈 Cosine Similarity Scores")
282
+
283
+ for main_text, main_vector, response, _ in top_similar_values:
284
+ product_name = response['metadatas'][0][0]['product_name']
285
+ link = response['metadatas'][0][0]['url']
286
+ similar_text = response['metadatas'][0][0]['text']
287
+ # similar_text_refined = imporve_text(similar_text)
288
+ # main_text_refined = imporve_text(main_text)
289
+
290
+ cosine_score = cosine_similarity([main_vector], response['embeddings'][0])[0][0]
291
+
292
+ # Display the product information
293
+ with st.expander(f"### Product: {product_name} - Score: {cosine_score:.4f}"):
294
+ link = link.replace(" ","%20")
295
+ st.markdown(f"[View Product Manual]({link})")
296
+ tab1, tab2 = st.tabs(["Raw Text", "Refined Text"])
297
+ with tab2:
298
+ col1, col2 = st.columns(2)
299
+ with col1:
300
+ st.markdown(f"*Main Text:\n* {imporve_text(main_text)}")
301
+ with col2:
302
+ st.markdown(f"*Similar Text\n:* {imporve_text(similar_text)}")
303
+
304
+ with tab1:
305
+ col1, col2 = st.columns(2)
306
+ with col1:
307
+ st.markdown(f"*Main Text:* {main_text}")
308
+ with col2:
309
+ st.markdown(f"*Similar Text:* {similar_text}")
310
+
311
+ if need_image == 'True':
312
+ with st.spinner('Processing Images...'):
313
+ emb_main , main_prod_imgs = get_image_embeddings(main_product)
314
+ similar_prod = extract_similar_products(main_product)[0]
315
+ emb_similar , similar_prod_imgs = get_image_embeddings(similar_prod)
316
+
317
+ similarity_matrix = np.zeros((5, 5))
318
+ for i in range(5):
319
+ for j in range(5):
320
+ similarity_matrix[i][j] = cosine_similarity([emb_main[i]], [emb_similar[j]])[0][0]
321
+
322
+ st.subheader("Image Similarity")
323
+ # Create an interactive heatmap
324
+ fig = px.imshow(similarity_matrix,
325
+ labels=dict(x=f"{similar_prod} Images", y=f"{main_product} Images", color="Similarity"),
326
+ x=[f"Image {i+1}" for i in range(5)],
327
+ y=[f"Image {i+1}" for i in range(5)],
328
+ color_continuous_scale="Viridis")
329
+
330
+ # Add title to the heatmap
331
+ fig.update_layout(title="Image Similarity Heatmap")
332
+
333
+ # Display the interactive heatmap
334
+ st.plotly_chart(fig)
335
+
336
+
337
+
338
+ @st.experimental_fragment
339
+ def image_viewer():
340
+ # Form to handle image selection
341
+
342
+ st.subheader("Image Viewer")
343
+
344
+ selected_row = st.selectbox('Select a row (Main Product Image)', [f'Image {i+1}' for i in range(5)])
345
+ selected_col = st.selectbox('Select a column (Similar Product Image)', [f'Image {i+1}' for i in range(5)])
346
+
347
+ # Get the selected indices from session state
348
+ row_idx = int(selected_row.split()[1]) - 1
349
+ col_idx = int(selected_col.split()[1]) - 1
350
+
351
+ col1, col2 = st.columns(2)
352
+
353
+ with col1:
354
+ st.image(main_prod_imgs[row_idx], caption=f'Main Product Image {row_idx+1}', use_column_width=True)
355
+ with col2:
356
+ st.image(similar_prod_imgs[col_idx], caption=f'Similar Product Image {col_idx+1}', use_column_width=True)
357
+
358
+ # Call the fragment
359
+ image_viewer()
360
+
361
+
362
+ @st.experimental_dialog("Confirm Database Backup")
363
+ def update():
364
+ st.write("Do you want to backup the new changes in the database?")
365
+ if st.button("Confirm",type="primary"):
366
+ st.write("Updating Database....")
367
+ st.session_state.update = {"Done": True}
368
+
369
+ update_db()
370
+
371
+ st.success('Backup Complete!', icon="✅")
372
+ time.sleep(2)
373
+ st.rerun()
374
+
375
+ if "update" not in st.session_state:
376
+ with col2:
377
+ update_button = st.button("Update Database",type="primary")
378
+ if update_button:
379
+ update()
380
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
381
 
382