Update app.py
Browse files
app.py
CHANGED
@@ -9,7 +9,7 @@ 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
|
13 |
|
14 |
# Load environment variables
|
15 |
load_dotenv()
|
@@ -40,39 +40,37 @@ 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 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
st.error("Failed to create vectorstore after multiple attempts due to rate limits.")
|
62 |
-
return None
|
63 |
|
64 |
# Function to set up the conversational retrieval chain
|
65 |
def get_conversation_chain(vectorstore):
|
66 |
try:
|
67 |
llm = ChatGroq(model="llama-3.1-70b-versatile", temperature=0.5)
|
68 |
memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
|
69 |
-
|
70 |
conversation_chain = ConversationalRetrievalChain.from_llm(
|
71 |
llm=llm,
|
72 |
retriever=vectorstore.as_retriever(),
|
73 |
memory=memory
|
74 |
)
|
75 |
-
|
76 |
logging.info("Conversation chain created successfully.")
|
77 |
return conversation_chain
|
78 |
except Exception as e:
|
@@ -105,6 +103,7 @@ def main():
|
|
105 |
|
106 |
st.header("Chat with multiple PDFs :books:")
|
107 |
user_question = st.text_input("Ask a question about your documents:")
|
|
|
108 |
if user_question:
|
109 |
handle_userinput(user_question)
|
110 |
|
@@ -113,14 +112,16 @@ def main():
|
|
113 |
pdf_docs = st.file_uploader(
|
114 |
"Upload your PDFs here and click on 'Process'", accept_multiple_files=True
|
115 |
)
|
|
|
116 |
if st.button("Process"):
|
117 |
with st.spinner("Processing..."):
|
118 |
raw_text = get_pdf_text(pdf_docs)
|
119 |
text_chunks = get_text_chunks(raw_text)
|
120 |
vectorstore = get_vectorstore(text_chunks)
|
121 |
-
if vectorstore is not None: #
|
122 |
st.session_state.conversation = get_conversation_chain(vectorstore)
|
123 |
|
124 |
if __name__ == '__main__':
|
125 |
main()
|
126 |
|
|
|
|
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
|
15 |
load_dotenv()
|
|
|
40 |
chunks = text_splitter.split_text(text)
|
41 |
return chunks
|
42 |
|
43 |
+
# Function to create a FAISS vectorstore with rate limiting
|
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 |
+
# Rate limiting: Ensure no more than 40 requests per minute
|
49 |
+
max_requests_per_minute = 40
|
50 |
+
wait_time = 60 / max_requests_per_minute
|
51 |
+
|
52 |
+
vectorstore = None
|
53 |
+
try:
|
54 |
+
vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
|
55 |
+
time.sleep(wait_time) # Sleep to avoid hitting API rate limit
|
56 |
+
except Exception as e:
|
57 |
+
logging.error(f"Error creating vectorstore: {e}")
|
58 |
+
st.error("An error occurred while creating the vectorstore.")
|
59 |
+
|
60 |
+
return vectorstore
|
|
|
|
|
61 |
|
62 |
# Function to set up the conversational retrieval chain
|
63 |
def get_conversation_chain(vectorstore):
|
64 |
try:
|
65 |
llm = ChatGroq(model="llama-3.1-70b-versatile", temperature=0.5)
|
66 |
memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
|
67 |
+
|
68 |
conversation_chain = ConversationalRetrievalChain.from_llm(
|
69 |
llm=llm,
|
70 |
retriever=vectorstore.as_retriever(),
|
71 |
memory=memory
|
72 |
)
|
73 |
+
|
74 |
logging.info("Conversation chain created successfully.")
|
75 |
return conversation_chain
|
76 |
except Exception as e:
|
|
|
103 |
|
104 |
st.header("Chat with multiple PDFs :books:")
|
105 |
user_question = st.text_input("Ask a question about your documents:")
|
106 |
+
|
107 |
if user_question:
|
108 |
handle_userinput(user_question)
|
109 |
|
|
|
112 |
pdf_docs = st.file_uploader(
|
113 |
"Upload your PDFs here and click on 'Process'", accept_multiple_files=True
|
114 |
)
|
115 |
+
|
116 |
if st.button("Process"):
|
117 |
with st.spinner("Processing..."):
|
118 |
raw_text = get_pdf_text(pdf_docs)
|
119 |
text_chunks = get_text_chunks(raw_text)
|
120 |
vectorstore = get_vectorstore(text_chunks)
|
121 |
+
if vectorstore is not None: # Ensure vectorstore was created successfully
|
122 |
st.session_state.conversation = get_conversation_chain(vectorstore)
|
123 |
|
124 |
if __name__ == '__main__':
|
125 |
main()
|
126 |
|
127 |
+
|