Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np # linear algebra
|
2 |
+
import os
|
3 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
4 |
+
from sentence_transformers import SentenceTransformer
|
5 |
+
import pickle
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
with open('embeddings.pkl', "rb") as fIn:
|
9 |
+
stored_data = pickle.load(fIn)
|
10 |
+
embeddings = stored_data['embeddings']
|
11 |
+
|
12 |
+
with open('answers.pkl', "rb") as fIn:
|
13 |
+
stored_data = pickle.load(fIn)
|
14 |
+
answers = stored_data['answers']
|
15 |
+
|
16 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
17 |
+
|
18 |
+
def predict(ques):
|
19 |
+
embeded_ques = model.encode(ques)
|
20 |
+
em_vec = np.vstack([embeded_ques]*10)
|
21 |
+
scores = cosine_similarity(embeddings, em_vec)
|
22 |
+
idx = np.argmax([np.mean(arr) for arr in scores])
|
23 |
+
return answers[idx]
|
24 |
+
|
25 |
+
|
26 |
+
inputs = gr.Textbox(label='query')
|
27 |
+
outputs = gr.Textbox(label='query')
|
28 |
+
title = "Similar faq"
|
29 |
+
description = "Retrive answers of similar queries sing sentence transformers"
|
30 |
+
gr.Interface(fn = predict, inputs = inputs, outputs = outputs, title = title, description = description).launch()
|