Spaces:
Sleeping
Sleeping
Update app.py
Browse files10 unmodified lines11121314151617181920212223241 unmodified line262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576772 unmodified lines808182838485868788899091929394959697989910 unmodified linesimport nltknltk.download('punkt')import ossecret = os.getenv('Groq_api')working_dir = os.path.dirname(os.path.abspath(__file__))def load_documents(file_path):loader = UnstructuredPDFLoader(file_path)documents = loader.load()return documents1 unmodified lineembeddings = HuggingFaceEmbeddings()text_splitter = CharacterTextSplitter(separator="/n",chunk_size = 1000,chunk_overlap = 200)doc_chunks = text_splitter.split_documents(documents)vectorstores = FAISS.from_documents(doc_chunks,embeddings)return vectorstoresdef create_chain(vectorstores):llm = ChatGroq(api_key = secret,model="llama-3.3-70b-versatile",temperature=0)retriever = vectorstores.as_retriever()memory = ConversationBufferMemory(llm = llm,output_key= "answer",memory_key = "chat_history",return_messages=True)chain = ConversationalRetrievalChain.from_llm(llm = llm,retriever = retriever,memory = memory,verbose = True)return chainst.set_page_config(page_title= "Chat with your documents",page_icon= "๐",layout="centered")st.title("๐Chat With your docs ๐")if "chat_history" not in st.session_state:st.session_state.chat_history = []uploaded_file = st.file_uploader(label="Upload your PDF")if uploaded_file:file_path = f"{working_dir}{uploaded_file.name}"with open(file_path,"wb") as f:f.write(uploaded_file.getbuffer())if "vectorstores" not in st.session_state:2 unmodified linesif "conversation_chain" not in st.session_state:st.session_state.conversation_chain = create_chain(st.session_state.vectorstores)for message in st.session_state.chat_history:with st.chat_message(message["role"]):st.markdown(message["content"])user_input = st.chat_input("Ask any questions relevant to uploaded pdf")if user_input:st.session_state.chat_history.append({"role":"user","content":user_input})with st.chat_message("user"):st.markdown(user_input)with st.chat_message("assistant"):response = st.session_state.conversation_chain({"question":user_input})assistant_response = response["answer"]st.markdown(assistant_response)st.session_state.chat_history.append({"role":"assistant","content":assistant_response})10 unmodified lines111213141516171819202122232425261 unmodified line282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778792 unmodified lines82838485868788899091929394959697989910010110210 unmodified linesimport nltknltk.download('punkt')# Install Poppler in the runtime environmentos.system("apt-get update && apt-get install -y poppler-utils")secret = os.getenv('Groq_api')working_dir = os.path.dirname(os.path.abspath(__file__))def load_documents(file_path):# Specify poppler_path to ensure compatibility in Spacesloader = UnstructuredPDFLoader(file_path, poppler_path="/usr/bin")documents = loader.load()return documents1 unmodified lineembeddings = HuggingFaceEmbeddings()text_splitter = CharacterTextSplitter(separator="/n",chunk_size=1000,chunk_overlap=200)doc_chunks = text_splitter.split_documents(documents)vectorstores = FAISS.from_documents(doc_chunks, embeddings)return vectorstoresdef create_chain(vectorstores):llm = ChatGroq(api_key=secret,model="llama-3.3-70b-versatile",temperature=0)retriever = vectorstores.as_retriever()memory = ConversationBufferMemory(llm=llm,output_key="answer",memory_key="chat_history",return_messages=True)chain = ConversationalRetrievalChain.from_llm(llm=llm,retriever=retriever,memory=memory,verbose=True)return chain# Streamlit page configurationst.set_page_config(page_title="Chat with your documents",page_icon="๐",layout="centered")st.title("๐Chat With your docs ๐")# Initialize session statesif "chat_history" not in st.session_state:st.session_state.chat_history = []uploaded_file = st.file_uploader(label="Upload your PDF")if uploaded_file:file_path = f"{working_dir}/{uploaded_file.name}"with open(file_path, "wb") as f:f.write(uploaded_file.getbuffer())if "vectorstores" not in st.session_state:2 unmodified linesif "conversation_chain" not in st.session_state:st.session_state.conversation_chain = create_chain(st.session_state.vectorstores)# Display chat historyfor message in st.session_state.chat_history:with st.chat_message(message["role"]):st.markdown(message["content"])# User input handlinguser_input = st.chat_input("Ask any questions relevant to uploaded pdf")if user_input:st.session_state.chat_history.append({"role": "user", "content": user_input})with st.chat_message("user"):st.markdown(user_input)with st.chat_message("assistant"):response = st.session_state.conversation_chain({"question": user_input})assistant_response = response["answer"]st.markdown(assistant_response)st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
app.py+26-23