-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
99 lines (83 loc) · 3.76 KB
/
app.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
89
90
91
92
93
94
95
96
97
98
99
import streamlit as st
from PyPDF2 import PdfReader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
from groq import Groq
import os
import pickle
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
@st.cache_resource
def initialize_groq_llm():
# Initialize the Groq language model with API key from environment
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
st.error("API key is missing. Please check your .env file.")
return None
return Groq(api_key=api_key)
def load_vector_store(pdf_file, embeddings, store_name):
# Load existing vector store or create a new one from the uploaded PDF file
if os.path.exists(f"{store_name}.pkl"):
with open(f"{store_name}.pkl", "rb") as f:
return pickle.load(f)
else:
# Read and extract text from the uploaded PDF
pdf_reader = PdfReader(pdf_file)
text = "".join([page.extract_text() for page in pdf_reader.pages])
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200, length_function=len)
chunks = text_splitter.split_text(text=text)
vector_store = FAISS.from_texts(chunks, embedding=embeddings)
with open(f"{store_name}.pkl", "wb") as f:
pickle.dump(vector_store, f)
return vector_store
def main():
llm = initialize_groq_llm()
if llm is None:
return # Exit if the API key is not available
# Ask the user to upload a PDF file
pdf_file = st.file_uploader("Upload your PDF document", type="pdf")
if pdf_file is None:
st.error("Please upload a PDF file to proceed.")
return
# Define only the 1536-dim embedding model
embeddings_models = {
'1536-dim': HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
}
# Load vector stores for the 1536-dim embedding model
vector_stores = {}
for name, embeddings in embeddings_models.items():
vector_stores[name] = load_vector_store(pdf_file, embeddings, f"vector_store_{name}")
# Button to trigger document summarization
if st.button("Summarize"):
responses = {}
for name, vector_store in vector_stores.items():
# Perform similarity search in the vector store to gather relevant content
docs = vector_store.similarity_search(query="Can you create a summary of the document?", k=3)
if not docs:
responses[name] = "No documents found."
continue
# Extract relevant snippets
snippets = " ".join([doc.page_content for doc in docs])
# Create the prompt for Groq model to summarize the document
prompt = f"Can you create a summary of the following document snippets?\n\nDocument Snippets:\n{snippets}"
try:
# Request completion from Groq language model
result = llm.chat.completions.create(
model="llama3-70b-8192",
messages=[
{"role": "system", "content": "Provide a detailed and concise summary of the provided document snippets."},
{"role": "user", "content": prompt}
]
)
response_content = result.choices[0].message.content
except Exception as e:
response_content = "An error occurred while generating the summary."
responses[name] = response_content
# Display the generated summary
st.subheader("Summary:")
for name, response in responses.items():
st.write(response)
if __name__ == '__main__':
main()