Spaces:
No application file
No application file
File size: 1,496 Bytes
521920d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain import FAISS
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import openai
from langchain.chat_models import ChatOpenAI
from langchain.callbacks import get_openai_callback
from PyPDF2 import PdfReader
def process_text(text):
text_splitter = CharacterTextSplitter(
separator="\n",
chunk_size=1000,
chunk_overlap=200,
length_function=len
)
chunks = text_splitter.split_text(text)
embeddings=HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
knowledgeBase=FAISS.from_texts(chunks,embeddings)
return knowledgeBase
def summarizer(pdf):
if pdf is not None:
pdf_reader=PdfReader(pdf)
text = ""
for page in pdf_reader.pages:
text += page.extract_text() or ""
knowledgeBase = process_text(text)
query="Summarize the content of the uploaded PDF file in 10-15 sentences."
if query:
docs=knowledgeBase.similarity_search(query)
OpenAIModel = "gpt-3.5-turbo-16k"
llm = ChatOpenAI(model=OpenAIModel, temperature=0.7)
chain=load_qa_chain(llm, chain_type='stuff')
with get_openai_callback() as cost:
response=chain.run(input_documents=docs, question=query)
print(cost)
return response
|