-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboth.py
More file actions
131 lines (113 loc) · 4.91 KB
/
Copy pathboth.py
File metadata and controls
131 lines (113 loc) · 4.91 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import streamlit as st
from dotenv import load_dotenv
from PyPDF2 import PdfReader
from sentence_transformers import SentenceTransformer
from langchain.vectorstores import FAISS
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from transformers import pipeline
# Load environment variables
load_dotenv()
# Function to extract text from PDF files
def extract_pdf_text(pdf_files):
combined_text = ""
for pdf in pdf_files:
try:
reader = PdfReader(pdf)
for page in reader.pages:
text = page.extract_text()
if text:
combined_text += text
except Exception as e:
st.error(f"Error loading PDF: {e}")
return combined_text
# Split text into manageable chunks
def split_text_into_chunks(text, chunk_size=1200, chunk_overlap=100):
chunks = []
start = 0
while start < len(text):
end = start + chunk_size
chunks.append(text[start:end])
start += chunk_size - chunk_overlap
return chunks
# Create a vector store for similarity search using SentenceTransformer
def create_vector_store(text_chunks):
model = SentenceTransformer('hkunlp/instructor-xl')
embeddings = [model.encode(chunk) for chunk in text_chunks]
# Create FAISS vector store from the embeddings
vector_store = FAISS.from_embeddings(embeddings)
return vector_store
# Initialize conversation chain for chat functionality
def initialize_conversation_chain(vector_store):
language_model = ChatOpenAI(model='gpt-3.5-turbo', temperature=0.5)
memory = ConversationBufferMemory(memory_key='conversation_history', return_messages=True)
chain = ConversationalRetrievalChain.from_llm(
llm=language_model,
retriever=vector_store.as_retriever(),
memory=memory
)
return chain
# Process user input for chat
def process_user_input(user_input):
result = st.session_state.conversation({'question': user_input})
st.session_state.history = result['chat_history']
for i, message in enumerate(st.session_state.history):
if i % 2 == 0:
st.markdown(f"**You:** {message.content}")
else:
st.markdown(f"**Bot:** {message.content}")
# Summarization functionality
def generate_summary(input_text, min_len, max_len):
summarizer = pipeline('summarization')
summary = summarizer(input_text, min_length=min_len, max_length=max_len, do_sample=False)
return summary[0]['summary_text']
# Main application function
def main():
# Initialize session state variables
if "conversation" not in st.session_state:
st.session_state.conversation = None
if "history" not in st.session_state:
st.session_state.history = []
# Set up page configuration
st.set_page_config(page_title="PDF Chat & Summarizer", page_icon="📄", layout="wide")
# Sidebar setup for file uploads
st.sidebar.title("Document Manager 🗂️")
pdf_files = st.sidebar.file_uploader("Upload your PDFs", type="pdf", accept_multiple_files=True)
if st.sidebar.button("Process PDFs"):
with st.spinner("Processing PDFs..."):
text_data = extract_pdf_text(pdf_files)
text_chunks = split_text_into_chunks(text_data)
vector_store = create_vector_store(text_chunks)
st.session_state.conversation = initialize_conversation_chain(vector_store)
st.sidebar.success("Processing completed!")
# Main tab layout
st.title("Interactive PDF Chat & Summarization Tool")
tabs = st.tabs(["💬 Chat with PDFs", "📄 Summarize Text"])
# Chat tab
with tabs[0]:
st.header("Engage in Conversation with your PDFs")
if st.session_state.conversation:
user_query = st.text_input("Ask a question based on your uploaded PDFs:")
if user_query:
process_user_input(user_query)
else:
st.info("Please upload PDFs and process them in the sidebar.")
# Summarizer tab
with tabs[1]:
st.header("Summarize Text Instantly")
input_text = st.text_area("Enter text to summarize:", height=250)
# Number input fields for summarization limits
min_words = st.slider("Minimum summary length (words)", min_value=20, max_value=150, value=40, step=10)
max_words = st.slider("Maximum summary length (words)", min_value=50, max_value=300, value=120, step=10)
# Summarize button
if st.button("Generate Summary"):
if input_text:
with st.spinner("Summarizing..."):
summary = generate_summary(input_text, min_words, max_words)
st.success(f"Summary: {summary}")
st.write(f"**Summary Length:** {len(summary.split())} words")
else:
st.warning("Please enter text to summarize.")
if __name__ == '__main__':
main()