-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
88 lines (62 loc) · 3.08 KB
/
utils.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import streamlit as st
import re
from collections import Counter
# import the pypdf2 library
from PyPDF2 import PdfReader
from open_ai_key import OPENAI_API_KEY
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain import OpenAI
from langchain.document_loaders import PyPDFLoader
from langchain.vectorstores import FAISS
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.chains.question_answering import load_qa_chain
from stopword_helper import get_stopwords
st.cache()
def get_basic_stats(text):
# i want to create a dictionary to store the basic statistics of the pdf file
basic_stats = {}
# i want to count the number of words in the pdf file
basic_stats['Number of words'] = len(text.split())
# i want to count the number of characters in the pdf file
basic_stats['Number of characters'] = len(text)
# i want to count the number of sentences in the pdf file
basic_stats['Number of sentences'] = len(text.split('.'))
return basic_stats
st.cache()
def extract_keywords(text):
text = re.sub(r'\s+', ' ', text) # Remove extra spaces
words = re.findall(r'\w+', text.lower()) # Extract words
stopwords_set = get_stopwords('english ')
stopwords = stopwords_set # stop words from nltk
keywords = [word for word in words if word not in stopwords] # Remove stop words
return Counter(keywords).most_common(10) # Get top 10 keywords with their frequencies
st.cache()
def extract_text_from_pdf(pdf_path):
"""
Extracts text from a PDF file and returns it as a string.
Parameters:
pdf_path (str): The file path of the PDF to be read.
Returns:
str: The extracted text from the PDF.
"""
pdf_reader = PdfReader(pdf_path)
text = ""
for page in pdf_reader.pages:
text += page.extract_text()
return text
st.cache()
def embed_text(text):
"""Split the text and embed it in a FAISS vector store"""
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=800, chunk_overlap=0, separators=["\n\n", ".", "?", "!", " ", ""])
texts = text_splitter.split_text(text)
embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)
index = FAISS.from_texts(texts, embeddings)
return index
st.cache()
def get_answer(index, query):
"""Returns answer to a query using langchain QA chain"""
docs = index.similarity_search(query)
chain = load_qa_chain(OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY))
answer = chain.run(input_documents=docs, question=query)
return answer