Spaces:
Sleeping
Sleeping
Update app.py
#3
by
Garvitj
- opened
app.py
CHANGED
@@ -1,25 +1,18 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
from langchain.document_loaders import
|
4 |
-
|
5 |
from langchain.text_splitter import CharacterTextSplitter
|
6 |
-
|
7 |
from langchain.llms import HuggingFaceHub
|
8 |
-
|
9 |
from langchain.embeddings import HuggingFaceHubEmbeddings
|
10 |
-
|
11 |
from langchain.vectorstores import Chroma
|
12 |
-
|
13 |
from langchain.chains import RetrievalQA
|
14 |
|
15 |
-
|
16 |
-
|
17 |
def loading_pdf():
|
18 |
return "Loading..."
|
19 |
|
20 |
def pdf_changes(pdf_doc, repo_id):
|
21 |
-
|
22 |
-
loader =
|
23 |
documents = loader.load()
|
24 |
text_splitter = CharacterTextSplitter(chunk_size=300, chunk_overlap=0)
|
25 |
texts = text_splitter.split_documents(documents)
|
@@ -41,13 +34,11 @@ def bot(history):
|
|
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 |
|
@@ -56,11 +47,9 @@ title = """
|
|
56 |
<h1>Chat with PDF</h1>
|
57 |
<p style="text-align: center;">Upload a .PDF from your computer, click the "Load PDF to LangChain" button, <br />
|
58 |
when everything is ready, you can start asking questions about the pdf ;)</p>
|
59 |
-
<a style="display:inline-block; margin-left: 1em" href="https://huggingface.co/spaces/fffiloni/langchain-chat-with-pdf?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space%20to%20skip%20the%20queue-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a>
|
60 |
</div>
|
61 |
"""
|
62 |
|
63 |
-
|
64 |
with gr.Blocks(css=css) as demo:
|
65 |
with gr.Column(elem_id="col-container"):
|
66 |
gr.HTML(title)
|
@@ -75,7 +64,7 @@ with gr.Blocks(css=css) as demo:
|
|
75 |
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
|
76 |
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
|
77 |
submit_btn = gr.Button("Send message")
|
78 |
-
|
79 |
repo_id.change(pdf_changes, inputs=[pdf_doc, repo_id], outputs=[langchain_status], queue=False)
|
80 |
load_pdf.click(pdf_changes, inputs=[pdf_doc, repo_id], outputs=[langchain_status], queue=False)
|
81 |
question.submit(add_text, [chatbot, question], [chatbot, question]).then(
|
@@ -85,4 +74,4 @@ with gr.Blocks(css=css) as demo:
|
|
85 |
bot, chatbot, chatbot
|
86 |
)
|
87 |
|
88 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
from langchain.document_loaders import PyPDFLoader # Change here
|
|
|
4 |
from langchain.text_splitter import CharacterTextSplitter
|
|
|
5 |
from langchain.llms import HuggingFaceHub
|
|
|
6 |
from langchain.embeddings import HuggingFaceHubEmbeddings
|
|
|
7 |
from langchain.vectorstores import Chroma
|
|
|
8 |
from langchain.chains import RetrievalQA
|
9 |
|
|
|
|
|
10 |
def loading_pdf():
|
11 |
return "Loading..."
|
12 |
|
13 |
def pdf_changes(pdf_doc, repo_id):
|
14 |
+
# Use PyPDFLoader to load local PDFs
|
15 |
+
loader = PyPDFLoader(pdf_doc.name)
|
16 |
documents = loader.load()
|
17 |
text_splitter = CharacterTextSplitter(chunk_size=300, chunk_overlap=0)
|
18 |
texts = text_splitter.split_documents(documents)
|
|
|
34 |
return history
|
35 |
|
36 |
def infer(question):
|
|
|
37 |
query = question
|
38 |
result = qa({"query": query})
|
|
|
39 |
return result
|
40 |
|
41 |
+
css = """
|
42 |
#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
|
43 |
"""
|
44 |
|
|
|
47 |
<h1>Chat with PDF</h1>
|
48 |
<p style="text-align: center;">Upload a .PDF from your computer, click the "Load PDF to LangChain" button, <br />
|
49 |
when everything is ready, you can start asking questions about the pdf ;)</p>
|
|
|
50 |
</div>
|
51 |
"""
|
52 |
|
|
|
53 |
with gr.Blocks(css=css) as demo:
|
54 |
with gr.Column(elem_id="col-container"):
|
55 |
gr.HTML(title)
|
|
|
64 |
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
|
65 |
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
|
66 |
submit_btn = gr.Button("Send message")
|
67 |
+
|
68 |
repo_id.change(pdf_changes, inputs=[pdf_doc, repo_id], outputs=[langchain_status], queue=False)
|
69 |
load_pdf.click(pdf_changes, inputs=[pdf_doc, repo_id], outputs=[langchain_status], queue=False)
|
70 |
question.submit(add_text, [chatbot, question], [chatbot, question]).then(
|
|
|
74 |
bot, chatbot, chatbot
|
75 |
)
|
76 |
|
77 |
+
demo.launch()
|