|
import gradio as gr |
|
import pandas as pd |
|
import numpy as np |
|
import faiss |
|
from sentence_transformers import SentenceTransformer |
|
|
|
model = SentenceTransformer("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2") |
|
|
|
embeddings_hotels = np.load("normalized_embeddings_hotels_multilingual.npy", allow_pickle=True) |
|
embeddings_ar = np.load("normalized_embeddings_ar_multilingual.npy", allow_pickle=True) |
|
df_hotels = pd.read_csv("hotel_dataset_processed.csv") |
|
df_ar = pd.read_csv("df_ar_1.csv") |
|
|
|
embeddings_combined = np.vstack((embeddings_hotels, embeddings_ar)) |
|
df_combined = pd.concat([df_hotels, df_ar], ignore_index=True) |
|
|
|
dimension = embeddings_combined.shape[1] |
|
index = faiss.IndexFlatL2(dimension) |
|
index.add(embeddings_combined) |
|
|
|
def search_in_faiss(query_text, model, k=5): |
|
query_embedding = model.encode(query_text).reshape(1, -1).astype("float32") |
|
_, indices = index.search(query_embedding, k) |
|
top_results = df_combined.iloc[indices[0]] |
|
return top_results |
|
|
|
def format_results(results): |
|
formatted_results = [] |
|
for _, row in results.iterrows(): |
|
if not pd.isna(row.get("hotel_name", "")): |
|
google_maps_url = f"https://www.google.com/maps/search/?api=1&query={row.get('hotel_name', 'N/A').replace(' ', '+')}" |
|
result = ( |
|
f"<b>Hotel Name</b>: {row.get('hotel_name', 'N/A')}<br>" |
|
f"<b>Description</b>: {row.get('hotel_description', 'N/A')}<br>" |
|
f"<b>Review Title</b>: {row.get('review_title', 'N/A')}<br>" |
|
f"<b>Review Text</b>: {row.get('review_text', 'N/A')}<br>" |
|
f"<b>Rating</b>: {row.get('rate', 'N/A')}<br>" |
|
f"<b>Trip Date</b>: {row.get('tripdate', 'N/A')}<br>" |
|
f"<b>Price Range</b>: {row.get('price_range', 'N/A')}<br>" |
|
f"<b>Location</b>: {row.get('locality', 'N/A')}, {row.get('country', 'N/A')}<br>" |
|
f"<b>Hotel Website URL</b>: <a href='{row.get('hotel_url', 'N/A')}' target='_blank'>Link</a><br>" |
|
f"<b>Google Maps</b>: <a href='{google_maps_url}' target='_blank'>View on Maps</a><br>" |
|
f"<b>Image</b>: <img src='{row.get('hotel_image', 'N/A')}' width='200' /><br>" |
|
) |
|
else: |
|
google_maps_url = f"https://www.google.com/maps/search/?api=1&query={row.get('name', 'N/A').replace(' ', '+')}" |
|
result = ( |
|
f"<b>Name</b>: {row.get('name', 'N/A')}<br>" |
|
f"<b>Location</b>: {row.get('location', 'N/A')}<br>" |
|
f"<b>Price</b>: {row.get('price', 'N/A')}<br>" |
|
f"<b>Price For</b>: {row.get('price_for', 'N/A')}<br>" |
|
f"<b>Room Type</b>: {row.get('room_type', 'N/A')}<br>" |
|
f"<b>Beds</b>: {row.get('beds', 'N/A')}<br>" |
|
f"<b>Rating</b>: {row.get('rating', 'N/A')}<br>" |
|
f"<b>Rating Title</b>: {row.get('rating_title', 'N/A')}<br>" |
|
f"<b>Google Maps</b>: <a href='{google_maps_url}' target='_blank'>View on Maps</a><br>" |
|
f"<b>Number of Ratings</b>: {row.get('number_of_ratings', 'N/A')}<br>" |
|
f"<b>Hotel Website URL</b>: <a href='{row.get('url', 'N/A')}' target='_blank'>Link</a><br>" |
|
f"<b>Additional Info</b>: {row.get('cm', 'N/A')}<br>" |
|
) |
|
formatted_results.append(result) |
|
return "<br><br>".join(formatted_results) |
|
|
|
def search_interface(query_text): |
|
results = search_in_faiss(query_text, model, 7) |
|
return format_results(results) |
|
|
|
iface = gr.Interface( |
|
fn=search_interface, |
|
inputs=gr.Textbox(label="Enter your search query"), |
|
outputs=gr.HTML(label="Search Results"), |
|
title="Hotel and Arabic Data Search", |
|
description="Enter a query to search for hotels or Arabic data. The results will show the top matches based on similarity and provide a Google Maps URL for hotel locations.", |
|
examples=["Riyadh", "Deluxe Room"] |
|
) |
|
|
|
iface.launch() |
|
|