Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,61 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import tiktoken
|
| 4 |
|
| 5 |
+
import time
|
| 6 |
+
from sentence_transformers import SentenceTransformer
|
| 7 |
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
import os
|
| 13 |
+
import torch
|
| 14 |
+
|
| 15 |
+
from openai.embeddings_utils import get_embedding, cosine_similarity
|
| 16 |
+
import os
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
df = pd.read_pickle('entire_data.pkl') #to load 123.pkl back to the dataframe df
|
| 21 |
+
embedder = SentenceTransformer('all-mpnet-base-v2')
|
| 22 |
+
|
| 23 |
+
def search(query):
|
| 24 |
+
n = 15
|
| 25 |
+
query_embedding = embedder.encode(query)
|
| 26 |
+
df["similarity"] = df.embedding.apply(lambda x: cosine_similarity(x, query_embedding.reshape(768,-1)))
|
| 27 |
+
|
| 28 |
+
results = (
|
| 29 |
+
df.sort_values("similarity", ascending=False)
|
| 30 |
+
.head(n))
|
| 31 |
+
|
| 32 |
+
resultlist = []
|
| 33 |
+
|
| 34 |
+
hlist = []
|
| 35 |
+
for r in results.index:
|
| 36 |
+
if results.name[r] not in hlist:
|
| 37 |
+
smalldf = results.loc[results.name == results.name[r]]
|
| 38 |
+
smallarr = smalldf.similarity[r].max()
|
| 39 |
+
sm =smalldf.rating[r].mean()
|
| 40 |
+
|
| 41 |
+
if smalldf.shape[1] > 3:
|
| 42 |
+
smalldf = smalldf[:3]
|
| 43 |
+
|
| 44 |
+
resultlist.append(
|
| 45 |
+
{
|
| 46 |
+
"name":results.name[r],
|
| 47 |
+
"description":results.description[r],
|
| 48 |
+
"relevance score": smallarr.tolist(),
|
| 49 |
+
"rating": sm.tolist(),
|
| 50 |
+
"relevant_reviews": [ smalldf.text[s] for s in smalldf.index]
|
| 51 |
+
})
|
| 52 |
+
hlist.append(results.name[r])
|
| 53 |
+
return resultlist
|
| 54 |
+
|
| 55 |
+
def greet(query):
|
| 56 |
+
bm25 = search(query)
|
| 57 |
+
return bm25
|
| 58 |
+
|
| 59 |
+
demo = gr.Interface(fn=greet, inputs="text", outputs="json")
|
| 60 |
+
|
| 61 |
+
demo.launch()
|