AzizTh commited on
Commit
da29c4d
1 Parent(s): f08eff9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -22
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
- # nlp = spacy.load("en_core_web_trf")
16
- #
17
- # # Function to extract city name from the query
18
- # def get_city_name(query):
19
- # text_query = nlp(query)
20
- # for city in text_query.ents:
21
- # if city.label_ == "GPE":
22
- # return city.text.lower()
23
- # return None
24
- #
25
- # # Function to filter DataFrame by location
26
- # def filter_by_loc(query):
27
- # city_name = get_city_name(query)
28
- # if city_name in df_new['locality'].str.lower().unique():
29
- # filtered_df = df_new[df_new['locality'].str.lower() == city_name.lower()]
30
- # return filtered_df
31
- # else:
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
- # filtered_data = filter_by_loc(query)
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
- # filtered_data['similarity_score'] = filtered_data.apply(lambda row: get_similarity_score(row, query_embedding_tensor), axis=1)
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 = df_new.sort_values('similarity_score', ascending=False).head(1)
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]