Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,9 @@ from langchain.document_loaders import PyPDFLoader
|
|
| 10 |
import os
|
| 11 |
import tempfile
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
def initialize_session_state():
|
| 14 |
if 'history' not in st.session_state:
|
| 15 |
st.session_state['history'] = []
|
|
@@ -50,23 +53,19 @@ def display_chat_history(chain):
|
|
| 50 |
def create_conversational_chain(vector_store):
|
| 51 |
# Create llm
|
| 52 |
llm = LlamaCpp(
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
)
|
| 61 |
|
| 62 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 63 |
|
| 64 |
-
chain = ConversationalRetrievalChain.from_llm(
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
retriever=vector_store.as_retriever(search_kwargs={"k": 2}),
|
| 68 |
-
memory=memory
|
| 69 |
-
)
|
| 70 |
return chain
|
| 71 |
|
| 72 |
def main():
|
|
@@ -77,6 +76,7 @@ def main():
|
|
| 77 |
st.sidebar.title("Document Processing")
|
| 78 |
uploaded_files = st.sidebar.file_uploader("Upload files", accept_multiple_files=True)
|
| 79 |
|
|
|
|
| 80 |
if uploaded_files:
|
| 81 |
text = []
|
| 82 |
for file in uploaded_files:
|
|
@@ -97,18 +97,17 @@ def main():
|
|
| 97 |
text_chunks = text_splitter.split_documents(text)
|
| 98 |
|
| 99 |
# Create embeddings
|
| 100 |
-
embeddings = HuggingFaceEmbeddings(
|
| 101 |
-
|
| 102 |
-
model_kwargs={'device': 'cuda'} # Use GPU for embeddings
|
| 103 |
-
)
|
| 104 |
|
| 105 |
# Create vector store
|
| 106 |
vector_store = FAISS.from_documents(text_chunks, embedding=embeddings)
|
| 107 |
|
| 108 |
# Create the chain object
|
| 109 |
chain = create_conversational_chain(vector_store)
|
|
|
|
| 110 |
|
| 111 |
display_chat_history(chain)
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
| 114 |
-
main()
|
|
|
|
| 10 |
import os
|
| 11 |
import tempfile
|
| 12 |
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
def initialize_session_state():
|
| 17 |
if 'history' not in st.session_state:
|
| 18 |
st.session_state['history'] = []
|
|
|
|
| 53 |
def create_conversational_chain(vector_store):
|
| 54 |
# Create llm
|
| 55 |
llm = LlamaCpp(
|
| 56 |
+
streaming = True,
|
| 57 |
+
model_path="mistral-7b-instruct-v0.1.Q4_K_M.gguf",
|
| 58 |
+
temperature=0.75,
|
| 59 |
+
top_p=1,
|
| 60 |
+
verbose=True,
|
| 61 |
+
n_ctx=4096
|
| 62 |
+
)
|
|
|
|
| 63 |
|
| 64 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 65 |
|
| 66 |
+
chain = ConversationalRetrievalChain.from_llm(llm=llm, chain_type='stuff',
|
| 67 |
+
retriever=vector_store.as_retriever(search_kwargs={"k": 2}),
|
| 68 |
+
memory=memory)
|
|
|
|
|
|
|
|
|
|
| 69 |
return chain
|
| 70 |
|
| 71 |
def main():
|
|
|
|
| 76 |
st.sidebar.title("Document Processing")
|
| 77 |
uploaded_files = st.sidebar.file_uploader("Upload files", accept_multiple_files=True)
|
| 78 |
|
| 79 |
+
|
| 80 |
if uploaded_files:
|
| 81 |
text = []
|
| 82 |
for file in uploaded_files:
|
|
|
|
| 97 |
text_chunks = text_splitter.split_documents(text)
|
| 98 |
|
| 99 |
# Create embeddings
|
| 100 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",
|
| 101 |
+
model_kwargs={'device': 'cpu'})
|
|
|
|
|
|
|
| 102 |
|
| 103 |
# Create vector store
|
| 104 |
vector_store = FAISS.from_documents(text_chunks, embedding=embeddings)
|
| 105 |
|
| 106 |
# Create the chain object
|
| 107 |
chain = create_conversational_chain(vector_store)
|
| 108 |
+
|
| 109 |
|
| 110 |
display_chat_history(chain)
|
| 111 |
|
| 112 |
if __name__ == "__main__":
|
| 113 |
+
main()
|