Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| from pinecone import Pinecone | |
| from langchain_chroma import Chroma | |
| from langchain_core.prompts import PromptTemplate | |
| from langchain_pinecone import PineconeVectorStore | |
| from langchain_community.vectorstores import FAISS | |
| from langchain_community.vectorstores import LanceDB | |
| from langchain_text_splitters import CharacterTextSplitter | |
| from langchain_community.document_loaders import PyPDFLoader | |
| from langchain_google_genai import GoogleGenerativeAIEmbeddings, GoogleGenerativeAI | |
| embeddings = GoogleGenerativeAIEmbeddings(model="models/text-embedding-004") | |
| gemini = GoogleGenerativeAI(model="models/gemini-2.0-flash") | |
| prompt_template = """ | |
| Context:\n {context}?\n | |
| Question: \n{question}\n | |
| Answer: | |
| """ | |
| prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"]) | |
| chain = prompt | gemini | |
| def inference(pdf_path, chunk_size, chunk_overlap): | |
| raw_documents = PyPDFLoader(pdf_path).load() | |
| text_splitter = CharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap) | |
| documents = text_splitter.split_documents(raw_documents) | |
| pc = Pinecone(api_key=os.environ["PINECONE_API_KEY"]) | |
| index_name = "langchain-test-index" | |
| index = pc.Index(host="https://langchain-test-index-la2n80y.svc.aped-4627-b74a.pinecone.io") | |
| index.delete(delete_all=True) | |
| chroma_db = Chroma.from_documents(documents, embeddings, persist_directory="./chroma_db") | |
| faiss_db = FAISS.from_documents(documents, embeddings) | |
| faiss_db.save_local("./faiss_db") | |
| lance_db = LanceDB.from_documents(documents, embeddings, uri="./lance_db") | |
| pinecone_db = PineconeVectorStore.from_documents(documents, index_name=index_name, | |
| embedding=embeddings) | |
| return "All embeddings are stored in vector database" | |
| title = "PDF Chat" | |
| description = "A simple Gradio interface to query PDFs and compare vector database" | |
| examples = [["data/amazon-10-k-2024.pdf", 1000, 100], | |
| ["data/goog-10-k-2023.pdf", 1000, 100]] | |
| with gr.Blocks(theme=gr.themes.Monochrome()) as demo: | |
| gr.Markdown(f"# {title}\n{description}") | |
| with gr.Row(): | |
| with gr.Column(): | |
| pdf = gr.UploadButton(file_types=[".pdf"]) | |
| chunk_size = gr.Slider(0, 2000, 1000, 100, label="Size of Chunk") | |
| chunk_overlap = gr.Slider(0, 1000, 100, 100, label="Size of Chunk Overlap") | |
| with gr.Row(): | |
| clear_btn = gr.ClearButton(components=[pdf, chunk_size, chunk_overlap]) | |
| submit_btn = gr.Button("Store Embeddings", variant='primary') | |
| with gr.Column(): | |
| message = gr.Textbox(label="Status", type="text") | |
| submit_btn.click(inference, inputs=[pdf, chunk_size, chunk_overlap], outputs=message) | |
| examples_obj = gr.Examples(examples=examples, inputs=[pdf, chunk_size, chunk_overlap]) | |
| demo.launch() | |