aibmedia commited on
Commit
dec3f0d
·
verified ·
1 Parent(s): 5c1eb47

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +53 -137
main.py CHANGED
@@ -22,7 +22,7 @@ API_URL3 = "https://api-inference.huggingface.co/models/Snowflake/snowflake-arct
22
 
23
 
24
 
25
- search = GoogleSearchAPIWrapper(k=20)
26
 
27
  bearer = "Bearer " + os.getenv('TOKEN')
28
  headers = {"Authorization": bearer }
@@ -47,25 +47,24 @@ def server_one():
47
 
48
  @app.route('/')
49
  async def server_1():
50
-
51
  query_sentence = "Obama's first name"
52
  duck_results = []
53
  try:
54
- searchduck = DuckDuckGoSearchResults(output_format="list" ,max_results=5, num_results=5)
55
  duck_results = searchduck.invoke(query_sentence)
56
  except:
57
  print("An exception occurred")
58
-
59
 
60
  tool = Tool(
61
  name="google_search",
62
  description="Search Google for recent results.",
63
  func=search.run,
64
  )
65
-
66
  google_results = search.results( query_sentence , 10 )
67
- print("type(duck_results)") ; print(type(duck_results)) ; print(type(google_results))
68
-
69
  all_results = duck_results + google_results
70
 
71
  # get the snippet put into list
@@ -73,12 +72,12 @@ async def server_1():
73
  for x in split_query_words:
74
  if x.isupper():
75
  important_keywords.append(x)
76
-
77
- ## get the longest word in sentence
78
- # res = "" ; iteratorx = 0
79
- # for word in split_query_words:
80
- # if len(word) > len(res):
81
- # res = word
82
 
83
  # get google 20 items
84
  # get user query in the url param
@@ -89,19 +88,25 @@ async def server_1():
89
  # payload = { "inputs": { "source_sentence": "That is a green painted house", "sentences": ["The house paint is green", "That house is green","That house was painted","The house is green", "The house was bought yesterday", "This house was painted green", "That house looks green", "Today the house is clean " ] } , }
90
  # payload = { "inputs": { "source_sentence": "Manila is the capital city of the Philippines", "sentences": ["The current capital city, Manila, has been the countrys capital throughout most of its history and regained the title through a presidential order in 1976", "Manila officially the City of Manila (Filipino: Lungsod ng Maynila), is the capital and second-most populous city of the Philippines, after Quezon City.", "Dis 4, 2024 — Manila, capital and chief city of the Philippines. The city is the centre of the countrys economic, political, social, and cultural activity.", "Quezon City is the capital of the Philippines", "Manila is the capital of the philippines", "For sometime Manila has been the capital of of the Philippines" , "What is the capital of Philippines", "Manila is not the capital of the Phillipines", "Quezon city was the capital of the Philippines, until President Ferdinand Marcos Sr. moved the capital to back to Manila. " ] } , }
91
  # payload = { "inputs": { "source_sentence": "Manila is the capital city of the Philippines", "sentences": ["The current capital city, Manila, has been the country's capital throughout most of its history and regained the title through a presidential order in 1976"] } , }
 
92
  payload = { "inputs": { "source_sentence": "Manila is the capital city of the Philippines", "sentences": ["The current capital city, Manila, has been the countrys capital throughout most","Manila officially the City of Manila (Filipino: Lungsod ng Maynila),","Dis 4, 2024 — Manila, capital and chief city of the Philippines. The city is the centre ","Quezon City is the capital of the Philippines","Manila is the capital of the philippines","For sometime Manila has been the capital of of the Philippines" ,"What is the capital of Philippines","Manila is not the capital of the Phillipines","Quezon city was the capital of the Philippines, until President Ferdinand "] } , }
93
  response0 = requests.post(API_URL0, headers=headers, json=payload)
94
  response1 = requests.post(API_URL1, headers=headers, json=payload)
95
  response2 = requests.post(API_URL2, headers=headers, json=payload)
96
  response3 = requests.post(API_URL3, headers=headers, json=payload)
 
97
  varcontinue_similarity = 0
98
  print("type( response0.json() )")
99
  print(type( response0.json() ))
