Wrong Deleting
Browse files- infridgement_score.py +0 -174
infridgement_score.py
DELETED
@@ -1,174 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import concurrent.futures
|
3 |
-
from concurrent.futures import ThreadPoolExecutor,as_completed
|
4 |
-
from functools import partial
|
5 |
-
import numpy as np
|
6 |
-
from io import StringIO
|
7 |
-
import sys
|
8 |
-
import time
|
9 |
-
|
10 |
-
# File Imports
|
11 |
-
from embedding import get_embeddings # Ensure this file/module is available
|
12 |
-
from preprocess import filtering # Ensure this file/module is available
|
13 |
-
from search import *
|
14 |
-
|
15 |
-
# Cosine Similarity Function
|
16 |
-
def cosine_similarity(vec1, vec2):
|
17 |
-
vec1 = np.array(vec1)
|
18 |
-
vec2 = np.array(vec2)
|
19 |
-
|
20 |
-
dot_product = np.dot(vec1, vec2)
|
21 |
-
magnitude_vec1 = np.linalg.norm(vec1)
|
22 |
-
magnitude_vec2 = np.linalg.norm(vec2)
|
23 |
-
|
24 |
-
if magnitude_vec1 == 0 or magnitude_vec2 == 0:
|
25 |
-
return 0.0
|
26 |
-
|
27 |
-
cosine_sim = dot_product / (magnitude_vec1 * magnitude_vec2)
|
28 |
-
return cosine_sim
|
29 |
-
|
30 |
-
# Logger class to capture output
|
31 |
-
class StreamCapture:
|
32 |
-
def __init__(self):
|
33 |
-
self.output = StringIO()
|
34 |
-
self._stdout = sys.stdout
|
35 |
-
|
36 |
-
def __enter__(self):
|
37 |
-
sys.stdout = self.output
|
38 |
-
return self.output
|
39 |
-
|
40 |
-
def __exit__(self, exc_type, exc_val, exc_tb):
|
41 |
-
sys.stdout = self._stdout
|
42 |
-
|
43 |
-
# Main Function
|
44 |
-
def score(main_product, main_url, product_count, link_count, search, logger, log_area):
|
45 |
-
data = {}
|
46 |
-
similar_products = extract_similar_products(main_product)[:product_count]
|
47 |
-
|
48 |
-
if search == 'All':
|
49 |
-
|
50 |
-
def process_product(product, search_function, main_product):
|
51 |
-
search_result = search_function(product)
|
52 |
-
return filtering(search_result, main_product, product, link_count)
|
53 |
-
|
54 |
-
|
55 |
-
search_functions = {
|
56 |
-
'google': search_google,
|
57 |
-
'duckduckgo': search_duckduckgo,
|
58 |
-
# 'archive': search_archive,
|
59 |
-
'github': search_github,
|
60 |
-
'wikipedia': search_wikipedia
|
61 |
-
}
|
62 |
-
|
63 |
-
with ThreadPoolExecutor() as executor:
|
64 |
-
future_to_product_search = {
|
65 |
-
executor.submit(process_product, product, search_function, main_product): (product, search_name)
|
66 |
-
for product in similar_products
|
67 |
-
for search_name, search_function in search_functions.items()
|
68 |
-
}
|
69 |
-
|
70 |
-
for future in as_completed(future_to_product_search):
|
71 |
-
product, search_name = future_to_product_search[future]
|
72 |
-
try:
|
73 |
-
if product not in data:
|
74 |
-
data[product] = {}
|
75 |
-
data[product] = future.result()
|
76 |
-
except Exception as e:
|
77 |
-
print(f"Error processing product {product} with {search_name}: {e}")
|
78 |
-
|
79 |
-
else:
|
80 |
-
|
81 |
-
for product in similar_products:
|
82 |
-
|
83 |
-
if search == 'google':
|
84 |
-
data[product] = filtering(search_google(product), main_product, product, link_count)
|
85 |
-
elif search == 'duckduckgo':
|
86 |
-
data[product] = filtering(search_duckduckgo(product), main_product, product, link_count)
|
87 |
-
elif search == 'archive':
|
88 |
-
data[product] = filtering(search_archive(product), main_product, product, link_count)
|
89 |
-
elif search == 'github':
|
90 |
-
data[product] = filtering(search_github(product), main_product, product, link_count)
|
91 |
-
elif search == 'wikipedia':
|
92 |
-
data[product] = filtering(search_wikipedia(product), main_product, product, link_count)
|
93 |
-
|
94 |
-
logger.write("\n\nFiltered Links ------------------>\n")
|
95 |
-
logger.write(str(data) + "\n")
|
96 |
-
log_area.text(logger.getvalue())
|
97 |
-
|
98 |
-
logger.write("\n\nCreating Main product Embeddings ---------->\n")
|
99 |
-
main_result, main_embedding = get_embeddings(main_url,tag_option)
|
100 |
-
log_area.text(logger.getvalue())
|
101 |
-
|
102 |
-
print("main",main_embedding)
|
103 |
-
|
104 |
-
cosine_sim_scores = []
|
105 |
-
|
106 |
-
logger.write("\n\nCreating Similar product Embeddings ---------->\n")
|
107 |
-
log_area.text(logger.getvalue())
|
108 |
-
|
109 |
-
|
110 |
-
for product in data:
|
111 |
-
|
112 |
-
if len(data[product])==0:
|
113 |
-
logger.write("\n\nNo Product links Found Increase No of Links or Change Search Source\n")
|
114 |
-
log_area.text(logger.getvalue())
|
115 |
-
|
116 |
-
cosine_sim_scores.append((product,'No Product links Found Increase Number of Links or Change Search Source',None,None))
|
117 |
-
|
118 |
-
else:
|
119 |
-
for link in data[product][:link_count]:
|
120 |
-
|
121 |
-
similar_result, similar_embedding = get_embeddings(link,tag_option)
|
122 |
-
log_area.text(logger.getvalue())
|
123 |
-
|
124 |
-
print(similar_embedding)
|
125 |
-
for i in range(len(main_embedding)):
|
126 |
-
score = cosine_similarity(main_embedding[i], similar_embedding[i])
|
127 |
-
cosine_sim_scores.append((product, link, i, score))
|
128 |
-
log_area.text(logger.getvalue())
|
129 |
-
|
130 |
-
logger.write("--------------- DONE -----------------\n")
|
131 |
-
log_area.text(logger.getvalue())
|
132 |
-
return cosine_sim_scores, main_result
|
133 |
-
|
134 |
-
# Streamlit Interface
|
135 |
-
st.title("Check Infringement")
|
136 |
-
|
137 |
-
|
138 |
-
# Inputs
|
139 |
-
main_product = st.text_input('Enter Main Product Name', 'Philips led 7w bulb')
|
140 |
-
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')
|
141 |
-
search_method = st.selectbox('Choose Search Engine', ['All','duckduckgo', 'google', 'archive', 'github', 'wikipedia'])
|
142 |
-
|
143 |
-
col1, col2 = st.columns(2)
|
144 |
-
with col1:
|
145 |
-
product_count = st.number_input("Number of Simliar Products",min_value=1, step=1, format="%i")
|
146 |
-
with col2:
|
147 |
-
link_count = st.number_input("Number of Links per product",min_value=1, step=1, format="%i")
|
148 |
-
|
149 |
-
|
150 |
-
tag_option = st.selectbox('Choose Similarity Method', ["Complete Document Similarity","Feild Wise Document Similarity"])
|
151 |
-
|
152 |
-
|
153 |
-
if st.button('Check for Infringement'):
|
154 |
-
log_output = st.empty() # Placeholder for log output
|
155 |
-
|
156 |
-
with st.spinner('Processing...'):
|
157 |
-
with StreamCapture() as logger:
|
158 |
-
cosine_sim_scores, main_result = score(main_product, main_url,product_count, link_count, search_method, logger, log_output)
|
159 |
-
|
160 |
-
st.success('Processing complete!')
|
161 |
-
|
162 |
-
st.subheader("Cosine Similarity Scores")
|
163 |
-
|
164 |
-
# = score(main_product, main_url, search, logger, log_output)
|
165 |
-
if tag_option == 'Complete Document Similarity':
|
166 |
-
tags = ['Details']
|
167 |
-
else:
|
168 |
-
tags = ['Introduction', 'Specifications', 'Product Overview', 'Safety Information', 'Installation Instructions', 'Setup and Configuration', 'Operation Instructions', 'Maintenance and Care', 'Troubleshooting', 'Warranty Information', 'Legal Information']
|
169 |
-
|
170 |
-
for product, link, index, value in cosine_sim_scores:
|
171 |
-
if not index:
|
172 |
-
st.write(f"Product: {product}, Link: {link}")
|
173 |
-
if value!=None:
|
174 |
-
st.write(f"{tags[index]:<20} - Similarity: {value:.2f}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|