|
import os |
|
import random |
|
import gradio as gr |
|
from langchain_community.document_loaders import PyPDFLoader |
|
from langchain_text_splitters import RecursiveCharacterTextSplitter |
|
from langchain_huggingface import HuggingFaceEmbeddings |
|
from langchain_community.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 |
|
|
|
|
|
sample_filename = "Attention Is All You Need.pdf" |
|
|
|
examples_questions = [["What is Transformer?"], |
|
["What is Attention?"], |
|
["What is Scaled Dot-Product Attention?"], |
|
["What are Encoder and Decoder?"], |
|
["Describe more about the Transformer."], |
|
["Why use self-attention?"], |
|
] |
|
|
|
template = \ |
|
"""Use the following pieces of context to answer the question at the end. |
|
If you don't know the answer, just say that you don't know, don't try to make up an answer. |
|
Always say "Thanks for asking!" at the end of the answer. |
|
|
|
{context} |
|
|
|
Question: {question} |
|
|
|
Answer: |
|
""" |
|
|
|
|
|
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 load_sample_pdf(): |
|
global vector_store |
|
|
|
|
|
loader = PyPDFLoader(sample_filename) |
|
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 "Sample PDF indexed successfully!" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks(theme="Nymbo/Alyx_Theme") as demo: |
|
with gr.Tab("Indexing"): |
|
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"]) |
|
index_button = gr.Button("Index PDF") |
|
load_sample = gr.Button("Alternatively, Load and Index [Attention Is All You Need.pdf] as a Sample") |
|
sample_description = gr.Markdown("This sample PDF is a seminal paper in the field of machine learning, titled 'Attention Is All You Need' at https://arxiv.org/abs/1706.03762. It introduces the Transformer model, which has become foundational in natural language processing.") |
|
index_output = gr.Textbox(label="Indexing Status") |
|
index_button.click(index_pdf, inputs=pdf_input, outputs=index_output) |
|
load_sample.click(load_sample_pdf, inputs=None, outputs=index_output) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
demo.launch() |
|
|