100
  print(type( response1.json() ))
101
  print(type( response2.json() ))
102
  print(type( response3.json() ))
 
103
  if type(response0.json()) == list and type(response1.json()) == list and type(response2.json()) == list and type(response3.json()) == list :
104
  similarity_scores = response0.json() + response1.json() + response2.json() + response3.json()
 
 
 
105
  varcontinue_similarity = 1
106
  else:
107
  similarity_scores = "There's an error in llm similarity search retrieval"
@@ -113,9 +118,9 @@ async def server_1():
113
  if varcontinue_similarity == 1 :
114
  # call processing with 10 google search result or 15 search results
115
  if len(all_results) == 10 :
116
- result_processed = process_similarity_10(all_results , similarity_scores )
117
  if len(all_results) > 10 :
118
- result_processed = process_similarity_15(all_results , similarity_scores )
119
  # return all_results
120
  return result_processed
121
 
@@ -127,49 +132,15 @@ def threadserver():
127
 
128
 
129
 
130
- def process_similarity_15(web_results , similarity_scores):
131
 
132
  # print(similarity_scores)
133
  # print(type(similarity_scores))
134
  print("length")
135
- print(len(similarity_scores))
136
  key_index = 0
137
  #copy + loop to get index
138
 
139
- r_iterator = 0
140
- resp_list0 = []
141
- resp_list1 = []
142
- resp_list2 = []
143
- resp_list3 = []
144
-
145
- for value_inlist in similarity_scores:
146
- print(value_inlist)
147
- print("index ")
148
- print(key_index)
149
- if key_index <= 14 :
150
- resp_list0.append(value_inlist)
151
- if key_index <= 29 and key_index > 14 :
152
- resp_list1.append(value_inlist)
153
- if key_index <= 44 and key_index > 29 :
154
- resp_list2.append(value_inlist)
155
- if key_index <= 59 and key_index > 44 :
156
- resp_list3.append(value_inlist)
157
- key_index = key_index + 1
158
-
159
- print("The Response list 0 ")
160
- print(resp_list0)
161
- print("The Response list 1 ")
162
- print(resp_list1)
163
- print("The Response list 2 ")
164
- print(resp_list2)
165
- print("The Response list 3 ")
166
- print(resp_list3)
167
- # sorted 0 - 3 are sorted lists of score ; we must get their indices which is 0-8 that will be mapped to sentence index
168
- sorted0 = sorted(resp_list0 , reverse=True)
169
- sorted1 = sorted(resp_list1 , reverse=True)
170
- sorted2 = sorted(resp_list2 , reverse=True)
171
- sorted3 = sorted(resp_list3 , reverse=True)
172
-
173
  print("the sorted0-3")
174
  print(sorted0)
175
  print(sorted1)
@@ -180,24 +151,24 @@ def process_similarity_15(web_results , similarity_scores):
180
 
181
  sorted0_with_index = []
182
  for x in sorted0:
183
- for y in resp_list0:
184
  if x == y:
185
  print("index of sorted0")
186
- print(resp_list0.index(y))
187
  if x > 0.90:
188
- sorted0_with_index.append(resp_list0.index(y))
189
  print("sorted_with_index")
190
  print(sorted0_with_index)
191
  print("sorted0_with_index")
192
  print(sorted0_with_index)
193
  sorted1_with_index = []
194
  for x in sorted1:
195
- for y in resp_list1:
196
  if x == y:
197
  print("index of sorted1")
198
- print(resp_list1.index(y))
199
  if y > 0.90:
200
- sorted1_with_index.append(resp_list1.index(y))
201
  print("sorted_with_index")
202
  print(sorted1_with_index)
203
 
@@ -206,15 +177,15 @@ def process_similarity_15(web_results , similarity_scores):
206
 
207
  sorted2_with_index = []
208
  print("b4 for x in sorted2:")
209
- print("resp_list2:" + str(resp_list2))
210
  print("sorted:" + str(sorted2))
211
  for x in sorted2:
212
- for y in resp_list2:
213
  if x == y:
214
  print("index of sorted2")
215
- print(resp_list2.index(y))
216
  if y > 0.90:
