DrishtiSharma commited on
Commit
c320ec9
Β·
verified Β·
1 Parent(s): fadbcd4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -34
app.py CHANGED
@@ -5,6 +5,7 @@ from langchain_chroma import Chroma
5
  from langchain_groq import ChatGroq
6
  from langchain.memory import ConversationBufferMemory
7
  from langchain.chains import ConversationalRetrievalChain
 
8
 
9
  # Ensure required environment variables are set
10
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
@@ -12,19 +13,17 @@ if not GROQ_API_KEY:
12
  st.error("GROQ_API_KEY is not set. Please configure it in Hugging Face Spaces secrets.")
13
  st.stop()
14
 
15
- # Function to set up the vectorstore
16
- def setup_vectorstore():
17
- working_dir = os.path.dirname(os.path.abspath(__file__))
18
- persist_directory = f"{working_dir}/vector_db_dir"
 
 
 
19
 
20
- # Initialize HuggingFace Embeddings
21
  embeddings = HuggingFaceEmbeddings()
22
-
23
- # Initialize Chroma vectorstore
24
- vectorstore = Chroma(
25
- persist_directory=persist_directory,
26
- embedding_function=embeddings
27
- )
28
  return vectorstore
29
 
30
  # Function to set up the chat chain
@@ -32,7 +31,6 @@ def chat_chain(vectorstore):
32
  llm = ChatGroq(model="llama-3.1-70b-versatile",
33
  temperature=0,
34
  groq_api_key=GROQ_API_KEY)
35
-
36
  retriever = vectorstore.as_retriever()
37
  memory = ConversationBufferMemory(
38
  llm=llm,
@@ -60,15 +58,20 @@ st.set_page_config(
60
 
61
  st.title("πŸ“š Multi Documents Chatbot")
62
 
63
- # Initialize session state variables
64
- if "chat_history" not in st.session_state:
65
- st.session_state.chat_history = []
66
 
67
- if "vectorstore" not in st.session_state:
68
- st.session_state.vectorstore = setup_vectorstore()
 
 
 
 
 
69
 
70
- if "conversational_chain" not in st.session_state:
71
- st.session_state.conversational_chain = chat_chain(st.session_state.vectorstore)
 
72
 
73
  # Display chat history
74
  for message in st.session_state.chat_history:
@@ -76,18 +79,20 @@ for message in st.session_state.chat_history:
76
  st.markdown(message["content"])
77
 
78
  # User input
79
- user_input = st.chat_input("Ask AI...")
80
-
81
- if user_input:
82
- st.session_state.chat_history.append({"role": "user", "content": user_input})
83
-
84
- with st.chat_message("user"):
85
- st.markdown(user_input)
86
-
87
- with st.chat_message("assistant"):
88
- # Generate response
89
- response = st.session_state.conversational_chain({"question": user_input})
90
- assistant_response = response["answer"]
91
-
92
- st.markdown(assistant_response)
93
- st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
 
 
 
5
  from langchain_groq import ChatGroq
6
  from langchain.memory import ConversationBufferMemory
7
  from langchain.chains import ConversationalRetrievalChain
8
+ from PyPDF2 import PdfReader
9
 
10
  # Ensure required environment variables are set
11
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
 
13
  st.error("GROQ_API_KEY is not set. Please configure it in Hugging Face Spaces secrets.")
14
  st.stop()
15
 
16
+ # Function to process PDFs and set up the vectorstore
17
+ def process_and_store_pdfs(uploaded_files):
18
+ texts = []
19
+ for uploaded_file in uploaded_files:
20
+ reader = PdfReader(uploaded_file)
21
+ for page in reader.pages:
22
+ texts.append(page.extract_text())
23
 
24
+ # Combine and embed the texts
25
  embeddings = HuggingFaceEmbeddings()
26
+ vectorstore = Chroma.from_texts(texts, embedding=embeddings)
 
 
 
 
 
27
  return vectorstore
28
 
29
  # Function to set up the chat chain
 
31
  llm = ChatGroq(model="llama-3.1-70b-versatile",
32
  temperature=0,
33
  groq_api_key=GROQ_API_KEY)
 
34
  retriever = vectorstore.as_retriever()
35
  memory = ConversationBufferMemory(
36
  llm=llm,
 
58
 
59
  st.title("πŸ“š Multi Documents Chatbot")
60
 
61
+ # File uploader for PDFs
62
+ uploaded_files = st.file_uploader("Upload PDF files", accept_multiple_files=True, type=["pdf"])
 
63
 
64
+ # Process PDFs and initialize the vectorstore
65
+ if uploaded_files:
66
+ with st.spinner("Processing files..."):
67
+ vectorstore = process_and_store_pdfs(uploaded_files)
68
+ st.session_state.vectorstore = vectorstore
69
+ st.session_state.conversational_chain = chat_chain(vectorstore)
70
+ st.success("Files successfully processed! You can now chat with your documents.")
71
 
72
+ # Initialize chat history
73
+ if "chat_history" not in st.session_state:
74
+ st.session_state.chat_history = []
75
 
76
  # Display chat history
77
  for message in st.session_state.chat_history:
 
79
  st.markdown(message["content"])
80
 
81
  # User input
82
+ if "conversational_chain" in st.session_state:
83
+ user_input = st.chat_input("Ask AI...")
84
+ if user_input:
85
+ st.session_state.chat_history.append({"role": "user", "content": user_input})
86
+
87
+ with st.chat_message("user"):
88
+ st.markdown(user_input)
89
+
90
+ with st.chat_message("assistant"):
91
+ # Generate response
92
+ response = st.session_state.conversational_chain({"question": user_input})
93
+ assistant_response = response["answer"]
94
+
95
+ st.markdown(assistant_response)
96
+ st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
97
+ else:
98
+ st.info("Please upload PDF files to start chatting.")