Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,6 @@ llm = HuggingFaceEndpoint(
|
|
| 18 |
|
| 19 |
|
| 20 |
def web_load(path):
|
| 21 |
-
|
| 22 |
loader = WebBaseLoader(
|
| 23 |
web_paths=(path,),
|
| 24 |
# bs_kwargs=dict(
|
|
@@ -33,11 +32,9 @@ def web_load(path):
|
|
| 33 |
|
| 34 |
|
| 35 |
def pdf_load(path):
|
| 36 |
-
|
| 37 |
loader = PyPDFLoader(path)
|
| 38 |
pages = loader.load_and_split()
|
| 39 |
|
| 40 |
-
|
| 41 |
return pages
|
| 42 |
|
| 43 |
|
|
@@ -50,14 +47,10 @@ def vector_store(path):
|
|
| 50 |
splits = text_splitter.split_documents(docs)
|
| 51 |
vectorstore = FAISS.from_documents(documents=splits, embedding=HuggingFaceEmbeddings(model_name='BAAI/bge-base-en-v1.5'))
|
| 52 |
|
| 53 |
-
|
| 54 |
return vectorstore, "Done setup! You may proceed to Chatbot. "
|
| 55 |
|
| 56 |
|
| 57 |
def invoke(user_input, retriever):
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
prompt_template = """
|
| 62 |
<|system|>
|
| 63 |
Answer the question based on your knowledge. Use the following context to help:
|
|
@@ -91,7 +84,6 @@ def invoke(user_input, retriever):
|
|
| 91 |
|
| 92 |
|
| 93 |
def rag_chatbot(vectorstore, user_input, chat_history):
|
| 94 |
-
|
| 95 |
retriever = vectorstore.as_retriever()
|
| 96 |
answer = invoke(user_input, retriever)
|
| 97 |
chat_history.append((user_input, answer))
|
|
@@ -99,13 +91,6 @@ def rag_chatbot(vectorstore, user_input, chat_history):
|
|
| 99 |
return "", chat_history
|
| 100 |
|
| 101 |
|
| 102 |
-
def selection (radio):
|
| 103 |
-
if radio == "website":
|
| 104 |
-
return gr.Textbox(label="Website", value = "https://lilianweng.github.io/posts/2023-06-23-agent/")
|
| 105 |
-
elif radio == "PDF":
|
| 106 |
-
return gr.Files(label="Upload a PDF file", file_count="single", file_types=["pdf"])
|
| 107 |
-
|
| 108 |
-
|
| 109 |
def source (radio, source1, source2):
|
| 110 |
if radio == "website":
|
| 111 |
return source1
|
|
@@ -118,14 +103,14 @@ with gr.Blocks() as demo:
|
|
| 118 |
vectorstore = gr.State()
|
| 119 |
with gr.Tab("Setup"):
|
| 120 |
radio = gr.Radio(["website", "PDF"], label="Select type of source", interactive=True)
|
| 121 |
-
path = gr.Textbox(
|
| 122 |
with gr.Row():
|
| 123 |
source1 = gr.Textbox(label="Input website",)
|
| 124 |
source2 = gr.Files(label="Upload a PDF file", file_count="single", file_types=["pdf"])
|
| 125 |
radio.change(fn=source, inputs=[radio,source1,source2], outputs=path)
|
| 126 |
source1.change(fn=source, inputs=[radio,source1,source2], outputs=path)
|
| 127 |
source2.change(fn=source, inputs=[radio,source1,source2], outputs=path)
|
| 128 |
-
done = gr.Textbox(label="Progress")
|
| 129 |
setup_btn = gr.Button("Initialize vectorstore")
|
| 130 |
setup_btn.click(fn=vector_store, inputs=[path], outputs=[vectorstore, done])
|
| 131 |
with gr.Tab("Chatbot"):
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
def web_load(path):
|
|
|
|
| 21 |
loader = WebBaseLoader(
|
| 22 |
web_paths=(path,),
|
| 23 |
# bs_kwargs=dict(
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
def pdf_load(path):
|
|
|
|
| 35 |
loader = PyPDFLoader(path)
|
| 36 |
pages = loader.load_and_split()
|
| 37 |
|
|
|
|
| 38 |
return pages
|
| 39 |
|
| 40 |
|
|
|
|
| 47 |
splits = text_splitter.split_documents(docs)
|
| 48 |
vectorstore = FAISS.from_documents(documents=splits, embedding=HuggingFaceEmbeddings(model_name='BAAI/bge-base-en-v1.5'))
|
| 49 |
|
|
|
|
| 50 |
return vectorstore, "Done setup! You may proceed to Chatbot. "
|
| 51 |
|
| 52 |
|
| 53 |
def invoke(user_input, retriever):
|
|
|
|
|
|
|
|
|
|
| 54 |
prompt_template = """
|
| 55 |
<|system|>
|
| 56 |
Answer the question based on your knowledge. Use the following context to help:
|
|
|
|
| 84 |
|
| 85 |
|
| 86 |
def rag_chatbot(vectorstore, user_input, chat_history):
|
|
|
|
| 87 |
retriever = vectorstore.as_retriever()
|
| 88 |
answer = invoke(user_input, retriever)
|
| 89 |
chat_history.append((user_input, answer))
|
|
|
|
| 91 |
return "", chat_history
|
| 92 |
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
def source (radio, source1, source2):
|
| 95 |
if radio == "website":
|
| 96 |
return source1
|
|
|
|
| 103 |
vectorstore = gr.State()
|
| 104 |
with gr.Tab("Setup"):
|
| 105 |
radio = gr.Radio(["website", "PDF"], label="Select type of source", interactive=True)
|
| 106 |
+
path = gr.Textbox(label="Path of source", visible=True, interactive=False)
|
| 107 |
with gr.Row():
|
| 108 |
source1 = gr.Textbox(label="Input website",)
|
| 109 |
source2 = gr.Files(label="Upload a PDF file", file_count="single", file_types=["pdf"])
|
| 110 |
radio.change(fn=source, inputs=[radio,source1,source2], outputs=path)
|
| 111 |
source1.change(fn=source, inputs=[radio,source1,source2], outputs=path)
|
| 112 |
source2.change(fn=source, inputs=[radio,source1,source2], outputs=path)
|
| 113 |
+
done = gr.Textbox(label="Progress", interactive=False)
|
| 114 |
setup_btn = gr.Button("Initialize vectorstore")
|
| 115 |
setup_btn.click(fn=vector_store, inputs=[path], outputs=[vectorstore, done])
|
| 116 |
with gr.Tab("Chatbot"):
|