Datasculptor fffiloni commited on
Commit
90a207f
0 Parent(s):

Duplicate from fffiloni/langchain-chat-with-pdf

Browse files

Co-authored-by: Sylvain Filoni <[email protected]>

Files changed (4) hide show
  1. .gitattributes +34 -0
  2. README.md +13 -0
  3. app.py +77 -0
  4. requirements.txt +5 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Chat with PDF
3
+ emoji: 📄🤖
4
+ colorFrom: purple
5
+ colorTo: pink
6
+ sdk: gradio
7
+ sdk_version: 3.27.0
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: fffiloni/langchain-chat-with-pdf
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from langchain.document_loaders import OnlinePDFLoader
4
+
5
+ from langchain.text_splitter import CharacterTextSplitter
6
+ text_splitter = CharacterTextSplitter(chunk_size=350, chunk_overlap=0)
7
+
8
+ from langchain.llms import HuggingFaceHub
9
+ flan_ul2 = HuggingFaceHub(repo_id="google/flan-ul2", model_kwargs={"temperature":0.1, "max_new_tokens":300})
10
+
11
+ from langchain.embeddings import HuggingFaceHubEmbeddings
12
+ embeddings = HuggingFaceHubEmbeddings()
13
+
14
+ from langchain.vectorstores import Chroma
15
+
16
+ from langchain.chains import RetrievalQA
17
+ def loading_pdf():
18
+ return "Loading..."
19
+ def pdf_changes(pdf_doc):
20
+ loader = OnlinePDFLoader(pdf_doc.name)
21
+ documents = loader.load()
22
+ texts = text_splitter.split_documents(documents)
23
+ db = Chroma.from_documents(texts, embeddings)
24
+ retriever = db.as_retriever()
25
+ global qa
26
+ qa = RetrievalQA.from_chain_type(llm=flan_ul2, chain_type="stuff", retriever=retriever, return_source_documents=True)
27
+ return "Ready"
28
+
29
+ def add_text(history, text):
30
+ history = history + [(text, None)]
31
+ return history, ""
32
+
33
+ def bot(history):
34
+ response = infer(history[-1][0])
35
+ history[-1][1] = response['result']
36
+ return history
37
+
38
+ def infer(question):
39
+
40
+ query = question
41
+ result = qa({"query": query})
42
+
43
+ return result
44
+
45
+ css="""
46
+ #col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
47
+ """
48
+
49
+ title = """
50
+ <div style="text-align: center;max-width: 700px;">
51
+ <h1>Chat with PDF</h1>
52
+ <p style="text-align: center;">Upload a .PDF from your computer, click the "Load PDF to LangChain" button, <br />
53
+ when everything is ready, you can start asking questions about the pdf ;)</p>
54
+ </div>
55
+ """
56
+
57
+
58
+ with gr.Blocks(css=css) as demo:
59
+ with gr.Column(elem_id="col-container"):
60
+ gr.HTML(title)
61
+
62
+ with gr.Column():
63
+ pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="file")
64
+ with gr.Row():
65
+ langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
66
+ load_pdf = gr.Button("Load pdf to langchain")
67
+
68
+ chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
69
+ with gr.Row():
70
+ question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
71
+ load_pdf.click(loading_pdf, None, langchain_status, queue=False)
72
+ load_pdf.click(pdf_changes, pdf_doc, langchain_status, queue=False)
73
+ question.submit(add_text, [chatbot, question], [chatbot, question]).then(
74
+ bot, chatbot, chatbot
75
+ )
76
+
77
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ huggingface_hub
2
+ chromadb
3
+ langchain
4
+ unstructured
5
+ unstructured[local-inference]