Pash1986 commited on
Commit
883ccea
1 Parent(s): ec1a5ac

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +130 -0
app.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import traceback
3
+ import gradio as gr
4
+ import os
5
+ import asyncio
6
+ from haystack_integrations.document_stores.mongodb_atlas import MongoDBAtlasDocumentStore
7
+ from haystack import Pipeline, Document
8
+ from haystack.components.generators import OpenAIGenerator
9
+ from haystack.components.builders.prompt_builder import PromptBuilder
10
+ from haystack.components.embedders import SentenceTransformersDocumentEmbedder, SentenceTransformersTextEmbedder
11
+ from haystack_integrations.document_stores.mongodb_atlas import MongoDBAtlasDocumentStore
12
+ from haystack_integrations.components.retrievers.mongodb_atlas import MongoDBAtlasEmbeddingRetriever
13
+ from haystack.components.embedders import OpenAITextEmbedder, OpenAIDocumentEmbedder
14
+ from haystack.document_stores.types import DuplicatePolicy
15
+ from haystack.components.writers import DocumentWriter
16
+
17
+ import json
18
+
19
+
20
+
21
+
22
+ try:
23
+
24
+ documents = [
25
+ Document(content="In the vibrant streets of Paris, Jean, a struggling painter, discovers an ancient secret hidden within his family’s artwork. As he unravels the mystery, he must navigate the treacherous art world, facing rivals and allies alike, on a journey that could redefine his destiny and restore a lost masterpiece."),
26
+ Document(content="Mark, a visionary software developer in Berlin, stumbles upon a groundbreaking algorithm that could change the tech world forever. But as he delves deeper, he finds himself entangled in a web of corporate espionage, challenging his ethics and risking everything he has worked for."),
27
+ Document(content="Giorgio, a dedicated archaeologist in Rome, uncovers an artifact that challenges the foundation of Roman history. His quest for truth leads him through ancient ruins and modern intrigue, as he battles to protect his discovery from those who wish to suppress it."),
28
+ Document(content="Lily, an ambitious journalist in New York, receives a mysterious diary that leads her on an investigation into a century-old mystery involving a forbidden romance and a family's hidden secrets."),
29
+ Document(content="Elena, a young physicist in Geneva, discovers a paradox within the laws of quantum mechanics that could alter our understanding of the universe. Her journey to prove her theory brings her face to face with dark matter and the very fabric of space and time."),
30
+ Document(content="Alex, a disillusioned detective in London, finds himself on the trail of a serial killer who leaves cryptic clues rooted in historical events. As he pieces together the puzzle, he uncovers a conspiracy that could shake the foundations of the British monarchy."),
31
+ Document(content="Nora, a marine biologist in the Great Barrier Reef, discovers a new species of coral with extraordinary regenerative properties. Her fight to protect her discovery from exploitation leads her into a conflict with powerful interests determined to harness the coral's powers for themselves."),
32
+ Document(content="Simon, an expert linguist in Cairo, deciphers an ancient scroll that reveals the location of a hidden chamber beneath the Sphinx. His search for the chamber propels him into a world of mystery and danger, as he confronts a secret society with its own agenda."),
33
+ Document(content="Amelia, a renowned chef in Tokyo, embarks on a quest to rediscover a lost recipe that was once the hallmark of her family's restaurant. Her journey takes her across Japan, delving into the country's culinary history and challenging her beliefs about cooking and tradition."),
34
+ Document(content="Victor, a retired astronaut in Houston, is contacted by an alien intelligence through a mysterious transmission. As he seeks to uncover the truth, he is drawn into an interstellar adventure that reveals the universe's greatest mysteries and humanity's place among the stars.")
35
+ ]
36
+
37
+ document_store = MongoDBAtlasDocumentStore(
38
+ database_name="sample_mflix",
39
+ collection_name="haystack_embedded_movies",
40
+ vector_search_index="default",
41
+ )
42
+ doc_writer = DocumentWriter(document_store=document_store, policy=DuplicatePolicy.SKIP)
43
+ doc_embedder = OpenAIDocumentEmbedder()
44
+
45
+ indexing_pipe = Pipeline()
46
+ indexing_pipe.add_component(instance=doc_embedder, name="doc_embedder")
47
+ indexing_pipe.add_component(instance=doc_writer, name="doc_writer")
48
+
49
+ indexing_pipe.connect("doc_embedder.documents", "doc_writer.documents")
50
+ indexing_pipe.run({"doc_embedder": {"documents": documents}})
51
+
52
+ prompt_template = """
53
+ You are a movie recommendation engine use the following cotext documents.\nDocuments:
54
+ {% for doc in documents %}
55
+ {{ doc.content }}
56
+ {% endfor %}
57
+
58
+ \Query: {{query}}
59
+ \nAnswer:
60
+ """
61
+
62
+ rag_pipeline = Pipeline()
63
+ rag_pipeline.add_component("text_embedder", OpenAITextEmbedder())
64
+
65
+
66
+ rag_pipeline.add_component(instance=MongoDBAtlasEmbeddingRetriever(document_store=document_store,top_k=15), name="retriever")
67
+ rag_pipeline.add_component(instance=PromptBuilder(template=prompt_template), name="prompt_builder")
68
+ rag_pipeline.add_component(instance=OpenAIGenerator(), name="llm")
69
+ rag_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
70
+ rag_pipeline.connect("retriever", "prompt_builder.documents")
71
+ rag_pipeline.connect("prompt_builder", "llm")
72
+
73
+ except Exception as e:
74
+ print("An error occurred: \n" + error_message)
75
+
76
+
77
+ def get_movies(message, history):
78
+
79
+ try:
80
+ result = rag_pipeline.run(
81
+ {
82
+ "text_embedder": {"text": message},
83
+ "prompt_builder": {"query": message},
84
+ });
85
+ # print(result)
86
+ print_llm_text = result['llm']['replies'][0]
87
+ for i in range(len(print_llm_text)):
88
+ time.sleep(0.03)
89
+ yield print_llm_text[: i+1]
90
+
91
+
92
+ except Exception as e:
93
+ error_message = traceback.format_exc()
94
+ print("An error occurred: \n" + error_message)
95
+ yield "Please clone the repo and add your open ai key as well as your MongoDB Atlas URI 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"
96
+
97
+ # Convert documents to a format suitable for Gradio Dataframe
98
+ data_for_dataframe = [[doc.content] for doc in documents]
99
+ headers = ["Plot"]
100
+
101
+ def update_movie_data(new_data):
102
+ """
103
+ Converts the updated data from the Dataframe back into Document objects.
104
+ """
105
+ print(new_data)
106
+ new_documents = []
107
+
108
+ # Iterate over the DataFrame rows as (index, Series) pairs
109
+ for index, row in new_data.iterrows():
110
+ plot_content = row['Plot'] # Access the 'Plot' column
111
+ print(plot_content)
112
+ new_documents.append(Document(content=plot_content))
113
+
114
+ indexing_pipe.run({"doc_embedder": {"documents": new_documents}})
115
+
116
+ return new_data
117
+ # Setup Gradio interface
118
+ with gr.Blocks() as demo:
119
+ gr.Markdown("## Movie Plot Viewer")
120
+ movie_table = gr.Dataframe(value=data_for_dataframe, headers=headers, interactive=False)
121
+ submit_button = gr.Button("Update Data")
122
+ ## value=[(None, "Hi, I'm a MongoDB and Heystack based question and answer bot 🤖, I can help you answer on the knowledge base above…")]
123
+ gr.ChatInterface(get_movies,examples=["What characters are from Rome?", "Combine 3 plots of your choice", "List all characters"], title="Movies Atlas Vector Search",description="This small chat uses a similarity search to find relevant movies, it uses MongoDB Atlas Vector Search read more here: https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-tutorial",submit_btn="Search").queue()
124
+
125
+
126
+
127
+ submit_button.click(fn=update_movie_data, inputs=[movie_table], outputs=[movie_table])
128
+
129
+ if __name__ == "__main__":
130
+ demo.launch()