That1BrainCell commited on
Commit
42046f1
·
verified ·
1 Parent(s): c3492f9

Infringement first commit

Browse files
Files changed (1) hide show
  1. app.py +132 -0
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import concurrent.futures
3
+ from functools import partial
4
+ import numpy as np
5
+ from io import StringIO
6
+ import sys
7
+ import time
8
+
9
+ # File Imports
10
+ from embedding import get_embeddings # Ensure this file/module is available
11
+ from preprocess import filtering # Ensure this file/module is available
12
+ from search import *
13
+
14
+ # Cosine Similarity Function
15
+ def cosine_similarity(vec1, vec2):
16
+ vec1 = np.array(vec1)
17
+ vec2 = np.array(vec2)
18
+
19
+ dot_product = np.dot(vec1, vec2)
20
+ magnitude_vec1 = np.linalg.norm(vec1)
21
+ magnitude_vec2 = np.linalg.norm(vec2)
22
+
23
+ if magnitude_vec1 == 0 or magnitude_vec2 == 0:
24
+ return 0.0
25
+
26
+ cosine_sim = dot_product / (magnitude_vec1 * magnitude_vec2)
27
+ return cosine_sim
28
+
29
+ # Logger class to capture output
30
+ class StreamCapture:
31
+ def __init__(self):
32
+ self.output = StringIO()
33
+ self._stdout = sys.stdout
34
+
35
+ def __enter__(self):
36
+ sys.stdout = self.output
37
+ return self.output
38
+
39
+ def __exit__(self, exc_type, exc_val, exc_tb):
40
+ sys.stdout = self._stdout
41
+
42
+ # Main Function
43
+ def score(main_product, main_url, search, logger, log_area):
44
+ data = {}
45
+
46
+ if search == 'all':
47
+ similar = extract_similar_products(main_product)[:1]
48
+
49
+ with concurrent.futures.ThreadPoolExecutor() as executor:
50
+ futures = []
51
+
52
+ search_functions = [search_google, search_duckduckgo, search_github, search_wikipedia]
53
+
54
+ for search_func in search_functions:
55
+ futures.append(executor.submit(partial(filtering, search_func(similar), main_product, similar)))
56
+
57
+ for future in concurrent.futures.as_completed(futures):
58
+ data[similar] = future.result()
59
+
60
+ else:
61
+ similar = extract_similar_products(main_product)[:1]
62
+
63
+ for product in similar:
64
+
65
+ if search == 'google':
66
+ data[product] = filtering(search_google(product), main_product, product)
67
+ elif search == 'duckduckgo':
68
+ data[product] = filtering(search_duckduckgo(product), main_product, product)
69
+ elif search == 'archive':
70
+ data[product] = filtering(search_archive(product), main_product, product)
71
+ elif search == 'github':
72
+ data[product] = filtering(search_github(product), main_product, product)
73
+ elif search == 'wikipedia':
74
+ data[product] = filtering(search_wikipedia(product), main_product, product)
75
+
76
+ logger.write("\n\nFiltered Links ------------------>\n")
77
+ logger.write(str(data) + "\n")
78
+ log_area.text(logger.getvalue())
79
+
80
+ logger.write("\n\nCreating Main product Embeddings ---------->\n")
81
+ main_result, main_embedding = get_embeddings(main_url)
82
+ log_area.text(logger.getvalue())
83
+
84
+ cosine_sim_scores = []
85
+
86
+ logger.write("\n\nCreating Similar product Embeddings ---------->\n")
87
+ log_area.text(logger.getvalue())
88
+
89
+ print("main",main_embedding)
90
+
91
+ for product in data:
92
+ for link in data[product][:2]:
93
+
94
+ similar_result, similar_embedding = get_embeddings(link)
95
+ log_area.text(logger.getvalue())
96
+
97
+ print(similar_embedding)
98
+ for i in range(len(main_embedding)):
99
+ score = cosine_similarity(main_embedding[i], similar_embedding[i])
100
+ cosine_sim_scores.append((product, link, i, score))
101
+ log_area.text(logger.getvalue())
102
+
103
+ logger.write("--------------- DONE -----------------\n")
104
+ log_area.text(logger.getvalue())
105
+ return cosine_sim_scores, main_result
106
+
107
+ # Streamlit Interface
108
+ st.title("Product Infringement Checker")
109
+
110
+ # Inputs
111
+ main_product = st.text_input('Enter Main Product Name', 'Philips led 7w bulb')
112
+ main_url = st.text_input('Enter Main Product Manual URL', 'https://www.assets.signify.com/is/content/PhilipsConsumer/PDFDownloads/Colombia/technical-sheets/ODLI20180227_001-UPD-es_CO-Ficha_Tecnica_LED_MR16_Master_7W_Dim_12V_CRI90.pdf')
113
+ search_method = st.selectbox('Choose Search Engine', ['duckduckgo', 'google', 'archive', 'github', 'wikipedia', 'all'])
114
+
115
+ if st.button('Check for Infringement'):
116
+ log_output = st.empty() # Placeholder for log output
117
+
118
+ with st.spinner('Processing...'):
119
+ with StreamCapture() as logger:
120
+ cosine_sim_scores, main_result = score(main_product, main_url, search_method, logger, log_output)
121
+
122
+ st.success('Processing complete!')
123
+
124
+ st.subheader("Cosine Similarity Scores")
125
+
126
+ # = score(main_product, main_url, search, logger, log_output)
127
+ tags = ['Introduction', 'Specifications', 'Product Overview', 'Safety Information', 'Installation Instructions', 'Setup and Configuration', 'Operation Instructions', 'Maintenance and Care', 'Troubleshooting', 'Warranty Information', 'Legal Information']
128
+
129
+ for product, link, index, value in cosine_sim_scores:
130
+ if not index:
131
+ st.write(f"Product: {product}, Link: {link}")
132
+ st.write(f"{tags[index]:<20} Cosine Similarity Score: {value:.2f}")