amartyasaran commited on
Commit
5dd5730
1 Parent(s): c2a504f
Files changed (3) hide show
  1. app.py +35 -31
  2. faiss_index +0 -0
  3. faiss_index.json +1 -0
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import json
2
  import streamlit as st
3
- # from haystack.document_stores import FAISSDocumentStore
4
- from haystack.document_stores import InMemoryDocumentStore
5
  from transformers import DPRContextEncoder, DPRContextEncoderTokenizer
6
  from haystack.nodes import DensePassageRetriever
7
  from haystack.nodes import FARMReader
@@ -9,22 +9,24 @@ from haystack.pipelines import ExtractiveQAPipeline
9
 
10
  st.title("DPR on Supreme Court Judgements (Capital Gain)")
11
 
12
- with open("responses.json", 'r') as f:
13
- data = json.load(f)
14
 
15
- documents = [
16
- {
17
- "content": doc["text"],
18
- "meta": {
19
- "name": doc["title"],
20
- "url": doc["url"]
21
- }
22
- } for doc in data
23
- ]
24
 
25
  # document_store = FAISSDocumentStore(embedding_dim=768, faiss_index_factory_str="Flat")
26
- document_store = InMemoryDocumentStore()
27
- document_store.write_documents(documents)
 
 
28
 
29
  retriever = DensePassageRetriever(
30
  document_store=document_store,
@@ -32,28 +34,30 @@ retriever = DensePassageRetriever(
32
  passage_embedding_model="facebook/dpr-ctx_encoder-single-nq-base",
33
  )
34
 
35
- document_store.update_embeddings(retriever)
36
- # document_store.save("faiss_index")
 
 
37
 
38
  reader = FARMReader(model_name_or_path="deepset/bert-base-cased-squad2")
39
 
40
  pipeline = ExtractiveQAPipeline(reader=reader, retriever=retriever)
41
 
42
- query = st.text_input("Enter your query:", "")
43
 
44
- if query:
45
- with st.spinner("Searching..."):
46
- results = pipeline.run(query=query, params={"Retriever": {"top_k": 5}})
47
- st.write("Results:")
48
- for idx, result in enumerate(results["documents"]):
49
- st.write(f"**{idx + 1}. {result.meta['name']}**")
50
- st.write(f"URL: {result.meta['url']}")
51
- st.write(result.content)
52
- st.write("---")
53
 
54
  # query = st.text_input("Enter Question")
55
- # # query = "What is the subject matter of the petition in the Sadanand S. Varde case?"
56
- # result = pipeline.run(query=query, params={"Retriever": {"top_k": 10}, "Reader": {"top_k": 5}})
57
 
58
- # for answer in result['answers']:
59
- # st.markdown(f"=====================\nAnswer: {answer.answer}\nContext: {answer.context}\nScore: {answer.score}")
 
1
  import json
2
  import streamlit as st
3
+ from haystack.document_stores import FAISSDocumentStore
4
+ # from haystack.document_stores import InMemoryDocumentStore
5
  from transformers import DPRContextEncoder, DPRContextEncoderTokenizer
6
  from haystack.nodes import DensePassageRetriever
7
  from haystack.nodes import FARMReader
 
9
 
10
  st.title("DPR on Supreme Court Judgements (Capital Gain)")
11
 
12
+ # with open("responses.json", 'r') as f:
13
+ # data = json.load(f)
14
 
15
+ # documents = [
16
+ # {
17
+ # "content": doc["text"],
18
+ # "meta": {
19
+ # "name": doc["title"],
20
+ # "url": doc["url"]
21
+ # }
22
+ # } for doc in data
23
+ # ]
24
 
25
  # document_store = FAISSDocumentStore(embedding_dim=768, faiss_index_factory_str="Flat")
26
+ # document_store = InMemoryDocumentStore()
27
+ # document_store.write_documents(documents)
28
+
29
+ document_store = FAISSDocumentStore.load("faiss_index")
30
 
31
  retriever = DensePassageRetriever(
32
  document_store=document_store,
 
34
  passage_embedding_model="facebook/dpr-ctx_encoder-single-nq-base",
35
  )
36
 
37
+ # document_store.update_embeddings(retriever)
38
+ # document_store.save("./document_store")
39
+
40
+
41
 
42
  reader = FARMReader(model_name_or_path="deepset/bert-base-cased-squad2")
43
 
44
  pipeline = ExtractiveQAPipeline(reader=reader, retriever=retriever)
45
 
46
+ # query = st.text_input("Enter your query:", "")
47
 
48
+ # # if query:
49
+ # # with st.spinner("Searching..."):
50
+ # # results = pipeline.run(query=query, params={"Retriever": {"top_k": 5}})
51
+ # # st.write("Results:")
52
+ # # for idx, result in enumerate(results["documents"]):
53
+ # # st.write(f"**{idx + 1}. {result.meta['name']}**")
54
+ # # st.write(f"URL: {result.meta['url']}")
55
+ # # st.write(result.content)
56
+ # # st.write("---")
57
 
58
  # query = st.text_input("Enter Question")
59
+ query = "What is the subject matter of the petition in the Sadanand S. Varde case?"
60
+ result = pipeline.run(query=query, params={"Retriever": {"top_k": 10}, "Reader": {"top_k": 5}})
61
 
62
+ for answer in result['answers']:
63
+ st.markdown(f"=====================\nAnswer: {answer.answer}\nContext: {answer.context}\nScore: {answer.score}")
faiss_index ADDED
Binary file (338 kB). View file
 
faiss_index.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"embedding_dim": 768, "faiss_index_factory_str": "Flat"}