Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,14 @@
|
|
1 |
import os
|
2 |
import logging
|
|
|
3 |
from dotenv import load_dotenv
|
4 |
import streamlit as st
|
5 |
from PyPDF2 import PdfReader
|
6 |
from langchain.text_splitter import CharacterTextSplitter
|
7 |
-
# from langchain.embeddings import HuggingFaceInstructEmbeddings
|
8 |
from langchain_cohere import CohereEmbeddings
|
9 |
from langchain.vectorstores import FAISS
|
10 |
from langchain.memory import ConversationBufferMemory
|
11 |
from langchain.chains import ConversationalRetrievalChain
|
12 |
-
# from langchain.llms import Ollama
|
13 |
from langchain_groq import ChatGroq
|
14 |
|
15 |
# Load environment variables
|
@@ -18,7 +17,7 @@ load_dotenv()
|
|
18 |
# Set up logging
|
19 |
logging.basicConfig(
|
20 |
level=logging.INFO,
|
21 |
-
format=
|
22 |
)
|
23 |
|
24 |
# Function to extract text from PDF files
|
@@ -41,24 +40,38 @@ def get_text_chunks(text):
|
|
41 |
chunks = text_splitter.split_text(text)
|
42 |
return chunks
|
43 |
|
44 |
-
# Function to create a FAISS vectorstore
|
45 |
-
# def get_vectorstore(text_chunks):
|
46 |
-
# embeddings = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-xl")
|
47 |
-
# vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
|
48 |
-
# return vectorstore
|
49 |
-
|
50 |
def get_vectorstore(text_chunks):
|
51 |
cohere_api_key = os.getenv("COHERE_API_KEY")
|
52 |
embeddings = CohereEmbeddings(model="embed-english-v3.0", cohere_api_key=cohere_api_key)
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
return vectorstore
|
55 |
|
56 |
# Function to set up the conversational retrieval chain
|
57 |
def get_conversation_chain(vectorstore):
|
58 |
try:
|
59 |
-
# llm = Ollama(model="llama3.2:1b")
|
60 |
llm = ChatGroq(model="llama-3.1-70b-versatile", temperature=0.5)
|
61 |
-
memory = ConversationBufferMemory(memory_key=
|
62 |
|
63 |
conversation_chain = ConversationalRetrievalChain.from_llm(
|
64 |
llm=llm,
|
@@ -75,8 +88,8 @@ def get_conversation_chain(vectorstore):
|
|
75 |
# Handle user input
|
76 |
def handle_userinput(user_question):
|
77 |
if st.session_state.conversation is not None:
|
78 |
-
response = st.session_state.conversation({
|
79 |
-
st.session_state.chat_history = response[
|
80 |
|
81 |
for i, message in enumerate(st.session_state.chat_history):
|
82 |
if i % 2 == 0:
|
@@ -113,21 +126,5 @@ def main():
|
|
113 |
vectorstore = get_vectorstore(text_chunks)
|
114 |
st.session_state.conversation = get_conversation_chain(vectorstore)
|
115 |
|
116 |
-
if __name__ ==
|
117 |
main()
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
|
|
1 |
import os
|
2 |
import logging
|
3 |
+
import time
|
4 |
from dotenv import load_dotenv
|
5 |
import streamlit as st
|
6 |
from PyPDF2 import PdfReader
|
7 |
from langchain.text_splitter import CharacterTextSplitter
|
|
|
8 |
from langchain_cohere import CohereEmbeddings
|
9 |
from langchain.vectorstores import FAISS
|
10 |
from langchain.memory import ConversationBufferMemory
|
11 |
from langchain.chains import ConversationalRetrievalChain
|
|
|
12 |
from langchain_groq import ChatGroq
|
13 |
|
14 |
# Load environment variables
|
|
|
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 rate-limiting and retry logic
|
|
|
|
|
|
|
|
|
|
|
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 |
+
vectorstore = None
|
49 |
+
batch_size = 10 # Process chunks in batches of 10
|
50 |
+
for i in range(0, len(text_chunks), batch_size):
|
51 |
+
batch = text_chunks[i:i+batch_size]
|
52 |
+
retry_count = 0
|
53 |
+
|
54 |
+
while retry_count < 5: # Retry up to 5 times
|
55 |
+
try:
|
56 |
+
if vectorstore is None:
|
57 |
+
vectorstore = FAISS.from_texts(texts=batch, embedding=embeddings)
|
58 |
+
else:
|
59 |
+
vectorstore.add_texts(batch, embedding=embeddings)
|
60 |
+
break # Exit retry loop if successful
|
61 |
+
except Exception as e:
|
62 |
+
if "rate limit" in str(e).lower():
|
63 |
+
logging.warning(f"Rate limit exceeded. Retrying batch {i//batch_size + 1} in {2 ** retry_count} seconds...")
|
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="chat_history", return_messages=True)
|
75 |
|
76 |
conversation_chain = ConversationalRetrievalChain.from_llm(
|
77 |
llm=llm,
|
|
|
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({"question": user_question})
|
92 |
+
st.session_state.chat_history = response["chat_history"]
|
93 |
|
94 |
for i, message in enumerate(st.session_state.chat_history):
|
95 |
if i % 2 == 0:
|
|
|
126 |
vectorstore = get_vectorstore(text_chunks)
|
127 |
st.session_state.conversation = get_conversation_chain(vectorstore)
|
128 |
|
129 |
+
if __name__ == "__main__":
|
130 |
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|