Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,17 @@ from sentence_transformers import SentenceTransformer
|
|
3 |
import gradio as gr
|
4 |
import spacy
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
model = SentenceTransformer("nomic-ai/nomic-embed-text-v1", trust_remote_code=True)
|
8 |
|
@@ -12,24 +23,23 @@ df_new = pd.read_csv('last_df.csv')
|
|
12 |
df_new['country'] = df_new['country'].replace('Türkiye', 'Turkey')
|
13 |
#
|
14 |
#
|
15 |
-
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
#
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
# return df_new
|
33 |
|
34 |
|
35 |
|
@@ -60,18 +70,18 @@ def process_query(query):
|
|
60 |
query_embedding = model.encode(query)
|
61 |
|
62 |
# Filter DataFrame by location
|
63 |
-
|
64 |
|
65 |
# Convert query_embedding to a tensor if it is not already
|
66 |
query_embedding_tensor = torch.tensor(query_embedding)
|
67 |
|
68 |
# Apply the similarity function to the filtered DataFrame
|
69 |
-
|
70 |
|
71 |
-
df_new['similarity_score'] = df_new.apply(lambda row: get_similarity_score(row, query_embedding_tensor), axis=1)
|
72 |
|
73 |
|
74 |
-
top_similar =
|
75 |
|
76 |
|
77 |
hotel_name = top_similar['hotel_name'].values[0]
|
|
|
3 |
import gradio as gr
|
4 |
import spacy
|
5 |
|
6 |
+
import subprocess
|
7 |
+
|
8 |
+
# Run the spacy model download command
|
9 |
+
|
10 |
+
try:
|
11 |
+
# Try to load the model to check if it's already installed
|
12 |
+
nlp = spacy.load("en_core_web_trf")
|
13 |
+
except OSError:
|
14 |
+
# If the model is not found, download it
|
15 |
+
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_trf"])
|
16 |
+
nlp = spacy.load("en_core_web_trf")
|
17 |
|
18 |
model = SentenceTransformer("nomic-ai/nomic-embed-text-v1", trust_remote_code=True)
|
19 |
|
|
|
23 |
df_new['country'] = df_new['country'].replace('Türkiye', 'Turkey')
|
24 |
#
|
25 |
#
|
26 |
+
|
27 |
+
# Function to extract city name from the query
|
28 |
+
def get_city_name(query):
|
29 |
+
text_query = nlp(query)
|
30 |
+
for city in text_query.ents:
|
31 |
+
if city.label_ == "GPE":
|
32 |
+
return city.text.lower()
|
33 |
+
return None
|
34 |
+
|
35 |
+
# Function to filter DataFrame by location
|
36 |
+
def filter_by_loc(query):
|
37 |
+
city_name = get_city_name(query)
|
38 |
+
if city_name in df_new['locality'].str.lower().unique():
|
39 |
+
filtered_df = df_new[df_new['locality'].str.lower() == city_name.lower()]
|
40 |
+
return filtered_df
|
41 |
+
else:
|
42 |
+
return df_new
|
|
|
43 |
|
44 |
|
45 |
|
|
|
70 |
query_embedding = model.encode(query)
|
71 |
|
72 |
# Filter DataFrame by location
|
73 |
+
filtered_data = filter_by_loc(query)
|
74 |
|
75 |
# Convert query_embedding to a tensor if it is not already
|
76 |
query_embedding_tensor = torch.tensor(query_embedding)
|
77 |
|
78 |
# Apply the similarity function to the filtered DataFrame
|
79 |
+
filtered_data['similarity_score'] = filtered_data.apply(lambda row: get_similarity_score(row, query_embedding_tensor), axis=1)
|
80 |
|
81 |
+
# df_new['similarity_score'] = df_new.apply(lambda row: get_similarity_score(row, query_embedding_tensor), axis=1)
|
82 |
|
83 |
|
84 |
+
top_similar = filtered_data.sort_values('similarity_score', ascending=False).head(1)
|
85 |
|
86 |
|
87 |
hotel_name = top_similar['hotel_name'].values[0]
|