Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import tiktoken
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import time
|
| 6 |
+
import spacy
|
| 7 |
+
from spacy.lang.en.stop_words import STOP_WORDS
|
| 8 |
+
from string import punctuation
|
| 9 |
+
from collections import Counter
|
| 10 |
+
from heapq import nlargest
|
| 11 |
+
import nltk
|
| 12 |
+
import numpy as np
|
| 13 |
+
from tqdm import tqdm
|
| 14 |
+
from sentence_transformers import SentenceTransformer, util
|
| 15 |
+
from sentence_transformers import SentenceTransformer, CrossEncoder, util
|
| 16 |
+
import gzip
|
| 17 |
+
import os
|
| 18 |
+
import torch
|
| 19 |
+
|
| 20 |
+
from openai.embeddings_utils import get_embedding, cosine_similarity
|
| 21 |
+
import os
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
df = pd.read_pickle('entire_data.pkl') #to load 123.pkl back to the dataframe df
|
| 26 |
+
embedder = SentenceTransformer('all-mpnet-base-v2')
|
| 27 |
+
|
| 28 |
+
def search(query):
|
| 29 |
+
n = 15
|
| 30 |
+
query_embedding = embedder.encode(query)
|
| 31 |
+
df["similarity"] = df.embedding.apply(lambda x: cosine_similarity(x, query_embedding.reshape(768,-1)))
|
| 32 |
+
|
| 33 |
+
results = (
|
| 34 |
+
df.sort_values("similarity", ascending=False)
|
| 35 |
+
.head(n))
|
| 36 |
+
|
| 37 |
+
resultlist = []
|
| 38 |
+
|
| 39 |
+
hlist = []
|
| 40 |
+
for r in results.index:
|
| 41 |
+
if results.name[r] not in hlist:
|
| 42 |
+
smalldf = results.loc[results.name == results.name[r]]
|
| 43 |
+
smallarr = smalldf.similarity[r].max()
|
| 44 |
+
sm =smalldf.rating[r].mean()
|
| 45 |
+
|
| 46 |
+
if smalldf.shape[1] > 3:
|
| 47 |
+
smalldf = smalldf[:3]
|
| 48 |
+
|
| 49 |
+
resultlist.append(
|
| 50 |
+
{
|
| 51 |
+
"name":results.name[r],
|
| 52 |
+
"relevance score": smallarr.tolist(),
|
| 53 |
+
"priceRange": smalldf.priceRange[r],
|
| 54 |
+
"rating": sm.tolist(),
|
| 55 |
+
"title": [ smalldf.title[s] for s in smalldf.index],
|
| 56 |
+
"relevant_reviews": [ smalldf.review[s] for s in smalldf.index]
|
| 57 |
+
})
|
| 58 |
+
hlist.append(results.name[r])
|
| 59 |
+
return resultlist
|
| 60 |
+
|
| 61 |
+
def greet(query):
|
| 62 |
+
bm25 = search(query)
|
| 63 |
+
return bm25
|
| 64 |
+
examples = [
|
| 65 |
+
["The Best LGBTQ+ Friendly Hotels in Miami"],
|
| 66 |
+
["The Best Nightlife Hotels in Miami"],
|
| 67 |
+
["The Best Miami Hotels with Stunning Pools"],
|
| 68 |
+
["The Most Romantic Hotels in Miami"],
|
| 69 |
+
["Eco-Friendly Hotels"]
|
| 70 |
+
]
|
| 71 |
+
|
| 72 |
+
demo = gr.Interface(fn=greet, outputs="json",title="miami-hotel-search",
|
| 73 |
+
inputs=gr.inputs.Textbox(lines=5, label="Tell us what you like in a hotel?"),examples=examples)
|
| 74 |
+
|
| 75 |
+
demo.launch(share=True)
|