Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import pandas as pd | |
| import tiktoken | |
| import pandas as pd | |
| import time | |
| import spacy | |
| from spacy.lang.en.stop_words import STOP_WORDS | |
| from string import punctuation | |
| from collections import Counter | |
| from heapq import nlargest | |
| import nltk | |
| import numpy as np | |
| from tqdm import tqdm | |
| from sentence_transformers import SentenceTransformer, util | |
| from sentence_transformers import SentenceTransformer, CrossEncoder, util | |
| import gzip | |
| import os | |
| import torch | |
| from openai.embeddings_utils import get_embedding, cosine_similarity | |
| import os | |
| df = pd.read_pickle('miami.pkl') #to load 123.pkl back to the dataframe df | |
| embedder = SentenceTransformer('all-mpnet-base-v2') | |
| def search(query): | |
| n = 15 | |
| query_embedding = embedder.encode(query) | |
| df["similarity"] = df.embedding.apply(lambda x: cosine_similarity(x, query_embedding.reshape(768,-1))) | |
| results = ( | |
| df.sort_values("similarity", ascending=False) | |
| .head(n)) | |
| resultlist = [] | |
| hlist = [] | |
| for r in results.index: | |
| if results.name[r] not in hlist: | |
| smalldf = results.loc[results.name == results.name[r]] | |
| smallarr = smalldf.similarity[r].max() | |
| sm =smalldf.rating[r].mean() | |
| if smalldf.shape[1] > 3: | |
| smalldf = smalldf[:3] | |
| resultlist.append( | |
| { | |
| "name":results.name[r], | |
| "relevance score": smallarr.tolist(), | |
| "priceRange": smalldf.priceRange[r], | |
| "rating": sm.tolist(), | |
| "title": [ smalldf.title[s] for s in smalldf.index], | |
| "relevant_reviews": [ smalldf.review[s] for s in smalldf.index] | |
| }) | |
| hlist.append(results.name[r]) | |
| return resultlist | |
| def greet(query): | |
| bm25 = search(query) | |
| return bm25 | |
| examples = [ | |
| ["LGBTQ+ Friendly"], | |
| ["Best Nightlife "], | |
| ["Stunning Pools"], | |
| ["The Most Romantic Hotels in Miami"], | |
| ["Eco-Friendly Hotels"] | |
| ] | |
| demo = gr.Interface(fn=greet, outputs="json",title="miami-hotel-search", | |
| inputs=gr.inputs.Textbox(lines=5, label="Tell us what you like in a hotel?",default='hotels for LGBTQ+ community and nice rooftop pool'),examples=examples) | |
| demo.launch() |