Spaces:
Sleeping
Sleeping
set Share True
Browse files
app.py
CHANGED
@@ -116,20 +116,23 @@ def get_user_embedding(query):
|
|
116 |
# Find similar places based on cosine similarity
|
117 |
def get_similar_places(user_embedding):
|
118 |
similarities = []
|
119 |
-
try:
|
120 |
-
res = cur.execute("SELECT * FROM places").fetchall()
|
121 |
-
|
122 |
-
for place in res:
|
123 |
-
embedding_str = place['Embedding']
|
124 |
-
embedding = np.array([float(x) for x in embedding_str.split(',')])
|
125 |
-
similarity = cosine_similarity([user_embedding], [embedding])[0][0]
|
126 |
-
similarities.append((place, similarity))
|
127 |
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
# Main function to get top 5 destinations
|
135 |
def get_top_5_destinations(user_query):
|
@@ -208,4 +211,4 @@ iface = gr.Interface(
|
|
208 |
|
209 |
# Launch the Gradio App
|
210 |
if __name__ == "__main__":
|
211 |
-
iface.launch(
|
|
|
116 |
# Find similar places based on cosine similarity
|
117 |
def get_similar_places(user_embedding):
|
118 |
similarities = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
+
# Select all places from the database
|
121 |
+
res = cur.execute("SELECT * FROM places").fetchall()
|
122 |
+
|
123 |
+
for place in res:
|
124 |
+
embedding_str = place['Embedding'] # Assuming embeddings are stored as comma-separated strings in the database
|
125 |
+
embedding = np.array([float(x) for x in embedding_str.split(',')]) # Convert the string back to a numpy array
|
126 |
+
|
127 |
+
# Compute cosine similarity
|
128 |
+
similarity = cosine_similarity([user_embedding], [embedding])[0][0]
|
129 |
+
similarities.append((place, similarity))
|
130 |
+
|
131 |
+
# Sort results based on similarity and then by rating
|
132 |
+
ranked_results = sorted(similarities, key=lambda x: (x[1], x[0]['Rating']), reverse=True)
|
133 |
+
|
134 |
+
# Return top places
|
135 |
+
return ranked_results
|
136 |
|
137 |
# Main function to get top 5 destinations
|
138 |
def get_top_5_destinations(user_query):
|
|
|
211 |
|
212 |
# Launch the Gradio App
|
213 |
if __name__ == "__main__":
|
214 |
+
iface.launch(share=True)
|