Srishti Chandra commited on
Commit
b175e3a
1 Parent(s): dc5c1e6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from langchain.document_loaders import OnlinePDFLoader
3
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
4
+ from langchain.embeddings import HuggingFaceHubEmbeddings
5
+ from langchain.vectorstores import FAISS
6
+ from langchain.llms import HuggingFaceHub
7
+ from langchain.chains import RetrievalQA
8
+
9
+
10
+ import sentence_transformers
11
+ import faiss
12
+
13
+ def loading_pdf():
14
+ return "Loading..."
15
+
16
+ def pdf_changes(pdf_doc):
17
+
18
+ loader = OnlinePDFLoader(pdf_doc.name)
19
+ pages = loader.load_and_split()
20
+ text_splitter = RecursiveCharacterTextSplitter(
21
+ chunk_size=1024,
22
+ chunk_overlap=64,
23
+ separators=['\n\n', '\n', '(?=>\. )', ' ', '']
24
+ )
25
+ docs = text_splitter.split_documents(pages)
26
+ embeddings = HuggingFaceHubEmbeddings()
27
+ db = FAISS.from_documents(docs, embeddings)
28
+
29
+ llm=HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":1, "max_length":1000000})
30
+ global qa
31
+ qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=db.as_retriever(search_kwargs={"k": 3}))
32
+ return "Ready"
33
+
34
+ def add_text(history, text):
35
+ history = history + [(text, None)]
36
+ return history, ""
37
+
38
+ def bot(history):
39
+ response = infer(history[-1][0])
40
+ history[-1][1] = response['result']
41
+ return history
42
+
43
+ def infer(question):
44
+
45
+ query = question
46
+ result = qa({"query": query})
47
+
48
+ return result
49
+
50
+ css="""
51
+ #col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
52
+ """
53
+
54
+ title = """
55
+ <div style="text-align: center;max-width: 700px;">
56
+ <h1>Chat with PDF</h1>
57
+ """
58
+
59
+
60
+ with gr.Blocks(css=css) as demo:
61
+ with gr.Column(elem_id="col-container"):
62
+ gr.HTML(title)
63
+
64
+ with gr.Column():
65
+ pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="file")
66
+ with gr.Row():
67
+ langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
68
+ load_pdf = gr.Button("Load pdf to langchain")
69
+
70
+ chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
71
+ question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
72
+ submit_btn = gr.Button("Send message")
73
+ #load_pdf.click(loading_pdf, None, langchain_status, queue=False)
74
+ load_pdf.click(pdf_changes, inputs=[pdf_doc], outputs=[langchain_status], queue=False)
75
+ question.submit(add_text, [chatbot, question], [chatbot, question]).then(
76
+ bot, chatbot, chatbot
77
+ )
78
+ submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(
79
+ bot, chatbot, chatbot
80
+ )
81
+
82
+ demo.launch()