Wootang01 commited on
Commit
80a4cc0
1 Parent(s): 59b240b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -3
app.py CHANGED
@@ -1,12 +1,51 @@
1
  import gradio as gr
2
- from langchain.text_splitter import CharacterTextSplitter
3
  from langchain.llms import HuggingFaceHub
4
  from langchain.embeddings import HuggingFaceEmbeddings
5
  from langchain.vectorstores import Chroma
6
  from langchain.chains import RetrievalQA
7
  from langchain.document_loaders import PyMuPDFLoader
8
 
9
- with gr.Blocks() as demo:
10
- pdf_file = gr.File(label='file upload', file_type='.pdf')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  demo.launch()
 
1
  import gradio as gr
2
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
3
  from langchain.llms import HuggingFaceHub
4
  from langchain.embeddings import HuggingFaceEmbeddings
5
  from langchain.vectorstores import Chroma
6
  from langchain.chains import RetrievalQA
7
  from langchain.document_loaders import PyMuPDFLoader
8
 
9
+ def load_doc(pdf_doc):
10
+
11
+ loader = PyMuPDFLoader(pdf_doc.name)
12
+ documents = loader.load()
13
+ embedding = HuggingFaceEmbeddings()
14
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
15
+ text = text_splitter.split_documents(documents)
16
+ db = Chroma.from_documents(text, embedding)
17
+ llm = HuggingFaceHub(repo_id="OpenAssistant/oasst-sft-1-pythia-12b", model_kwargs={"temperature": 1.0, "max_length": 256})
18
+ global chain
19
+ chain = RetrievalQA.from_chain_type(llm=llm,chain_type="stuff",retriever=db.as_retriever())
20
+ return 'Document has successfully been loaded'
21
+
22
+ def answer_query(query):
23
+ question = query
24
+ return chain.run(question)
25
+ html = """
26
+ <div style="text-align:center; max width: 700px;">
27
+ <h1>ChatPDF</h1>
28
+ <p> Upload a PDF File, then click on Load PDF File <br>
29
+ Once the document has been loaded you can begin chatting with the PDF =)
30
+ </div>"""
31
+ css = """container{max-width:700px; margin-left:auto; margin-right:auto,padding:20px}"""
32
+ with gr.Blocks(css=css,theme=gr.themes.Monochrome()) as demo:
33
+ gr.HTML(html)
34
+ with gr.Column():
35
+ gr.Markdown('ChatPDF')
36
+ pdf_doc = gr.File(label="Load a pdf",file_types=['.pdf','.docx'],type='file')
37
+ with gr.Row():
38
+ load_pdf = gr.Button('Load pdf file')
39
+ status = gr.Textbox(label="Status",placeholder='',interactive=False)
40
+
41
+
42
+ with gr.Row():
43
+ input = gr.Textbox(label="type in your question")
44
+ output = gr.Textbox(label="output")
45
+ submit_query = gr.Button("submit")
46
+
47
+ load_pdf.click(load_doc,inputs=pdf_doc,outputs=status)
48
+
49
+ submit_query.click(answer_query,input,output)
50
 
51
  demo.launch()