Spaces:
Runtime error
Runtime error
michal
commited on
Commit
•
5f4adea
1
Parent(s):
4c15272
greg funcs
Browse files- app.py +4 -1
- audios/tempfile.mp3 +0 -0
- greg_funcs.py +47 -0
app.py
CHANGED
@@ -41,6 +41,9 @@ from sentence_transformers import SentenceTransformer, CrossEncoder, util
|
|
41 |
from torch import tensor as torch_tensor
|
42 |
from datasets import load_dataset
|
43 |
|
|
|
|
|
|
|
44 |
"""# import models"""
|
45 |
|
46 |
bi_encoder = SentenceTransformer(
|
@@ -63,7 +66,6 @@ mycorpus_embeddings = torch_tensor(dataset_embed_pd.values)
|
|
63 |
|
64 |
def search(query, top_k=20, top_n=1):
|
65 |
question_embedding = bi_encoder.encode(query, convert_to_tensor=True)
|
66 |
-
question_embedding = question_embedding # .cuda()
|
67 |
hits = util.semantic_search(
|
68 |
question_embedding, mycorpus_embeddings, top_k=top_k)
|
69 |
hits = hits[0] # Get the hits for the first query
|
@@ -84,6 +86,7 @@ def search(query, top_k=20, top_n=1):
|
|
84 |
|
85 |
|
86 |
def get_text(qry):
|
|
|
87 |
predictions = search(qry)
|
88 |
prediction_text = []
|
89 |
for hit in predictions:
|
|
|
41 |
from torch import tensor as torch_tensor
|
42 |
from datasets import load_dataset
|
43 |
|
44 |
+
|
45 |
+
from greg_funcs import greg_search
|
46 |
+
|
47 |
"""# import models"""
|
48 |
|
49 |
bi_encoder = SentenceTransformer(
|
|
|
66 |
|
67 |
def search(query, top_k=20, top_n=1):
|
68 |
question_embedding = bi_encoder.encode(query, convert_to_tensor=True)
|
|
|
69 |
hits = util.semantic_search(
|
70 |
question_embedding, mycorpus_embeddings, top_k=top_k)
|
71 |
hits = hits[0] # Get the hits for the first query
|
|
|
86 |
|
87 |
|
88 |
def get_text(qry):
|
89 |
+
# predictions = greg_search(qry)
|
90 |
predictions = search(qry)
|
91 |
prediction_text = []
|
92 |
for hit in predictions:
|
audios/tempfile.mp3
CHANGED
Binary files a/audios/tempfile.mp3 and b/audios/tempfile.mp3 differ
|
|
greg_funcs.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
from sentence_transformers import SentenceTransformer, CrossEncoder, util
|
4 |
+
from torch import tensor as torch_tensor
|
5 |
+
from datasets import load_dataset
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
"""# import models"""
|
10 |
+
|
11 |
+
bi_encoder = SentenceTransformer('multi-qa-MiniLM-L6-cos-v1')
|
12 |
+
bi_encoder.max_seq_length = 256 #Truncate long passages to 256 tokens
|
13 |
+
|
14 |
+
#The bi-encoder will retrieve top_k documents. We use a cross-encoder, to re-rank the results list to improve the quality
|
15 |
+
cross_encoder = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
|
16 |
+
|
17 |
+
|
18 |
+
|
19 |
+
"""# import datasets"""
|
20 |
+
|
21 |
+
dataset = load_dataset("gfhayworth/hack_policy", split='train')
|
22 |
+
mypassages = list(dataset.to_pandas()['psg'])
|
23 |
+
|
24 |
+
dataset_embed = load_dataset("gfhayworth/hack_policy_embed", split='train')
|
25 |
+
dataset_embed_pd = dataset_embed.to_pandas()
|
26 |
+
mycorpus_embeddings = torch_tensor(dataset_embed_pd.values)
|
27 |
+
|
28 |
+
def greg_search(query, passages = mypassages, doc_embedding = mycorpus_embeddings, top_k=20, top_n = 1):
|
29 |
+
question_embedding = bi_encoder.encode(query, convert_to_tensor=True)
|
30 |
+
question_embedding = question_embedding #.cuda()
|
31 |
+
hits = util.semantic_search(question_embedding, doc_embedding, top_k=top_k)
|
32 |
+
hits = hits[0] # Get the hits for the first query
|
33 |
+
|
34 |
+
##### Re-Ranking #####
|
35 |
+
cross_inp = [[query, passages[hit['corpus_id']]] for hit in hits]
|
36 |
+
cross_scores = cross_encoder.predict(cross_inp)
|
37 |
+
|
38 |
+
# Sort results by the cross-encoder scores
|
39 |
+
for idx in range(len(cross_scores)):
|
40 |
+
hits[idx]['cross-score'] = cross_scores[idx]
|
41 |
+
|
42 |
+
hits = sorted(hits, key=lambda x: x['cross-score'], reverse=True)
|
43 |
+
predictions = hits[:top_n]
|
44 |
+
return predictions
|
45 |
+
# for hit in hits[0:3]:
|
46 |
+
# print("\t{:.3f}\t{}".format(hit['cross-score'], mypassages[hit['corpus_id']].replace("\n", " ")))
|
47 |
+
|