217
- sorted2_with_index.append(resp_list2.index(y))
218
  print("sorted_with_index")
219
  print(sorted2_with_index)
220
 
@@ -222,14 +193,14 @@ def process_similarity_15(web_results , similarity_scores):
222
  print(sorted2_with_index)
223
  sorted3_with_index = []
224
  print("b4 for x in sorted3:")
225
- print("resp_list3:" + str(resp_list3))
226
  for x in sorted3:
227
- for y in resp_list3:
228
  if x == y:
229
  print("index of sorted3")
230
- print(resp_list3.index(y))
231
  if y > 0.90:
232
- sorted3_with_index.append(resp_list3.index(y))
233
  print("sorted_with_index")
234
  print(sorted3_with_index)
235
 
@@ -318,52 +289,16 @@ def process_similarity_15(web_results , similarity_scores):
318
  print("No reliable similarity found by 4 llms")
319
 
320
  return str( index_sorted0 ) + "," + str( index_sorted1 ) + "," + str( index_sorted2 ) + "," + str( index_sorted3 )
321
-
322
-
323
-
324
- def process_similarity_10(web_results , similarity_scores):
325
 
326
  # print(similarity_scores)
327
  # print(type(similarity_scores))
328
  print("length")
329
- print(len(similarity_scores))
330
  key_index = 0
331
  #copy + loop to get index
332
 
333
- r_iterator = 0
334
- resp_list0 = []
335
- resp_list1 = []
336
- resp_list2 = []
337
- resp_list3 = []
338
-
339
- for value_inlist in similarity_scores:
340
- print(value_inlist)
341
- print("index ")
342
- print(key_index)
343
- if key_index <= 9 :
344
- resp_list0.append(value_inlist)
345
- if key_index <= 19 and key_index > 9 :
346
- resp_list1.append(value_inlist)
347
- if key_index <= 29 and key_index > 19 :
348
- resp_list2.append(value_inlist)
349
- if key_index <= 39 and key_index > 29 :
350
- resp_list3.append(value_inlist)
351
- key_index = key_index + 1
352
-
353
- print("The Response list 0 ")
354
- print(resp_list0)
355
- print("The Response list 1 ")
356
- print(resp_list1)
357
- print("The Response list 2 ")
358
- print(resp_list2)
359
- print("The Response list 3 ")
360
- print(resp_list3)
361
- # sorted 0 - 3 are sorted lists of score ; we must get their indices which is 0-8 that will be mapped to sentence index
362
- sorted0 = sorted(resp_list0 , reverse=True)
363
- sorted1 = sorted(resp_list1 , reverse=True)
364
- sorted2 = sorted(resp_list2 , reverse=True)
365
- sorted3 = sorted(resp_list3 , reverse=True)
366
-
367
  print("the sorted0-3")
368
  print(sorted0)
369
  print(sorted1)
@@ -374,24 +309,24 @@ def process_similarity_10(web_results , similarity_scores):
374
 
375
  sorted0_with_index = []
376
  for x in sorted0:
377
- for y in resp_list0:
378
  if x == y:
379
  print("index of sorted0")
380
- print(resp_list0.index(y))
381
  if x > 0.90:
382
- sorted0_with_index.append(resp_list0.index(y))
383
  print("sorted_with_index")
384
  print(sorted0_with_index)
385
  print("sorted0_with_index")
386
  print(sorted0_with_index)
387
  sorted1_with_index = []
388
  for x in sorted1:
389
- for y in resp_list1:
390
  if x == y:
391
  print("index of sorted1")
392
- print(resp_list1.index(y))
393
  if y > 0.90:
394
- sorted1_with_index.append(resp_list1.index(y))
395
  print("sorted_with_index")
396
  print(sorted1_with_index)
397
 
@@ -400,15 +335,15 @@ def process_similarity_10(web_results , similarity_scores):
400
 
401
  sorted2_with_index = []
402
  print("b4 for x in sorted2:")
403
- print("resp_list2:" + str(resp_list2))
404
  print("sorted:" + str(sorted2))
405
  for x in sorted2:
406
- for y in resp_list2:
407
  if x == y:
408
  print("index of sorted2")
