Spaces:
Runtime error
Runtime error
initial app.py template
Browse files
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="HuggingFaceH4/zephyr-7b-beta", 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()
|