Spaces:
Sleeping
Sleeping
Update langchain_movie_search.py
Browse files- langchain_movie_search.py +10 -23
langchain_movie_search.py
CHANGED
|
@@ -32,9 +32,12 @@ class MoviesSearch:
|
|
| 32 |
mongodb_connection_url: str = os.getenv("MONGODB_CONNECTION_URL")
|
| 33 |
mongodb_db_name: str = os.getenv("MONGODB_DB_NAME")
|
| 34 |
mongodb_collection_name: str = os.getenv("MONGODB_COLLECTION_NAME")
|
|
|
|
|
|
|
|
|
|
| 35 |
self.huggingface_api_token: str = os.getenv("HF_TOKEN")
|
| 36 |
self.huggingface_text_generation_model: str = os.getenv("HUGGINGFACE_TEXT_GENERATION_MODEL")
|
| 37 |
-
|
| 38 |
# Setup MongoDB connection
|
| 39 |
self.client: pymongo.synchronous.mongo_client.MongoClient = pymongo.MongoClient(mongodb_connection_url)
|
| 40 |
db: str = mongodb_db_name
|
|
@@ -47,26 +50,11 @@ class MoviesSearch:
|
|
| 47 |
|
| 48 |
self.retrieve_vector_store = MongoDBAtlasVectorSearch(collection=self.langchain_movies_collection,
|
| 49 |
embedding=self.hf_plot_embedding,
|
| 50 |
-
embedding_key=
|
| 51 |
-
index_name=
|
| 52 |
-
text_key=
|
| 53 |
)
|
| 54 |
|
| 55 |
-
def generate_insert_embeddings(self):
|
| 56 |
-
"""
|
| 57 |
-
Generate vector embeddings
|
| 58 |
-
"""
|
| 59 |
-
new_doc_list: List = []
|
| 60 |
-
for doc in self.sample_movies_collection.find({"fullplot": {"$exists": True}}).limit(9000):
|
| 61 |
-
new_doc_list.append({
|
| 62 |
-
"movie-title": doc["title"],
|
| 63 |
-
"movie-plot": doc["fullplot"],
|
| 64 |
-
"text": doc["fullplot"],
|
| 65 |
-
"embedding": self.hf_plot_embedding.embed_query(doc["fullplot"])
|
| 66 |
-
})
|
| 67 |
-
|
| 68 |
-
self.langchain_movies_collection.insert_many(new_doc_list)
|
| 69 |
-
|
| 70 |
def query_data(self, query: str):
|
| 71 |
"""
|
| 72 |
Query data from Atlas Vector Search
|
|
@@ -92,7 +80,6 @@ class MoviesSearch:
|
|
| 92 |
formatted_prompt = prompt.format(context=query)
|
| 93 |
llm_answer = hf_llm.invoke(formatted_prompt)
|
| 94 |
llm_answer = llm_answer.split("\n", 1)[1]
|
| 95 |
-
print(llm_answer)
|
| 96 |
|
| 97 |
return llm_answer
|
| 98 |
|
|
@@ -102,12 +89,12 @@ class MoviesSearch:
|
|
| 102 |
gr.Markdown("# Generate Movie Plot using Vector Search + RAG")
|
| 103 |
with gr.Row():
|
| 104 |
textbox = gr.Textbox(label="Enter your prompt here:", lines=1,
|
| 105 |
-
placeholder="e.g. Generate a movie
|
| 106 |
with gr.Row():
|
| 107 |
button = gr.Button("Generate")
|
| 108 |
with gr.Column():
|
| 109 |
output = gr.Textbox(interactive=False,
|
| 110 |
-
label="Here is a Movie Plot for you. Don't forget to invite us to the premier!",
|
| 111 |
autoscroll=False,
|
| 112 |
show_label=True,
|
| 113 |
show_copy_button=True,
|
|
@@ -115,7 +102,7 @@ class MoviesSearch:
|
|
| 115 |
|
| 116 |
button.click(fn=self.query_data, inputs=textbox, outputs=[output])
|
| 117 |
|
| 118 |
-
dashboard.launch(
|
| 119 |
|
| 120 |
|
| 121 |
if __name__ == "__main__":
|
|
|
|
| 32 |
mongodb_connection_url: str = os.getenv("MONGODB_CONNECTION_URL")
|
| 33 |
mongodb_db_name: str = os.getenv("MONGODB_DB_NAME")
|
| 34 |
mongodb_collection_name: str = os.getenv("MONGODB_COLLECTION_NAME")
|
| 35 |
+
mongodb_index_name: str = os.getenv("MONGODB_INDEX_NAME")
|
| 36 |
+
text_key: str = os.getenv("TEXT_KEY")
|
| 37 |
+
embedding_key: str = os.getenv("EMBEDDING_KEY")
|
| 38 |
self.huggingface_api_token: str = os.getenv("HF_TOKEN")
|
| 39 |
self.huggingface_text_generation_model: str = os.getenv("HUGGINGFACE_TEXT_GENERATION_MODEL")
|
| 40 |
+
|
| 41 |
# Setup MongoDB connection
|
| 42 |
self.client: pymongo.synchronous.mongo_client.MongoClient = pymongo.MongoClient(mongodb_connection_url)
|
| 43 |
db: str = mongodb_db_name
|
|
|
|
| 50 |
|
| 51 |
self.retrieve_vector_store = MongoDBAtlasVectorSearch(collection=self.langchain_movies_collection,
|
| 52 |
embedding=self.hf_plot_embedding,
|
| 53 |
+
embedding_key=embedding_key,
|
| 54 |
+
index_name=mongodb_index_name,
|
| 55 |
+
text_key=text_key,
|
| 56 |
)
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
def query_data(self, query: str):
|
| 59 |
"""
|
| 60 |
Query data from Atlas Vector Search
|
|
|
|
| 80 |
formatted_prompt = prompt.format(context=query)
|
| 81 |
llm_answer = hf_llm.invoke(formatted_prompt)
|
| 82 |
llm_answer = llm_answer.split("\n", 1)[1]
|
|
|
|
| 83 |
|
| 84 |
return llm_answer
|
| 85 |
|
|
|
|
| 89 |
gr.Markdown("# Generate Movie Plot using Vector Search + RAG")
|
| 90 |
with gr.Row():
|
| 91 |
textbox = gr.Textbox(label="Enter your prompt here:", lines=1,
|
| 92 |
+
placeholder="e.g. Generate a movie where a couple discovers love during a war")
|
| 93 |
with gr.Row():
|
| 94 |
button = gr.Button("Generate")
|
| 95 |
with gr.Column():
|
| 96 |
output = gr.Textbox(interactive=False,
|
| 97 |
+
label="Here is a Movie Plot for you. Don't forget to invite us to the premier! :)",
|
| 98 |
autoscroll=False,
|
| 99 |
show_label=True,
|
| 100 |
show_copy_button=True,
|
|
|
|
| 102 |
|
| 103 |
button.click(fn=self.query_data, inputs=textbox, outputs=[output])
|
| 104 |
|
| 105 |
+
dashboard.launch(share=True)
|
| 106 |
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|