Spaces:
Configuration error
Configuration error
File size: 2,064 Bytes
f57a103 | 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | # -*- coding: utf-8 -*-
"""
Created on Sun Dec 4 18:15:42 2022
@author: amand
"""
import streamlit as st
import pandas as pd
import pickle as pkl
import spacy
from spacy.lang.en.stop_words import STOP_WORDS
from string import punctuation
from collections import Counter
from heapq import nlargest
import re
import os
from spacy import displacy
import nltk
nltk.download('punkt')
from nltk.tokenize import word_tokenize,sent_tokenize
nltk.download('stopwords')
from nltk.corpus import stopwords
nltk.download('wordnet')
nltk.download('omw-1.4')
from nltk.stem import WordNetLemmatizer
from tqdm import tqdm
from sentence_transformers import SentenceTransformer, util
import scipy.spatial
import torch
with open("santorini_df.pkl", "rb") as file1:
df = pkl.load(file1)
with open("santorini_corpus.pkl", "rb") as file2:
corpus = pkl.load(file2)
with open("santorini_embeddings.pkl", "rb") as file3:
corpus_embeddings = pkl.load(file3)
embedder = SentenceTransformer('all-MiniLM-L6-v2')
queries = st.text_input('What are you looking for in a hotel?', value = "",
type=str)
st.write('You are looking for a hotel that is', queries)
top_k = min(5, len(corpus))
for query in queries:
query_embedding = embedder.encode(query, convert_to_tensor=True)
# We use cosine-similarity and torch.topk to find the highest 5 scores
cos_scores = util.pytorch_cos_sim(query_embedding, corpus_embeddings)[0]
top_results = torch.topk(cos_scores, k=top_k)
print("\n\n======================\n\n")
print("\nTop 5 Hotel reviews matching your description:")
for score, idx in zip(top_results[0], top_results[1]):
hotel = df.Hotel[df['all_reviews']== corpus[idx]].to_string(index=False)
summary = df.key_words[df['all_reviews']== corpus[idx]].to_string(index=False).replace("[", '').replace("]", '')
print("Hotel: " , hotel)
print("(Score: {:.4f})".format(score))
print("Highlights from reviews:")
print(summary)
print("\n")
st.title("Enjoy your vacation!")
|