Spaces:
Runtime error
Runtime error
Rename app to app.py
Browse files
app
DELETED
File without changes
|
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# Function to summarize text using BART
|
3 |
+
def summarize_text(text):
|
4 |
+
inputs = tokenizer.encode("" + text, return_tensors="pt", max_length=1024, truncation=True)
|
5 |
+
summary_ids = model_bart.generate(inputs, max_length=150, min_length=30, length_penalty=2.0, num_beams=4, early_stopping=True)
|
6 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
7 |
+
return summary
|
8 |
+
|
9 |
+
def search_hotels(query_text, k=1):
|
10 |
+
try:
|
11 |
+
# Encode the query text
|
12 |
+
query_embedding = model.encode(query_text, convert_to_tensor=True)
|
13 |
+
query_embedding = query_embedding.cpu().numpy().reshape(1, -1)
|
14 |
+
query_embedding = normalize(query_embedding, norm='l2')
|
15 |
+
|
16 |
+
# Compute cosine similarity between query and stored embeddings
|
17 |
+
similarities = cosine_similarity(query_embedding, normalized_embeddings)
|
18 |
+
|
19 |
+
# Get indices of the top k similar hotels
|
20 |
+
top_indices = similarities[0].argsort()[-k:][::-1]
|
21 |
+
|
22 |
+
# Retrieve the top k similar hotels
|
23 |
+
top_hotels = df_copy_first_1000.iloc[top_indices]
|
24 |
+
|
25 |
+
# Prepare results
|
26 |
+
results = []
|
27 |
+
for _, row in top_hotels.iterrows():
|
28 |
+
# Create a summary for the description, review title, review text, and review count
|
29 |
+
summary_text = f"Description: {row['hotel_description']}\nReview Title: {row['review_title']}\nReview Text: {row['review_text']}\nReview Count: {row['review_count']}"
|
30 |
+
summary = summarize_text(summary_text)
|
31 |
+
|
32 |
+
result = (
|
33 |
+
f"Hotel Name: {row['hotel_name']}\n"
|
34 |
+
f"Locality: {row['locality']}\n"
|
35 |
+
f"Price Range: {row['price_range']}\n"
|
36 |
+
f"Rate: {row['rate']}\n"
|
37 |
+
f"\n {summary}\n"
|
38 |
+
)
|
39 |
+
results.append(result)
|
40 |
+
|
41 |
+
return "\n\n".join(results)
|
42 |
+
|
43 |
+
except Exception as e:
|
44 |
+
return f"An error occurred during the search: {e}"
|
45 |
+
|
46 |
+
# Gradio Interface
|
47 |
+
iface = gr.Interface(
|
48 |
+
fn=search_hotels,
|
49 |
+
inputs=gr.Textbox(label="Enter your search query"),
|
50 |
+
outputs="text",
|
51 |
+
title="Hotel Search Engine",
|
52 |
+
description="Enter a query to search for hotels and get details about the top results."
|
53 |
+
)
|
54 |
+
|
55 |
+
# Launch Gradio Interface
|
56 |
+
if __name__ == "__main__":
|
57 |
+
iface.launch()
|