|
import os |
|
import keyboard |
|
import time |
|
import requests |
|
os.environ["HUGGINGFACEHUB_API_TOKEN"] = "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" |
|
|
|
from langchain.document_loaders import TextLoader |
|
from langchain.text_splitter import CharacterTextSplitter |
|
from langchain.embeddings import HuggingFaceEmbeddings |
|
|
|
from langchain.vectorstores import FAISS |
|
from langchain.chains.question_answering import load_qa_chain |
|
from langchain import HuggingFaceHub |
|
from langchain.document_loaders import UnstructuredPDFLoader |
|
from langchain.indexes import VectorstoreIndexCreator |
|
from langchain.chains import RetrievalQA |
|
from langchain.document_loaders import UnstructuredURLLoader |
|
import requests |
|
import textwrap |
|
from langchain.document_loaders import TextLoader |
|
|
|
loader = TextLoader('./KS-all-info_rev1.txt') |
|
documents = loader.load() |
|
def wrap_text_preserve_newlines(text, width=110): |
|
|
|
lines = text.split('\n') |
|
|
|
wrapped_lines = [textwrap.fill(line, width=width) for line in lines] |
|
|
|
wrapped_text = '\n'.join(wrapped_lines) |
|
return wrapped_text |
|
text_splitter = CharacterTextSplitter(chunk_size=3000, chunk_overlap=10) |
|
docs = text_splitter.split_documents(documents) |
|
|
|
embeddings = HuggingFaceEmbeddings() |
|
|
|
|
|
db = FAISS.from_documents(docs, embeddings) |
|
llm=HuggingFaceHub(repo_id="MBZUAI/LaMini-Flan-T5-783M", model_kwargs={"temperature":0, "max_length":512}) |
|
chain = load_qa_chain(llm, chain_type="stuff") |
|
def run_chain(query): |
|
result=chain.run(input_documents=docs, question=query) |
|
return result |
|
|
|
|
|
|