Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,13 @@ 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_cohere import CohereEmbeddings
|
| 8 |
from langchain.vectorstores import FAISS
|
| 9 |
from langchain.memory import ConversationBufferMemory
|
| 10 |
from langchain.chains import ConversationalRetrievalChain
|
| 11 |
-
from
|
|
|
|
| 12 |
|
| 13 |
# Load environment variables
|
| 14 |
load_dotenv()
|
|
@@ -19,10 +21,6 @@ logging.basicConfig(
|
|
| 19 |
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 20 |
)
|
| 21 |
|
| 22 |
-
# Access API keys
|
| 23 |
-
cohere_api_key = os.getenv("COHERE_API_KEY")
|
| 24 |
-
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 25 |
-
|
| 26 |
# Function to extract text from PDF files
|
| 27 |
def get_pdf_text(pdf_docs):
|
| 28 |
text = ""
|
|
@@ -44,7 +42,13 @@ def get_text_chunks(text):
|
|
| 44 |
return chunks
|
| 45 |
|
| 46 |
# Function to create a FAISS vectorstore
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
def get_vectorstore(text_chunks):
|
|
|
|
| 48 |
embeddings = CohereEmbeddings(model="embed-english-v3.0", cohere_api_key=cohere_api_key)
|
| 49 |
vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
|
| 50 |
return vectorstore
|
|
@@ -52,17 +56,10 @@ def get_vectorstore(text_chunks):
|
|
| 52 |
# Function to set up the conversational retrieval chain
|
| 53 |
def get_conversation_chain(vectorstore):
|
| 54 |
try:
|
| 55 |
-
#
|
| 56 |
-
llm = ChatGroq(
|
| 57 |
-
model="llama2-70b-4096", # You can use other models like "mixtral-8x7b-32768"
|
| 58 |
-
temperature=0.5,
|
| 59 |
-
groq_api_key=groq_api_key
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
# Set up memory for the conversation
|
| 63 |
memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
|
| 64 |
|
| 65 |
-
# Create the conversational retrieval chain
|
| 66 |
conversation_chain = ConversationalRetrievalChain.from_llm(
|
| 67 |
llm=llm,
|
| 68 |
retriever=vectorstore.as_retriever(),
|
|
@@ -91,6 +88,7 @@ def handle_userinput(user_question):
|
|
| 91 |
|
| 92 |
# Main function to run the Streamlit app
|
| 93 |
def main():
|
|
|
|
| 94 |
st.set_page_config(page_title="Chat with multiple PDFs", page_icon=":books:")
|
| 95 |
|
| 96 |
if "conversation" not in st.session_state:
|
|
|
|
| 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
|
| 16 |
load_dotenv()
|
|
|
|
| 21 |
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 22 |
)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# Function to extract text from PDF files
|
| 25 |
def get_pdf_text(pdf_docs):
|
| 26 |
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 |
vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
|
| 54 |
return vectorstore
|
|
|
|
| 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.3-70b-versatile", temperature=0.5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
|
| 62 |
|
|
|
|
| 63 |
conversation_chain = ConversationalRetrievalChain.from_llm(
|
| 64 |
llm=llm,
|
| 65 |
retriever=vectorstore.as_retriever(),
|
|
|
|
| 88 |
|
| 89 |
# Main function to run the Streamlit app
|
| 90 |
def main():
|
| 91 |
+
load_dotenv()
|
| 92 |
st.set_page_config(page_title="Chat with multiple PDFs", page_icon=":books:")
|
| 93 |
|
| 94 |
if "conversation" not in st.session_state:
|