Update app.py
Browse files
app.py
CHANGED
@@ -17,7 +17,7 @@ load_dotenv()
|
|
17 |
# Set up logging
|
18 |
logging.basicConfig(
|
19 |
level=logging.INFO,
|
20 |
-
format=
|
21 |
)
|
22 |
|
23 |
# Function to extract text from PDF files
|
@@ -40,38 +40,36 @@ def get_text_chunks(text):
|
|
40 |
chunks = text_splitter.split_text(text)
|
41 |
return chunks
|
42 |
|
43 |
-
# Function to create a FAISS vectorstore with
|
44 |
def get_vectorstore(text_chunks):
|
45 |
cohere_api_key = os.getenv("COHERE_API_KEY")
|
46 |
embeddings = CohereEmbeddings(model="embed-english-v3.0", cohere_api_key=cohere_api_key)
|
47 |
|
48 |
-
|
49 |
-
batch_size =
|
|
|
|
|
50 |
for i in range(0, len(text_chunks), batch_size):
|
51 |
-
batch = text_chunks[i:i+batch_size]
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
time.sleep(2 ** retry_count) # Exponential backoff
|
65 |
-
retry_count += 1
|
66 |
-
else:
|
67 |
-
raise e # Raise other errors
|
68 |
return vectorstore
|
69 |
|
70 |
# Function to set up the conversational retrieval chain
|
71 |
def get_conversation_chain(vectorstore):
|
72 |
try:
|
73 |
llm = ChatGroq(model="llama-3.1-70b-versatile", temperature=0.5)
|
74 |
-
memory = ConversationBufferMemory(memory_key=
|
75 |
|
76 |
conversation_chain = ConversationalRetrievalChain.from_llm(
|
77 |
llm=llm,
|
@@ -88,8 +86,8 @@ def get_conversation_chain(vectorstore):
|
|
88 |
# Handle user input
|
89 |
def handle_userinput(user_question):
|
90 |
if st.session_state.conversation is not None:
|
91 |
-
response = st.session_state.conversation({
|
92 |
-
st.session_state.chat_history = response[
|
93 |
|
94 |
for i, message in enumerate(st.session_state.chat_history):
|
95 |
if i % 2 == 0:
|
@@ -126,5 +124,5 @@ def main():
|
|
126 |
vectorstore = get_vectorstore(text_chunks)
|
127 |
st.session_state.conversation = get_conversation_chain(vectorstore)
|
128 |
|
129 |
-
if __name__ ==
|
130 |
main()
|
|
|
17 |
# Set up logging
|
18 |
logging.basicConfig(
|
19 |
level=logging.INFO,
|
20 |
+
format='%(asctime)s - %(levelname)s - %(message)s'
|
21 |
)
|
22 |
|
23 |
# Function to extract text from PDF files
|
|
|
40 |
chunks = text_splitter.split_text(text)
|
41 |
return chunks
|
42 |
|
43 |
+
# Function to create a FAISS vectorstore with batching
|
44 |
def get_vectorstore(text_chunks):
|
45 |
cohere_api_key = os.getenv("COHERE_API_KEY")
|
46 |
embeddings = CohereEmbeddings(model="embed-english-v3.0", cohere_api_key=cohere_api_key)
|
47 |
|
48 |
+
# Batch processing to respect Cohere's rate limit
|
49 |
+
batch_size = 40
|
50 |
+
all_embeddings = []
|
51 |
+
|
52 |
for i in range(0, len(text_chunks), batch_size):
|
53 |
+
batch = text_chunks[i:i + batch_size]
|
54 |
+
logging.info(f"Processing batch {i // batch_size + 1}: {len(batch)} texts")
|
55 |
+
try:
|
56 |
+
batch_embeddings = embeddings.embed_documents(batch)
|
57 |
+
all_embeddings.extend(batch_embeddings)
|
58 |
+
except Exception as e:
|
59 |
+
logging.error(f"Error embedding batch {i // batch_size + 1}: {e}")
|
60 |
+
st.error(f"An error occurred while embedding batch {i // batch_size + 1}.")
|
61 |
+
if i + batch_size < len(text_chunks): # Enforce delay only if more batches remain
|
62 |
+
logging.info("Waiting for 60 seconds to respect API rate limits...")
|
63 |
+
time.sleep(60) # Wait for 60 seconds
|
64 |
+
|
65 |
+
vectorstore = FAISS.from_texts_with_embeddings(texts=text_chunks, embeddings=all_embeddings)
|
|
|
|
|
|
|
|
|
66 |
return vectorstore
|
67 |
|
68 |
# Function to set up the conversational retrieval chain
|
69 |
def get_conversation_chain(vectorstore):
|
70 |
try:
|
71 |
llm = ChatGroq(model="llama-3.1-70b-versatile", temperature=0.5)
|
72 |
+
memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
|
73 |
|
74 |
conversation_chain = ConversationalRetrievalChain.from_llm(
|
75 |
llm=llm,
|
|
|
86 |
# Handle user input
|
87 |
def handle_userinput(user_question):
|
88 |
if st.session_state.conversation is not None:
|
89 |
+
response = st.session_state.conversation({'question': user_question})
|
90 |
+
st.session_state.chat_history = response['chat_history']
|
91 |
|
92 |
for i, message in enumerate(st.session_state.chat_history):
|
93 |
if i % 2 == 0:
|
|
|
124 |
vectorstore = get_vectorstore(text_chunks)
|
125 |
st.session_state.conversation = get_conversation_chain(vectorstore)
|
126 |
|
127 |
+
if __name__ == '__main__':
|
128 |
main()
|