Pash1986 commited on
Commit
1a64d83
1 Parent(s): e624303

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -24
app.py CHANGED
@@ -3,7 +3,6 @@ import traceback
3
  import gradio as gr
4
  import os
5
  import asyncio
6
- import requests
7
  from haystack_integrations.document_stores.mongodb_atlas import MongoDBAtlasDocumentStore
8
  from haystack import Pipeline, Document
9
  from haystack.components.generators import OpenAIGenerator
@@ -14,7 +13,7 @@ from haystack_integrations.components.retrievers.mongodb_atlas import MongoDBAtl
14
  from haystack.components.embedders import OpenAITextEmbedder, OpenAIDocumentEmbedder
15
  from haystack.document_stores.types import DuplicatePolicy
16
  from haystack.components.writers import DocumentWriter
17
-
18
  import json
19
 
20
 
@@ -88,9 +87,9 @@ try:
88
  rag_pipeline.connect("retriever", "prompt_builder.documents")
89
  rag_pipeline.connect("prompt_builder", "llm")
90
 
91
- # Exception handling to catch and display errors during the pipeline execution.
92
- except Exception as e:
93
- print("An error occurred: \n" + error_message)
94
 
95
 
96
  def get_movies(message, history):
@@ -115,15 +114,7 @@ def get_movies(message, history):
115
 
116
  # Convert documents to a format suitable for Gradio Dataframe
117
  data_for_dataframe = [[doc.content] for doc in documents]
118
- headers = ["Plot
119
-
120
- def fetch_url_data(url):
121
- try:
122
- response = requests.get(url)
123
- response.raise_for_status() # Raises an HTTPError if the HTTP request returned an unsuccessful status code
124
- return response.text
125
- except requests.RequestException as e:
126
- return f"Error: {e}"
127
 
128
  def update_movie_data(new_data):
129
  """
@@ -141,20 +132,31 @@ def update_movie_data(new_data):
141
  indexing_pipe.run({"doc_embedder": {"documents": new_documents}})
142
 
143
  return new_data
 
 
 
 
 
 
 
 
 
144
  # Setup Gradio interface
145
  with gr.Blocks() as demo:
146
- with gr.Tab("Chat"):
147
- gr.Markdown("## Movie Plot Viewer")
148
- movie_table = gr.Dataframe(value=data_for_dataframe, headers=headers, interactive=False)
149
- submit_button = gr.Button("Update Data")
150
- ## value=[(None, "Hi, I'm a MongoDB and Heystack based question and answer bot 🤖, I can help you answer on the knowledge base above…")]
151
- gr.ChatInterface(get_movies,examples=["What characters are from Rome?", "Combine 3 plots of your choice", "List all characters"], title="Atlas Vector Search Chat",description="This small chat uses a similarity search to find relevant plots as listed above, it uses MongoDB Atlas and Haystack integaration: https://haystack.deepset.ai/integrations/mongodb",submit_btn="Search").queue()
152
-
153
-
154
-
155
- submit_button.click(fn=update_movie_data, inputs=[movie_table], outputs=[movie_table])
156
  with gr.Tab("Code"):
157
  gr.Code(label="Code", language="python", value=fetch_url_data('https://huggingface.co/spaces/MongoDB/Haystack-MongoDB-Integration-Chat/raw/main/app.py'))
 
158
 
 
 
 
159
  if __name__ == "__main__":
160
  demo.launch()
 
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
 
13
  from haystack.components.embedders import OpenAITextEmbedder, OpenAIDocumentEmbedder
14
  from haystack.document_stores.types import DuplicatePolicy
15
  from haystack.components.writers import DocumentWriter
16
+ import requests
17
  import json
18
 
19
 
 
87
  rag_pipeline.connect("retriever", "prompt_builder.documents")
88
  rag_pipeline.connect("prompt_builder", "llm")
89
 
90
+ # Exception handling to catch and display errors during the pipeline execution.
91
+ except Exception as e:
92
+ print("An error occurred: \n" + error_message)
93
 
94
 
95
  def get_movies(message, history):
 
114
 
115
  # Convert documents to a format suitable for Gradio Dataframe
116
  data_for_dataframe = [[doc.content] for doc in documents]
117
+ headers = ["Plot"]
 
 
 
 
 
 
 
 
118
 
119
  def update_movie_data(new_data):
120
  """
 
132
  indexing_pipe.run({"doc_embedder": {"documents": new_documents}})
133
 
134
  return new_data
135
+
136
+ def fetch_url_data(url):
137
+ try:
138
+ response = requests.get(url)
139
+ response.raise_for_status() # Raises an HTTPError if the HTTP request returned an unsuccessful status code
140
+ return response.text
141
+ except requests.RequestException as e:
142
+ return f"Error: {e}"
143
+
144
  # Setup Gradio interface
145
  with gr.Blocks() as demo:
146
+ with gr.Tab("Demo"):
147
+ gr.Markdown("## Movie Plot Viewer")
148
+ movie_table = gr.Dataframe(value=data_for_dataframe, headers=headers, interactive=False)
149
+ submit_button = gr.Button("Update Data")
150
+ ## value=[(None, "Hi, I'm a MongoDB and Heystack based question and answer bot 🤖, I can help you answer on the knowledge base above…")]
151
+ gr.ChatInterface(get_movies,examples=["What characters are from Rome?", "Combine 3 plots of your choice", "List all characters"], title="Atlas Vector Search Chat",description="This small chat uses a similarity search to find relevant plots as listed above, it uses MongoDB Atlas and Haystack integaration: https://haystack.deepset.ai/integrations/mongodb",submit_btn="Search").queue()
152
+
153
+ submit_button.click(fn=update_movie_data, inputs=[movie_table], outputs=[movie_table])
 
 
154
  with gr.Tab("Code"):
155
  gr.Code(label="Code", language="python", value=fetch_url_data('https://huggingface.co/spaces/MongoDB/Haystack-MongoDB-Integration-Chat/raw/main/app.py'))
156
+
157
 
158
+
159
+
160
+
161
  if __name__ == "__main__":
162
  demo.launch()