imranjamal commited on
Commit
b82b5a1
·
verified ·
1 Parent(s): 55bf78e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import necessary libraries
2
+ import gradio as gr
3
+ from haystack.document_stores import InMemoryDocumentStore
4
+ from haystack.nodes import DensePassageRetriever, FARMReader
5
+ from haystack.pipelines import ExtractiveQAPipeline
6
+
7
+ # 1. Initialize Document Store
8
+ document_store = InMemoryDocumentStore(embedding_dim=768)
9
+
10
+ # 2. Add Documents
11
+ documents = [
12
+ {"content": "Haystack is an open-source NLP framework for search.", "meta": {"source": "Introduction"}},
13
+ {"content": "You can use Hugging Face models in Haystack pipelines.", "meta": {"source": "Hugging Face"}},
14
+ {"content": "The DensePassageRetriever is a key component of Haystack.", "meta": {"source": "Retrievers"}}
15
+ ]
16
+ document_store.write_documents(documents)
17
+
18
+ # 3. Set up Retriever
19
+ retriever = DensePassageRetriever(
20
+ document_store=document_store,
21
+ query_embedding_model="facebook/dpr-question_encoder-single-nq-base",
22
+ passage_embedding_model="facebook/dpr-ctx_encoder-single-nq-base"
23
+ )
24
+ document_store.update_embeddings(retriever)
25
+
26
+ # 4. Set up Reader
27
+ reader = FARMReader(model_name_or_path="deepset/roberta-base-squad2", use_gpu=False)
28
+
29
+ # 5. Create QA Pipeline
30
+ qa_pipeline = ExtractiveQAPipeline(reader=reader, retriever=retriever)
31
+
32
+ # 6. Define Prediction Function
33
+ def ask_question(query):
34
+ results = qa_pipeline.run(query=query, params={"Retriever": {"top_k": 3}, "Reader": {"top_k": 1}})
35
+ if results["answers"]:
36
+ answer = results["answers"][0].answer
37
+ context = results["answers"][0].context
38
+ source = results["answers"][0].meta.get("source", "Unknown Source")
39
+ return f"**Answer:** {answer}\n\n**Context:** {context}\n\n**Source:** {source}"
40
+ else:
41
+ return "No relevant answer found. Please refine your question."
42
+
43
+ # 7. Set up Gradio Interface
44
+ interface = gr.Interface(
45
+ fn=ask_question,
46
+ inputs=gr.Textbox(lines=2, label="Ask a Question"),
47
+ outputs="text",
48
+ title="AI Search with Haystack",
49
+ description="Ask any question about the content in the document set."
50
+ )
51
+
52
+ # 8. Launch Application
53
+ if __name__ == "__main__":
54
+ interface.launch()