Spaces:
Runtime error
Runtime error
Pavel Duchovny
commited on
Commit
·
be5aa57
1
Parent(s):
dd4c4b0
new features
Browse files- app.py +60 -35
- iframe.html +0 -1
app.py
CHANGED
@@ -7,25 +7,36 @@ from openai import OpenAI
|
|
7 |
openai_client = OpenAI()
|
8 |
import os
|
9 |
|
10 |
-
uri = os.environ.get('MONGODB_ATLAS_URI')
|
11 |
-
client = MongoClient(uri)
|
12 |
-
db_name = 'whatscooking'
|
13 |
-
collection_name = 'restaurants'
|
14 |
-
restaurants_collection = client[db_name][collection_name]
|
15 |
-
trips_collection = client[db_name]['smart_trips']
|
16 |
|
17 |
|
18 |
|
|
|
19 |
def get_restaurants(search, location, meters):
|
20 |
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
response = openai_client.embeddings.create(
|
24 |
input=search,
|
25 |
model="text-embedding-3-small",
|
26 |
dimensions=256
|
27 |
)
|
28 |
|
|
|
29 |
vectorQuery = {
|
30 |
"$vectorSearch": {
|
31 |
"index" : "vector_index",
|
@@ -35,6 +46,8 @@ def get_restaurants(search, location, meters):
|
|
35 |
"limit": 3,
|
36 |
"filter": {"searchTrip": newTrip}
|
37 |
}}
|
|
|
|
|
38 |
restaurant_docs = list(trips_collection.aggregate([vectorQuery,
|
39 |
{"$project": {"_id" : 0, "embedding": 0}}]))
|
40 |
|
@@ -47,10 +60,14 @@ def get_restaurants(search, location, meters):
|
|
47 |
]
|
48 |
)
|
49 |
|
|
|
50 |
trips_collection.delete_many({"searchTrip": newTrip})
|
|
|
|
|
51 |
if len(restaurant_docs) == 0:
|
52 |
return "No restaurants found", '<iframe style="background: #FFFFFF;border: none;border-radius: 2px;box-shadow: 0 2px 10px 0 rgba(70, 76, 79, .2);" width="640" height="480" src="https://charts.mongodb.com/charts-paveldev-wiumf/embed/charts?id=65c24b0c-2215-4e6f-829c-f484dfd8a90c&filter={\'restaurant_id\':\'\'}&maxDataAge=3600&theme=light&autoRefresh=true"></iframe>', str(pre_agg), str(vectorQuery)
|
53 |
|
|
|
54 |
first_restaurant = restaurant_docs[0]['restaurant_id']
|
55 |
second_restaurant = restaurant_docs[1]['restaurant_id']
|
56 |
third_restaurant = restaurant_docs[2]['restaurant_id']
|
@@ -58,12 +75,13 @@ def get_restaurants(search, location, meters):
|
|
58 |
|
59 |
|
60 |
iframe = '<iframe style="background: #FFFFFF;border: none;border-radius: 2px;box-shadow: 0 2px 10px 0 rgba(70, 76, 79, .2);" width="640" height="480" src="https://charts.mongodb.com/charts-paveldev-wiumf/embed/charts?id=65c24b0c-2215-4e6f-829c-f484dfd8a90c&filter={\'restaurant_id\':{$in:[' + restaurant_string + ']}}&maxDataAge=3600&theme=light&autoRefresh=true"></iframe>'
|
61 |
-
|
62 |
return chat_response.choices[0].message.content, iframe,str(pre_agg), str(vectorQuery)
|
63 |
|
64 |
|
65 |
-
def pre_aggregate_meters(location, meters):
|
66 |
|
|
|
67 |
tripId = ObjectId()
|
68 |
pre_aggregate_pipeline = [{
|
69 |
"$geoNear": {
|
@@ -87,8 +105,7 @@ def pre_aggregate_meters(location, meters):
|
|
87 |
|
88 |
result = restaurants_collection.aggregate(pre_aggregate_pipeline);
|
89 |
|
90 |
-
|
91 |
-
sleep(5)
|
92 |
|
93 |
return tripId, pre_aggregate_pipeline
|
94 |
|
@@ -97,38 +114,46 @@ with gr.Blocks() as demo:
|
|
97 |
gr.Markdown(
|
98 |
"""
|
99 |
# MongoDB's Vector Restaurant planner
|
100 |
-
Start typing below to see the results
|
|
|
|
|
101 |
""")
|
102 |
-
|
103 |
-
#
|
104 |
gr.Interface(
|
105 |
get_restaurants,
|
106 |
-
[
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
"type": "Point",
|
122 |
-
"coordinates": [ -74.000468,40.720777
|
123 |
-
]
|
124 |
-
})], label="Location", info="What location you need?"),
|
125 |
gr.Slider(minimum=500, maximum=10000, randomize=False, step=5, label="Radius in meters")],
|
126 |
[gr.Textbox(label="MongoDB Vector Recommendations", placeholder="Results will be displayed here"), "html",
|
127 |
gr.Code(label="Pre-aggregate pipeline",language="json" ),
|
128 |
gr.Code(label="Vector Query", language="json")],
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
)
|
131 |
-
|
|
|
132 |
if __name__ == "__main__":
|
133 |
demo.launch()
|
134 |
|
|
|
7 |
openai_client = OpenAI()
|
8 |
import os
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
|
12 |
|
13 |
+
## Get the restaurants based on the search and location
|
14 |
def get_restaurants(search, location, meters):
|
15 |
|
16 |
+
try:
|
17 |
+
uri = os.environ.get('MONGODB_ATLAS_URI')
|
18 |
+
client = MongoClient(uri)
|
19 |
+
db_name = 'whatscooking'
|
20 |
+
collection_name = 'restaurants'
|
21 |
+
restaurants_collection = client[db_name][collection_name]
|
22 |
+
trips_collection = client[db_name]['smart_trips']
|
23 |
|
24 |
+
except:
|
25 |
+
print("Error Connecting to the MongoDB Atlas Cluster")
|
26 |
+
|
27 |
+
|
28 |
+
# Pre aggregate restaurants collection based on chosen location and radius, the output is stored into
|
29 |
+
# trips_collection
|
30 |
+
newTrip, pre_agg = pre_aggregate_meters(restaurants_collection, location, meters)
|
31 |
+
|
32 |
+
## Get openai embeddings
|
33 |
response = openai_client.embeddings.create(
|
34 |
input=search,
|
35 |
model="text-embedding-3-small",
|
36 |
dimensions=256
|
37 |
)
|
38 |
|
39 |
+
## prepare the similarity search on current trip
|
40 |
vectorQuery = {
|
41 |
"$vectorSearch": {
|
42 |
"index" : "vector_index",
|
|
|
46 |
"limit": 3,
|
47 |
"filter": {"searchTrip": newTrip}
|
48 |
}}
|
49 |
+
|
50 |
+
## Run the retrieved documents through a RAG system.
|
51 |
restaurant_docs = list(trips_collection.aggregate([vectorQuery,
|
52 |
{"$project": {"_id" : 0, "embedding": 0}}]))
|
53 |
|
|
|
60 |
]
|
61 |
)
|
62 |
|
63 |
+
## Removed the temporary documents
|
64 |
trips_collection.delete_many({"searchTrip": newTrip})
|
65 |
+
|
66 |
+
|
67 |
if len(restaurant_docs) == 0:
|
68 |
return "No restaurants found", '<iframe style="background: #FFFFFF;border: none;border-radius: 2px;box-shadow: 0 2px 10px 0 rgba(70, 76, 79, .2);" width="640" height="480" src="https://charts.mongodb.com/charts-paveldev-wiumf/embed/charts?id=65c24b0c-2215-4e6f-829c-f484dfd8a90c&filter={\'restaurant_id\':\'\'}&maxDataAge=3600&theme=light&autoRefresh=true"></iframe>', str(pre_agg), str(vectorQuery)
|
69 |
|
70 |
+
## Build the map filter
|
71 |
first_restaurant = restaurant_docs[0]['restaurant_id']
|
72 |
second_restaurant = restaurant_docs[1]['restaurant_id']
|
73 |
third_restaurant = restaurant_docs[2]['restaurant_id']
|
|
|
75 |
|
76 |
|
77 |
iframe = '<iframe style="background: #FFFFFF;border: none;border-radius: 2px;box-shadow: 0 2px 10px 0 rgba(70, 76, 79, .2);" width="640" height="480" src="https://charts.mongodb.com/charts-paveldev-wiumf/embed/charts?id=65c24b0c-2215-4e6f-829c-f484dfd8a90c&filter={\'restaurant_id\':{$in:[' + restaurant_string + ']}}&maxDataAge=3600&theme=light&autoRefresh=true"></iframe>'
|
78 |
+
client.close()
|
79 |
return chat_response.choices[0].message.content, iframe,str(pre_agg), str(vectorQuery)
|
80 |
|
81 |
|
82 |
+
def pre_aggregate_meters(restaurants_collection, location, meters):
|
83 |
|
84 |
+
## Do the geo location preaggregate and assign the search trip id.
|
85 |
tripId = ObjectId()
|
86 |
pre_aggregate_pipeline = [{
|
87 |
"$geoNear": {
|
|
|
105 |
|
106 |
result = restaurants_collection.aggregate(pre_aggregate_pipeline);
|
107 |
|
108 |
+
sleep(3)
|
|
|
109 |
|
110 |
return tripId, pre_aggregate_pipeline
|
111 |
|
|
|
114 |
gr.Markdown(
|
115 |
"""
|
116 |
# MongoDB's Vector Restaurant planner
|
117 |
+
Start typing below to see the results. You can search a specific cuisine for you and choose 3 predefined locations.
|
118 |
+
|
119 |
+
The radius specify the distance from the start search location.
|
120 |
""")
|
121 |
+
|
122 |
+
# Create the interface
|
123 |
gr.Interface(
|
124 |
get_restaurants,
|
125 |
+
[gr.Textbox(placeholder="What type of dinner are you looking for?"),
|
126 |
+
gr.Radio(choices=[
|
127 |
+
("Timesquare Manhattan", {
|
128 |
+
"type": "Point",
|
129 |
+
"coordinates": [-73.98527039999999, 40.7589099]
|
130 |
+
}),
|
131 |
+
("Westside Manhattan", {
|
132 |
+
"type": "Point",
|
133 |
+
"coordinates": [-74.013686, 40.701975]
|
134 |
+
}),
|
135 |
+
("Downtown Manhattan", {
|
136 |
+
"type": "Point",
|
137 |
+
"coordinates": [-74.000468, 40.720777]
|
138 |
+
})
|
139 |
+
], label="Location", info="What location you need?"),
|
|
|
|
|
|
|
|
|
140 |
gr.Slider(minimum=500, maximum=10000, randomize=False, step=5, label="Radius in meters")],
|
141 |
[gr.Textbox(label="MongoDB Vector Recommendations", placeholder="Results will be displayed here"), "html",
|
142 |
gr.Code(label="Pre-aggregate pipeline",language="json" ),
|
143 |
gr.Code(label="Vector Query", language="json")],
|
144 |
+
examples=[
|
145 |
+
["Laxuary italian",
|
146 |
+
[("Westside Manhattan", {
|
147 |
+
"type": "Point",
|
148 |
+
"coordinates": [-74.013686, 40.701975]
|
149 |
+
})]
|
150 |
+
, 1500]
|
151 |
+
|
152 |
+
],
|
153 |
+
live=False
|
154 |
)
|
155 |
+
|
156 |
+
|
157 |
if __name__ == "__main__":
|
158 |
demo.launch()
|
159 |
|
iframe.html
DELETED
@@ -1 +0,0 @@
|
|
1 |
-
<iframe style="background: #FFFFFF;border: none;border-radius: 2px;box-shadow: 0 2px 10px 0 rgba(70, 76, 79, .2);" width="640" height="480" src="https://charts.mongodb.com/charts-paveldev-wiumf/embed/charts?id=65c24b0c-2215-4e6f-829c-f484dfd8a90c&filter={'restaurant_id':{$in:['50005104', '41166347', '41314543']}}&maxDataAge=3600&theme=light&autoRefresh=true"></iframe>
|
|
|
|