|
import gradio as gr |
|
from langchain_community.document_loaders import PyPDFLoader |
|
from langchain_text_splitters import RecursiveCharacterTextSplitter |
|
from langchain_huggingface import HuggingFaceEmbeddings |
|
from langchain.vectorstores import FAISS |
|
from langchain.chains import RetrievalQA |
|
from langchain_groq import ChatGroq |
|
from langchain_core.prompts import PromptTemplate |
|
from langchain_core.output_parsers import StrOutputParser |
|
from langchain_core.runnables import RunnablePassthrough |
|
|
|
|
|
|
|
vector_store = None |
|
|
|
|
|
def index_pdf(pdf): |
|
global vector_store |
|
|
|
|
|
loader = PyPDFLoader(pdf.name) |
|
documents = loader.load() |
|
|
|
|
|
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) |
|
texts = text_splitter.split_documents(documents) |
|
|
|
|
|
embeddings = HuggingFaceEmbeddings(model_name="bert-base-uncased", encode_kwargs={"normalize_embeddings": True}) |
|
|
|
|
|
vector_store = FAISS.from_documents(texts, embeddings) |
|
|
|
return "PDF indexed successfully!" |
|
|
|
|
|
def chatbot_query(query): |
|
if vector_store is None: |
|
return "Please upload and index a PDF first." |
|
|
|
|
|
retriever = vector_store.as_retriever() |
|
qa_chain = RetrievalQA(llm=OpenAI(), retriever=retriever) |
|
|
|
|
|
response = qa_chain.run(query) |
|
|
|
return response |
|
|
|
|
|
with gr.Blocks() as demo: |
|
with gr.Tab("Indexing"): |
|
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"]) |
|
index_button = gr.Button("Index PDF") |
|
index_output = gr.Textbox(label="Indexing Status") |
|
|
|
index_button.click(index_pdf, inputs=pdf_input, outputs=index_output) |
|
|
|
with gr.Tab("Chatbot"): |
|
query_input = gr.Textbox(label="Enter your question") |
|
query_button = gr.Button("Submit") |
|
query_output = gr.Textbox(label="Response") |
|
|
|
query_button.click(chatbot_query, inputs=query_input, outputs=query_output) |
|
|
|
|
|
demo.launch() |
|
|