409
- print(resp_list2.index(y))
410
  if y > 0.90:
411
- sorted2_with_index.append(resp_list2.index(y))
412
  print("sorted_with_index")
413
  print(sorted2_with_index)
414
 
@@ -416,14 +351,14 @@ def process_similarity_10(web_results , similarity_scores):
416
  print(sorted2_with_index)
417
  sorted3_with_index = []
418
  print("b4 for x in sorted3:")
419
- print("resp_list3:" + str(resp_list3))
420
  for x in sorted3:
421
- for y in resp_list3:
422
  if x == y:
423
  print("index of sorted3")
424
- print(resp_list3.index(y))
425
  if y > 0.90:
426
- sorted3_with_index.append(resp_list3.index(y))
427
  print("sorted_with_index")
428
  print(sorted3_with_index)
429
 
@@ -511,27 +446,8 @@ def process_similarity_10(web_results , similarity_scores):
511
  else:
512
  print("No reliable similarity found by 4 llms")
513
 
514
-
515
  return str( index_sorted0 ) + "," + str( index_sorted1 ) + "," + str( index_sorted2 ) + "," + str( index_sorted3 )
516
 
517
- # index_sorted0 = sorted0_with_index[:4]
518
- # index_sorted1 = sorted1_with_index[:4]
519
- # index_sorted2 = sorted2_with_index[:4]
520
- # index_sorted3 = sorted3_with_index[:4]
521
-
522
- # combined_indexes = index_sorted0 +index_sorted1 +index_sorted2 +index_sorted3
523
- # uniq_list = []
524
- # print("combined_indexes")
525
- # print(combined_indexes)
526
- # for item in combined_indexes:
527
- # if item not in uniq_list:
528
- # uniq_list.append(item)
529
- # print("uniq_list")
530
- # print(uniq_list)
531
- # top_3_indexes = []
532
- # get the top 3 from the combined_indexes
533
- # the top 3 indexes must be above .78 similarity score
534
- # the top 3 must have occured 4 times or more in combined_indexes
535
 
536
  if __name__ == '__main__':
537
  app.run(host='0.0.0.0', port=8080)
 
22
 
23
 
24
 
25
+ search = GoogleSearchAPIWrapper()
26
 
27
  bearer = "Bearer " + os.getenv('TOKEN')
28
  headers = {"Authorization": bearer }
 
47
 
48
  @app.route('/')
49
  async def server_1():
50
+ # check docs first then check similarity
51
  query_sentence = "Obama's first name"
52
  duck_results = []
53
  try:
54
+ searchduck = DuckDuckGoSearchResults(output_format="list", max_results=5, num_results=5)
55
  duck_results = searchduck.invoke(query_sentence)
56
  except:
57
  print("An exception occurred")
58
+
59
 
60
  tool = Tool(
61
  name="google_search",
62
  description="Search Google for recent results.",
63
  func=search.run,
64
  )
65
+
66
  google_results = search.results( query_sentence , 10 )
