Spaces:
Build error
Build error
Pavel Duchovny
commited on
Commit
·
d580761
1
Parent(s):
f937f4c
init
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from time import sleep
|
|
|
|
| 3 |
from pymongo import MongoClient
|
| 4 |
from bson import ObjectId
|
| 5 |
from openai import OpenAI
|
|
@@ -17,7 +18,7 @@ trips_collection = client[db_name]['smart_trips']
|
|
| 17 |
|
| 18 |
def get_restaurants(search, location, meters):
|
| 19 |
|
| 20 |
-
newTrip = pre_aggregate_meters(location, meters)
|
| 21 |
|
| 22 |
response = openai_client.embeddings.create(
|
| 23 |
input=search,
|
|
@@ -25,7 +26,7 @@ def get_restaurants(search, location, meters):
|
|
| 25 |
dimensions=256
|
| 26 |
)
|
| 27 |
|
| 28 |
-
|
| 29 |
"$vectorSearch": {
|
| 30 |
"index" : "vector_index",
|
| 31 |
"queryVector": response.data[0].embedding,
|
|
@@ -33,7 +34,8 @@ def get_restaurants(search, location, meters):
|
|
| 33 |
"numCandidates": 10,
|
| 34 |
"limit": 3,
|
| 35 |
"filter": {"searchTrip": newTrip}
|
| 36 |
-
}}
|
|
|
|
| 37 |
{"$project": {"_id" : 0, "embedding": 0}}]))
|
| 38 |
|
| 39 |
|
|
@@ -57,15 +59,13 @@ def get_restaurants(search, location, meters):
|
|
| 57 |
restaurant_string = f"\'{first_restaurant}\', \'{second_restaurant}\', \'{third_restaurant}\'"
|
| 58 |
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>'
|
| 59 |
|
| 60 |
-
return chat_response.choices[0].message.content, iframe
|
| 61 |
|
| 62 |
|
| 63 |
def pre_aggregate_meters(location, meters):
|
| 64 |
|
| 65 |
tripId = ObjectId()
|
| 66 |
-
|
| 67 |
-
result = restaurants_collection.aggregate([
|
| 68 |
-
{
|
| 69 |
"$geoNear": {
|
| 70 |
"near": location,
|
| 71 |
"distanceField": "distance",
|
|
@@ -83,14 +83,14 @@ def pre_aggregate_meters(location, meters):
|
|
| 83 |
"$merge": {
|
| 84 |
"into": "smart_trips"
|
| 85 |
}
|
| 86 |
-
}
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
-
print(result)
|
| 90 |
print(trips_collection.count_documents({"searchTrip": tripId}));
|
| 91 |
sleep(10)
|
| 92 |
|
| 93 |
-
return tripId
|
| 94 |
|
| 95 |
|
| 96 |
with gr.Blocks() as demo:
|
|
@@ -115,15 +115,17 @@ with gr.Blocks() as demo:
|
|
| 115 |
}), ("home",{
|
| 116 |
"type": "Point",
|
| 117 |
"coordinates": [
|
| 118 |
-
|
| 119 |
]
|
| 120 |
}), ("park", {
|
| 121 |
"type": "Point",
|
| 122 |
-
"coordinates": [
|
| 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 |
|
| 128 |
)
|
| 129 |
#radio.change(location_searched, loc, out)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from time import sleep
|
| 3 |
+
import json
|
| 4 |
from pymongo import MongoClient
|
| 5 |
from bson import ObjectId
|
| 6 |
from openai import OpenAI
|
|
|
|
| 18 |
|
| 19 |
def get_restaurants(search, location, meters):
|
| 20 |
|
| 21 |
+
newTrip, pre_agg = pre_aggregate_meters(location, meters)
|
| 22 |
|
| 23 |
response = openai_client.embeddings.create(
|
| 24 |
input=search,
|
|
|
|
| 26 |
dimensions=256
|
| 27 |
)
|
| 28 |
|
| 29 |
+
vectorQuery = {
|
| 30 |
"$vectorSearch": {
|
| 31 |
"index" : "vector_index",
|
| 32 |
"queryVector": response.data[0].embedding,
|
|
|
|
| 34 |
"numCandidates": 10,
|
| 35 |
"limit": 3,
|
| 36 |
"filter": {"searchTrip": newTrip}
|
| 37 |
+
}}
|
| 38 |
+
restaurant_docs = list(trips_collection.aggregate([vectorQuery,
|
| 39 |
{"$project": {"_id" : 0, "embedding": 0}}]))
|
| 40 |
|
| 41 |
|
|
|
|
| 59 |
restaurant_string = f"\'{first_restaurant}\', \'{second_restaurant}\', \'{third_restaurant}\'"
|
| 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": {
|
| 70 |
"near": location,
|
| 71 |
"distanceField": "distance",
|
|
|
|
| 83 |
"$merge": {
|
| 84 |
"into": "smart_trips"
|
| 85 |
}
|
| 86 |
+
} ]
|
| 87 |
+
|
| 88 |
+
result = restaurants_collection.aggregate(pre_aggregate_pipeline);
|
| 89 |
|
|
|
|
| 90 |
print(trips_collection.count_documents({"searchTrip": tripId}));
|
| 91 |
sleep(10)
|
| 92 |
|
| 93 |
+
return tripId, pre_aggregate_pipeline
|
| 94 |
|
| 95 |
|
| 96 |
with gr.Blocks() as demo:
|
|
|
|
| 115 |
}), ("home",{
|
| 116 |
"type": "Point",
|
| 117 |
"coordinates": [
|
| 118 |
+
-74.013686, 40.701975
|
| 119 |
]
|
| 120 |
}), ("park", {
|
| 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 |
#radio.change(location_searched, loc, out)
|