Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from langchain.document_loaders import PyPDFLoader
|
| 3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 5 |
+
from langchain.vectorstores import FAISS
|
| 6 |
+
from langchain import HuggingFaceHub
|
| 7 |
+
from langchain.chains import RetrievalQA
|
| 8 |
+
from langchain import HuggingFaceHub
|
| 9 |
+
|
| 10 |
+
import sentence_transformers
|
| 11 |
+
import faiss
|
| 12 |
+
|
| 13 |
+
def loading_pdf():
|
| 14 |
+
return "Loading..."
|
| 15 |
+
|
| 16 |
+
def pdf_changes(pdf_doc, repo_id):
|
| 17 |
+
|
| 18 |
+
loader = PyPDFLoader(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=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 |
+
<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)
|
| 67 |
+
|
| 68 |
+
with gr.Column():
|
| 69 |
+
pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="file")
|
| 70 |
+
with gr.Row():
|
| 71 |
+
langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
|
| 72 |
+
load_pdf = gr.Button("Load pdf to langchain")
|
| 73 |
+
|
| 74 |
+
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
|
| 75 |
+
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
|
| 76 |
+
submit_btn = gr.Button("Send message")
|
| 77 |
+
#load_pdf.click(loading_pdf, None, langchain_status, queue=False)
|
| 78 |
+
load_pdf.click(pdf_changes, inputs=[pdf_doc, repo_id], outputs=[langchain_status], queue=False)
|
| 79 |
+
question.submit(add_text, [chatbot, question], [chatbot, question]).then(
|
| 80 |
+
bot, chatbot, chatbot
|
| 81 |
+
)
|
| 82 |
+
submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(
|
| 83 |
+
bot, chatbot, chatbot
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
demo.launch()
|
| 87 |
+
|