Spaces:
Build error
Build error
| import json | |
| 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 sentence_transformers import SentenceTransformer, util | |
| from openai.embeddings_utils import get_embedding, cosine_similarity | |
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| import nltk | |
| from spacy.cli import download | |
| nltk.download('punkt') | |
| nltk.download('punkt_tab') | |
| df_with_embedding2 = pd.read_pickle('df_2.pkl') | |
| from sentence_transformers import SentenceTransformer #import model | |
| embedder = SentenceTransformer("nomic-ai/nomic-embed-text-v1.5",trust_remote_code=True) | |
| def search(query): | |
| # return the first 15 results ranked by similarity. | |
| n = 15 | |
| # Embedding the query. | |
| query_embedding = embedder.encode(query) | |
| # Generate the similarity column. | |
| df_with_embedding2["similarity"] = (df_with_embedding2.embedding_summary.apply(lambda x: cosine_similarity(x, query_embedding.reshape(768,-1)))+df_with_embedding2.embedding_reviews.apply(lambda x: cosine_similarity(x, query_embedding.reshape(768,-1))))/2 | |
| results = ( | |
| df_with_embedding2.sort_values("similarity", ascending=False) | |
| .head(n)) | |
| resultlist = [] | |
| hlist = [] | |
| for r in results.index: | |
| if results.hotel_name[r] not in hlist: | |
| smalldf = results.loc[results.hotel_name == results.hotel_name[r]] | |
| if smalldf.shape[1] > 3: | |
| smalldf = smalldf[:3] | |
| resultlist.append( | |
| { | |
| "name":results.hotel_name[r], | |
| "score": smalldf.similarity[r][0], | |
| "rating": smalldf.rating_value[r], | |
| "review_count": smalldf.review_count[r], | |
| "street_address": smalldf.street_address[r], | |
| "city": smalldf.locality[r], | |
| "country": smalldf.country[r], | |
| "hotel_description":smalldf.hotel_description[r], | |
| "hotel_image":smalldf.hotel_image[r] | |
| }) | |
| hlist.append(results.hotel_name[r]) | |
| return resultlist | |
| import gradio as gr | |
| import json | |
| def display_hotel_info(query_json_str): | |
| """This app helps you find hotels based on your search query. Enter a city, location, hotel name or just type what you looking for .""" | |
| try: | |
| query_json = search(query_json_str) # Assume this function returns a list of hotel data dictionaries | |
| hotel_infos = [] | |
| image_outputs = [] | |
| for hotel in query_json: | |
| if not isinstance(hotel, dict): | |
| raise ValueError("Expected hotel data to be a dictionary.") | |
| name = hotel.get("name", "N/A") | |
| score = hotel.get("score", 0.0) | |
| rating = hotel.get("rating", "N/A") | |
| review_count = hotel.get("review_count", 0) | |
| street_address = hotel.get("street_address", "N/A") | |
| city = hotel.get("city", "N/A") | |
| country = hotel.get("country", "N/A") | |
| hotel_description=hotel.get("hotel_description","N/A") | |
| hotel_image = hotel.get("hotel_image", None) | |
| hotel_info = f""" | |
| <div style="display: flex; align-items: center; margin-bottom: 20px;"> | |
| <div style="flex: 1;"> | |
| <h3>{name}</h3> | |
| <p><strong>Rating:</strong> {rating}</p> | |
| <p><strong>Review Count:</strong> {review_count}</p> | |
| <p><strong>Address:</strong> {street_address}, {city}, {country}</p> | |
| <p><strong>hotel_description:</strong> {hotel_description}</p> | |
| </div> | |
| <div style="flex: 0 0 150px;"> | |
| <img src="{hotel_image}" alt="{name}" style="max-width: 150px; max-height: 150px; object-fit: cover;"> | |
| </div> | |
| </div> | |
| """ | |
| hotel_infos.append(hotel_info) | |
| return "<br><br>".join(hotel_infos) | |
| except (json.JSONDecodeError, ValueError) as e: | |
| return f"Error: {str(e)}" | |
| interface = gr.Interface( | |
| fn=display_hotel_info, | |
| inputs="text", | |
| outputs=gr.HTML(label="Hotel Information"), | |
| title="Hotel Recommendation Display", | |
| description="This app helps you find hotels based on your search query. Enter a city, location, hotel name or just type what you looking for.", | |
| ) | |
| interface.launch() | |