Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -35,20 +35,29 @@ print ('Creating vector search index')
|
|
| 35 |
# print ('Waiting for vector index on field "embedding" to be created')
|
| 36 |
# time.sleep(60)
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
def get_movies(message, history):
|
| 41 |
# Use AsyncIO to run the similarity search in the background
|
| 42 |
# movies = vector_store.similarity_search(message, 3)
|
| 43 |
print ('Searching for: ' + message)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
demo = gr.ChatInterface(get_movies, examples=["What movies are scary?", "Find me a comedy", "Movies for kids"], title="Movies Atlas Vector Search", submit_btn="Search").queue()
|
|
|
|
| 35 |
# print ('Waiting for vector index on field "embedding" to be created')
|
| 36 |
# time.sleep(60)
|
| 37 |
|
| 38 |
+
try:
|
| 39 |
+
vector_store = MongoDBAtlasVectorSearch(embedding=OpenAIEmbeddings(), collection=collection, index_name='vector_index', text_key='plot', embedding_key='plot_embedding')
|
| 40 |
+
|
| 41 |
+
except:
|
| 42 |
+
# If open ai key is wrong
|
| 43 |
+
print ('Open AI key is wrong')
|
| 44 |
+
vector_store = None
|
| 45 |
|
| 46 |
def get_movies(message, history):
|
| 47 |
# Use AsyncIO to run the similarity search in the background
|
| 48 |
# movies = vector_store.similarity_search(message, 3)
|
| 49 |
print ('Searching for: ' + message)
|
| 50 |
+
try:
|
| 51 |
+
movies = vector_store.similarity_search(message, 3)
|
| 52 |
+
retrun_text = ''
|
| 53 |
+
for movie in movies:
|
| 54 |
+
retrun_text = retrun_text + 'Title : ' + movie.metadata['title'] + '\n------------\n' + 'Plot: ' + movie.page_content + '\n\n'
|
| 55 |
+
|
| 56 |
+
for i in range(len(retrun_text)):
|
| 57 |
+
time.sleep(0.05)
|
| 58 |
+
yield "Found: " + "\n\n" + retrun_text[: i+1]
|
| 59 |
+
except:
|
| 60 |
+
yield "Please clone the repo and add your open ai key as well as your MongoDB Atlas UR in the Secret Section of you Space\n OPENAI_API_KEY (your Open AI key) and MONGODB_ATLAS_CLUSTER_URI (0.0.0.0/0 whitelisted instance with Vector index created) \n\n For more information : https://mongodb.com/products/platform/atlas-vector-search"
|
| 61 |
|
| 62 |
|
| 63 |
demo = gr.ChatInterface(get_movies, examples=["What movies are scary?", "Find me a comedy", "Movies for kids"], title="Movies Atlas Vector Search", submit_btn="Search").queue()
|