File size: 1,190 Bytes
d7bb6d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a29c1f
d7bb6d4
 
 
 
 
 
 
 
d138e01
6ed3096
c2b16d2
d7bb6d4
9f02c99
0a7c391
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import numpy as np # linear algebra
import os
from sklearn.metrics.pairwise import cosine_similarity
from sentence_transformers import SentenceTransformer
import pickle
import gradio as gr

with open('embeddings.pkl', "rb") as fIn:
    stored_data = pickle.load(fIn)
    embeddings = stored_data['embeddings']

with open('answers.pkl', "rb") as fIn:
    stored_data = pickle.load(fIn)
    answers = stored_data['answers']

model = SentenceTransformer('msmarco-MiniLM-L-6-v3')

def predict(ques):
    embeded_ques = model.encode(ques)
    em_vec = np.vstack([embeded_ques]*10)
    scores = cosine_similarity(embeddings, em_vec)
    idx = np.argmax([np.mean(arr) for arr in scores])
    return answers[idx]

examples = ['What is the date of his death?', 'Did Einstein have siblings?', 'Who was his wife?', 'What was Einstein\'s father\'s name?', 'At what institutions did he study?']
inputs = gr.Textbox(label='query')
outputs = gr.Textbox(label='Answers')
title = "Similar faq"
description = "Retreive answers of similar queries using sentence transformers"
gr.Interface(fn = predict, inputs = inputs, examples=examples, outputs = outputs, title = title, description = description).launch()