Spaces:
Sleeping
Sleeping
Commit
·
1b14b9b
1
Parent(s):
400f53b
Adding chat response
Browse files
app.py
CHANGED
|
@@ -25,17 +25,18 @@ prompt = PromptTemplate(template = prompt_template, input_variables = ["context"
|
|
| 25 |
|
| 26 |
chain = prompt | gemini
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
raw_documents = []
|
| 30 |
for path in pdf_path:
|
| 31 |
raw_documents.extend(PyPDFLoader(path).load())
|
|
|
|
| 32 |
text_splitter = CharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
|
| 33 |
documents = text_splitter.split_documents(raw_documents)
|
| 34 |
|
| 35 |
pc = Pinecone(api_key=os.environ["PINECONE_API_KEY"])
|
| 36 |
|
| 37 |
-
index_name = "langchain-test-index"
|
| 38 |
-
|
| 39 |
index = pc.Index(host="https://langchain-test-index-la2n80y.svc.aped-4627-b74a.pinecone.io")
|
| 40 |
|
| 41 |
if index.list():
|
|
@@ -55,6 +56,26 @@ description = "A simple Gradio interface to query PDFs and compare vector databa
|
|
| 55 |
examples = [[["data/amazon-10-k-2024.pdf"], 1000, 100],
|
| 56 |
[["data/goog-10-k-2023.pdf"], 1000, 100]]
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
| 59 |
gr.Markdown(f"# {title}\n{description}")
|
| 60 |
with gr.Row():
|
|
@@ -68,7 +89,22 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
|
| 68 |
with gr.Column():
|
| 69 |
message = gr.Textbox(label="Status", type="text")
|
| 70 |
|
| 71 |
-
submit_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
examples_obj = gr.Examples(examples=examples, inputs=[pdf, chunk_size, chunk_overlap])
|
| 74 |
|
|
|
|
| 25 |
|
| 26 |
chain = prompt | gemini
|
| 27 |
|
| 28 |
+
index_name = "langchain-test-index"
|
| 29 |
+
|
| 30 |
+
def store_embeddings(pdf_path, chunk_size, chunk_overlap):
|
| 31 |
raw_documents = []
|
| 32 |
for path in pdf_path:
|
| 33 |
raw_documents.extend(PyPDFLoader(path).load())
|
| 34 |
+
|
| 35 |
text_splitter = CharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
|
| 36 |
documents = text_splitter.split_documents(raw_documents)
|
| 37 |
|
| 38 |
pc = Pinecone(api_key=os.environ["PINECONE_API_KEY"])
|
| 39 |
|
|
|
|
|
|
|
| 40 |
index = pc.Index(host="https://langchain-test-index-la2n80y.svc.aped-4627-b74a.pinecone.io")
|
| 41 |
|
| 42 |
if index.list():
|
|
|
|
| 56 |
examples = [[["data/amazon-10-k-2024.pdf"], 1000, 100],
|
| 57 |
[["data/goog-10-k-2023.pdf"], 1000, 100]]
|
| 58 |
|
| 59 |
+
def inference(query):
|
| 60 |
+
chroma_db = Chroma(persist_directory="./chroma_db", embedding_function=embeddings)
|
| 61 |
+
chroma_docs = chroma_db.similarity_search(query)
|
| 62 |
+
chroma_answer = chain.invoke({"context":chroma_docs, "question": query}, return_only_outputs=True)
|
| 63 |
+
|
| 64 |
+
faiss_db = FAISS.load_local("./faiss_db", embeddings, allow_dangerous_deserialization=True)
|
| 65 |
+
faiss_docs = faiss_db.similarity_search(query)
|
| 66 |
+
faiss_answer = chain.invoke({"context":faiss_docs, "question": query}, return_only_outputs=True)
|
| 67 |
+
|
| 68 |
+
lance_db = LanceDB(embedding=embeddings, uri="./lance_db")
|
| 69 |
+
lance_docs = lance_db.similarity_search(query)
|
| 70 |
+
lance_answer = chain.invoke({"context":lance_docs, "question": query}, return_only_outputs=True)
|
| 71 |
+
|
| 72 |
+
pinecone_db = PineconeVectorStore(index="langchain-test-index", embedding=embeddings)
|
| 73 |
+
pinecone_docs = pinecone_db.similarity_search(query)
|
| 74 |
+
pinecoce_answer = chain.invoke({"context":pinecone_docs, "question": query}, return_only_outputs=True)
|
| 75 |
+
|
| 76 |
+
return chroma_answer, faiss_answer, lance_answer, pinecoce_answer
|
| 77 |
+
|
| 78 |
+
|
| 79 |
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
| 80 |
gr.Markdown(f"# {title}\n{description}")
|
| 81 |
with gr.Row():
|
|
|
|
| 89 |
with gr.Column():
|
| 90 |
message = gr.Textbox(label="Status", type="text")
|
| 91 |
|
| 92 |
+
submit_btn.click(store_embeddings, inputs=[pdf, chunk_size, chunk_overlap], outputs=message)
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
with gr.Column():
|
| 96 |
+
text = gr.Textbox(label="Question", type="text")
|
| 97 |
+
with gr.Row():
|
| 98 |
+
chat_clear_btn = gr.ClearButton(components=[text])
|
| 99 |
+
chat_submit_btn = gr.Button("Submit", variant='primary')
|
| 100 |
+
with gr.Column():
|
| 101 |
+
chroma_out = gr.Textbox(label="ChromaDB Response", type="text")
|
| 102 |
+
faiss_out = gr.Textbox(label="FaissDB Response", type="text")
|
| 103 |
+
lance_out = gr.Textbox(label="LanceDB Response", type="text")
|
| 104 |
+
pinecone_out = gr.Textbox(label="PineconeDB Response", type="text")
|
| 105 |
+
|
| 106 |
+
chat_submit_btn.click(inference, inputs=[text], outputs=[chroma_out, faiss_out, lance_out,
|
| 107 |
+
pinecone_out])
|
| 108 |
|
| 109 |
examples_obj = gr.Examples(examples=examples, inputs=[pdf, chunk_size, chunk_overlap])
|
| 110 |
|