Spaces:
Sleeping
Sleeping
tonyliu404
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -36,5 +36,65 @@ def getEmbedding(text):
|
|
36 |
return []
|
37 |
encoded = embedding.encode(text)
|
38 |
return encoded.tolist()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
print("HELLO WORLD")
|
40 |
-
st.title("
|
|
|
36 |
return []
|
37 |
encoded = embedding.encode(text)
|
38 |
return encoded.tolist()
|
39 |
+
|
40 |
+
|
41 |
+
# Connect to MongoDB
|
42 |
+
def get_mongo_client(mongo_uri):
|
43 |
+
try:
|
44 |
+
client = pymongo.MongoClient(mongo_uri)
|
45 |
+
print("Connection to MongoDB successful")
|
46 |
+
return client
|
47 |
+
except pymongo.errors.ConnectionFailure as e:
|
48 |
+
print(f"Connection failed: {e}")
|
49 |
+
return None
|
50 |
+
|
51 |
+
if not mongo_uri:
|
52 |
+
print("MONGO_URI not set in env")
|
53 |
+
|
54 |
+
mongo_client = get_mongo_client(mongo_uri)
|
55 |
+
|
56 |
+
mongo_db = mongo_client['recipes']
|
57 |
+
mongo_collection = mongo_db['recipesCollection']
|
58 |
+
|
59 |
+
def vector_search(user_query, collection):
|
60 |
+
query_embedding = getEmbedding(user_query)
|
61 |
+
if query_embedding is None:
|
62 |
+
return "Invalid query or embedding gen failed"
|
63 |
+
vector_search_stage = {
|
64 |
+
"$vectorSearch": {
|
65 |
+
"index": "vector_index",
|
66 |
+
"queryVector": query_embedding,
|
67 |
+
"path": "embedding",
|
68 |
+
"numCandidates": 150, # Number of candidate matches to consider
|
69 |
+
"limit": 4 # Return top 4 matches
|
70 |
+
}
|
71 |
+
}
|
72 |
+
|
73 |
+
unset_stage = {
|
74 |
+
"$unset": "embedding" # Exclude the 'embedding' field from the results
|
75 |
+
}
|
76 |
+
|
77 |
+
project_stage = {
|
78 |
+
"$project": {
|
79 |
+
"_id": 0, # Exclude the _id field
|
80 |
+
"name": 1,
|
81 |
+
"minutes": 1,
|
82 |
+
"tags": 1,
|
83 |
+
"n_steps": 1,
|
84 |
+
"description": 1,
|
85 |
+
"ingredients": 1,
|
86 |
+
"n_ingredients": 1,
|
87 |
+
"formatted_nutrition": 1,
|
88 |
+
"formatted_steps": 1,
|
89 |
+
"score": {
|
90 |
+
"$meta": "vectorSearchScore" # Include the search score
|
91 |
+
}
|
92 |
+
}
|
93 |
+
}
|
94 |
+
|
95 |
+
pipeline = [vector_search_stage, unset_stage, project_stage]
|
96 |
+
results = mongo_collection.aggregate(pipeline)
|
97 |
+
return list(results)
|
98 |
+
|
99 |
print("HELLO WORLD")
|
100 |
+
st.title("qwerty :D")
|