Spaces:
Runtime error
Runtime error
algomuffin
commited on
Commit
•
493c668
1
Parent(s):
942d8ae
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sentence_transformers import SentenceTransformer, CrossEncoder, util
|
2 |
+
import torch
|
3 |
+
import pickle
|
4 |
+
import pandas as pd
|
5 |
+
import gradio as gr
|
6 |
+
bi_encoder = SentenceTransformer("multi-qa-MiniLM-L6-cos-v1")
|
7 |
+
cross_encoder = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2")
|
8 |
+
corpus_embeddings=pd.read_pickle("corpus_embeddings_cpu.pkl")
|
9 |
+
corpus=pd.read_pickle("corpus.pkl")
|
10 |
+
def search(query,top_k=100):
|
11 |
+
print("Top 5 Answer by the NSE:")
|
12 |
+
print()
|
13 |
+
ans=[]
|
14 |
+
##### Sematic Search #####
|
15 |
+
# Encode the query using the bi-encoder and find potentially relevant passages
|
16 |
+
question_embedding = bi_encoder.encode(query, convert_to_tensor=True)
|
17 |
+
hits = util.semantic_search(question_embedding, corpus_embeddings, top_k=top_k)
|
18 |
+
hits = hits[0] # Get the hits for the first query
|
19 |
+
##### Re-Ranking #####
|
20 |
+
# Now, score all retrieved passages with the cross_encoder
|
21 |
+
cross_inp = [[query, corpus[hit['corpus_id']]] for hit in hits]
|
22 |
+
cross_scores = cross_encoder.predict(cross_inp)
|
23 |
+
# Sort results by the cross-encoder scores
|
24 |
+
for idx in range(len(cross_scores)):
|
25 |
+
hits[idx]['cross-score'] = cross_scores[idx]
|
26 |
+
hits = sorted(hits, key=lambda x: x['cross-score'], reverse=True)
|
27 |
+
|
28 |
+
for idx, hit in enumerate(hits[0:5]):
|
29 |
+
ans.append(corpus[hit['corpus_id']])
|
30 |
+
return ans[0],ans[1],ans[2],ans[3],ans[4]
|
31 |
+
iface = gr.Interface(fn=search, inputs=["text"], outputs=["textbox","textbox","textbox","textbox","textbox"]).launch(share=True)
|