67
+ print("type(duck_results)") ; print(type(duck_results)) ; print(type(google_results)
 
68
  all_results = duck_results + google_results
69
 
70
  # get the snippet put into list
 
72
  for x in split_query_words:
73
  if x.isupper():
74
  important_keywords.append(x)
75
+
76
+ ## get the longest word in sentence
77
+ # res = "" ; iteratorx = 0
78
+ # for word in split_query_words:
79
+ # if len(word) > len(res):
80
+ # res = word
81
 
82
  # get google 20 items
83
  # get user query in the url param
 
88
  # payload = { "inputs": { "source_sentence": "That is a green painted house", "sentences": ["The house paint is green", "That house is green","That house was painted","The house is green", "The house was bought yesterday", "This house was painted green", "That house looks green", "Today the house is clean " ] } , }
89
  # payload = { "inputs": { "source_sentence": "Manila is the capital city of the Philippines", "sentences": ["The current capital city, Manila, has been the countrys capital throughout most of its history and regained the title through a presidential order in 1976", "Manila officially the City of Manila (Filipino: Lungsod ng Maynila), is the capital and second-most populous city of the Philippines, after Quezon City.", "Dis 4, 2024 — Manila, capital and chief city of the Philippines. The city is the centre of the countrys economic, political, social, and cultural activity.", "Quezon City is the capital of the Philippines", "Manila is the capital of the philippines", "For sometime Manila has been the capital of of the Philippines" , "What is the capital of Philippines", "Manila is not the capital of the Phillipines", "Quezon city was the capital of the Philippines, until President Ferdinand Marcos Sr. moved the capital to back to Manila. " ] } , }
90
  # payload = { "inputs": { "source_sentence": "Manila is the capital city of the Philippines", "sentences": ["The current capital city, Manila, has been the country's capital throughout most of its history and regained the title through a presidential order in 1976"] } , }
91
+
92
  payload = { "inputs": { "source_sentence": "Manila is the capital city of the Philippines", "sentences": ["The current capital city, Manila, has been the countrys capital throughout most","Manila officially the City of Manila (Filipino: Lungsod ng Maynila),","Dis 4, 2024 — Manila, capital and chief city of the Philippines. The city is the centre ","Quezon City is the capital of the Philippines","Manila is the capital of the philippines","For sometime Manila has been the capital of of the Philippines" ,"What is the capital of Philippines","Manila is not the capital of the Phillipines","Quezon city was the capital of the Philippines, until President Ferdinand "] } , }
93
  response0 = requests.post(API_URL0, headers=headers, json=payload)
94
  response1 = requests.post(API_URL1, headers=headers, json=payload)
95
  response2 = requests.post(API_URL2, headers=headers, json=payload)
96
  response3 = requests.post(API_URL3, headers=headers, json=payload)
97
+
98
  varcontinue_similarity = 0
99
  print("type( response0.json() )")
100
  print(type( response0.json() ))
101
  print(type( response1.json() ))
102
  print(type( response2.json() ))
103
  print(type( response3.json() ))
104
+
105
  if type(response0.json()) == list and type(response1.json()) == list and type(response2.json()) == list and type(response3.json()) == list :
106
  similarity_scores = response0.json() + response1.json() + response2.json() + response3.json()
107
+ # If all list then pass to process func
108
+ sorted0 = sorted(response0.json() , reverse=True); sorted1 = sorted(response1.json() , reverse=True)
109
+ sorted2 = sorted(response1.json() , reverse=True); sorted3 = sorted(response3.json() , reverse=True)
110
  varcontinue_similarity = 1
111
  else:
112
  similarity_scores = "There's an error in llm similarity search retrieval"
 
118
  if varcontinue_similarity == 1 :
119
  # call processing with 10 google search result or 15 search results
120
  if len(all_results) == 10 :
121
+ result_processed = process_similarity_10(sorted0, sorted1, sorted2, sorted3,response0.json(), response1.json(), response2.json(), response3.json() )
122
  if len(all_results) > 10 :
123
+ result_processed = process_similarity_15(sorted0, sorted1, sorted2, sorted3,response0.json(), response1.json(), response2.json(), response3.json() )
124
  # return all_results
125
  return result_processed
126
 
 
132
 
133
 
134
 
135
+ def process_similarity_15(sorted0, sorted1, sorted2, sorted3, actualscore0, actualscore1, actualscore2, actualscore3):
136
 
137
  # print(similarity_scores)
138
  # print(type(similarity_scores))
139
  print("length")
140
+ # print(len(similarity_scores))
141
  key_index = 0
142
  #copy + loop to get index
143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  print("the sorted0-3")
145
  print(sorted0)
146
  print(sorted1)
 
151
 
152
  sorted0_with_index = []
153
  for x in sorted0:
154
+ for y in actualscore0:
155
  if x == y:
156
  print("index of sorted0")
157
+ print(actualscore0.index(y))
158
  if x > 0.90:
159
+ sorted0_with_index.append(actualscore0.index(y))
160
  print("sorted_with_index")
161
  print(sorted0_with_index)
162
  print("sorted0_with_index")
163
  print(sorted0_with_index)
164
  sorted1_with_index = []
165
  for x in sorted1:
166
+ for y in actualscore1:
167
  if x == y:
168
  print("index of sorted1")
169
+ print(actualscore1.index(y))
170
  if y > 0.90:
171
+ sorted1_with_index.append(actualscore1.index(y))
172
  print("sorted_with_index")
173
  print(sorted1_with_index)
174
 
 
177
 
178
  sorted2_with_index = []
179
  print("b4 for x in sorted2:")
180
+ print("resp_list2:" + str(actualscore2))
181
  print("sorted:" + str(sorted2))
182
  for x in sorted2:
183
+ for y in actualscore2:
184
  if x == y:
185
  print("index of sorted2")
186
+ print(actualscore2.index(y))
187
  if y > 0.90:
188
+ sorted2_with_index.append(actualscore2.index(y))
189
  print("sorted_with_index")
190
  print(sorted2_with_index)
191
 
 
193
  print(sorted2_with_index)
194
  sorted3_with_index = []
195
  print("b4 for x in sorted3:")
196
+ print("resp_list3:" + str(actualscore3))
197
  for x in sorted3:
198
+ for y in actualscore3:
199
  if x == y:
200
  print("index of sorted3")
201
+ print(actualscore3.index(y))
202
  if y > 0.90:
203
+ sorted3_with_index.append(actualscore3.index(y))
204
  print("sorted_with_index")
205
  print(sorted3_with_index)
206
 
 
289
  print("No reliable similarity found by 4 llms")
290
 
291
  return str( index_sorted0 ) + "," + str( index_sorted1 ) + "," + str( index_sorted2 ) + "," + str( index_sorted3 )
292
+
293
+ def process_similarity_10(sorted0, sorted1, sorted2, sorted3, actualscore0, actualscore1, actualscore2, actualscore3):
 
 
294
 
295
  # print(similarity_scores)
296
  # print(type(similarity_scores))
297
  print("length")
298
+ # print(len(similarity_scores))
299
  key_index = 0
300
  #copy + loop to get index
301
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
302
  print("the sorted0-3")
303
  print(sorted0)
304
  print(sorted1)
 
309
 
310
  sorted0_with_index = []
311
  for x in sorted0:
312
+ for y in actualscore0:
313
  if x == y:
314
  print("index of sorted0")
315
+ print(actualscore0.index(y))
316
  if x > 0.90:
317
+ sorted0_with_index.append(actualscore0.index(y))
318
  print("sorted_with_index")
319
  print(sorted0_with_index)
320
  print("sorted0_with_index")
321
  print(sorted0_with_index)
322
  sorted1_with_index = []
323
  for x in sorted1:
324
+ for y in actualscore1:
325
  if x == y:
326
  print("index of sorted1")
327
+ print(actualscore1.index(y))
328
  if y > 0.90:
329
+ sorted1_with_index.append(actualscore1.index(y))
330
  print("sorted_with_index")
331
  print(sorted1_with_index)
332
 
 
335
 
336
  sorted2_with_index = []
337
  print("b4 for x in sorted2:")
338
+ print("resp_list2:" + str(actualscore2))
339
  print("sorted:" + str(sorted2))
340
  for x in sorted2:
341
+ for y in actualscore2:
342
  if x == y:
343
  print("index of sorted2")
344
+ print(actualscore2.index(y))
345
  if y > 0.90:
346
+ sorted2_with_index.append(actualscore2.index(y))
347
  print("sorted_with_index")
348
  print(sorted2_with_index)
349
 
 
351
  print(sorted2_with_index)
352
  sorted3_with_index = []
353
  print("b4 for x in sorted3:")
354
+ print("resp_list3:" + str(actualscore3))
355
  for x in sorted3:
356
+ for y in actualscore3:
357
  if x == y:
358
  print("index of sorted3")
359
+ print(actualscore3.index(y))
360
  if y > 0.90:
361
+ sorted3_with_index.append(actualscore3.index(y))
362
  print("sorted_with_index")
363
  print(sorted3_with_index)
364
 
 
446
  else:
447
  print("No reliable similarity found by 4 llms")
448
 
 
449
  return str( index_sorted0 ) + "," + str( index_sorted1 ) + "," + str( index_sorted2 ) + "," + str( index_sorted3 )
450
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
451
 
452
  if __name__ == '__main__':
453
  app.run(host='0.0.0.0', port=8080)