|
import gradio as gr |
|
from langchain import HuggingFaceHub |
|
from langchain.chains.question_answering import load_qa_chain |
|
from langchain.document_loaders import PyMuPDFLoader, TextLoader |
|
from langchain.embeddings import HuggingFaceEmbeddings |
|
from langchain.text_splitter import RecursiveCharacterTextSplitter |
|
from langchain.vectorstores import FAISS |
|
from timeit import default_timer as timer |
|
|
|
|
|
loader = TextLoader("rdna3.txt") |
|
documents = loader.load() |
|
|
|
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=50) |
|
chunks = splitter.split_documents(documents) |
|
|
|
embeddings = HuggingFaceEmbeddings() |
|
db = FAISS.from_documents(chunks, embeddings) |
|
|
|
llm = HuggingFaceHub( |
|
repo_id='google/flan-t5-base', |
|
model_kwargs={"temperature": 0, "max_length": 128}) |
|
chain = load_qa_chain(llm, chain_type="stuff") |
|
|
|
def ask(question): |
|
answers = db.similarity_search(question, k=4) |
|
result = chain.run(input_documents=answers, question=question) |
|
return result |
|
|
|
|
|
ask("What is VGPR") |
|
|
|
iface = gr.Interface( |
|
fn=ask, |
|
inputs=gr.Textbox(label="Question", placeholder="What is..."), |
|
outputs=gr.Textbox(label="Answer"), |
|
allow_flagging=False) |
|
|
|
iface.launch(share=True) |
|
|