Update app.py
#4
by
ChanYeon
- opened
app.py
CHANGED
|
@@ -8,53 +8,51 @@ from langchain.chains import ConversationalRetrievalChain
|
|
| 8 |
from htmlTemplates import css, bot_template, user_template
|
| 9 |
from langchain.llms import LlamaCpp # For loading transformer models.
|
| 10 |
from langchain.document_loaders import PyPDFLoader, TextLoader, JSONLoader, CSVLoader
|
| 11 |
-
import tempfile
|
| 12 |
import os
|
| 13 |
-
from huggingface_hub import hf_hub_download
|
| 14 |
|
| 15 |
-
# PDF λ¬Έμλ‘λΆν° ν
μ€νΈλ₯Ό μΆμΆνλ
|
| 16 |
def get_pdf_text(pdf_docs):
|
| 17 |
-
temp_dir = tempfile.TemporaryDirectory()
|
| 18 |
-
temp_filepath = os.path.join(temp_dir.name, pdf_docs.name)
|
| 19 |
-
with open(temp_filepath, "wb") as f:
|
| 20 |
-
f.write(pdf_docs.getvalue())
|
| 21 |
-
pdf_loader = PyPDFLoader(temp_filepath)
|
| 22 |
-
pdf_doc = pdf_loader.load()
|
| 23 |
-
return pdf_doc
|
| 24 |
-
|
| 25 |
-
# κ³Όμ
|
| 26 |
-
# μλ ν
μ€νΈ μΆμΆ ν¨μλ₯Ό μμ±
|
| 27 |
def get_text_file(docs):
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
def get_csv_file(docs):
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
def get_json_file(docs):
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
# λ¬Έμλ€μ μ²λ¦¬νμ¬ ν
μ€νΈ μ²ν¬λ‘ λλλ ν¨μμ
λλ€.
|
| 38 |
def get_text_chunks(documents):
|
| 39 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 40 |
-
chunk_size=1000,
|
| 41 |
-
chunk_overlap=200,
|
| 42 |
-
length_function=len
|
| 43 |
)
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
return documents # λλ μ²ν¬λ₯Ό λ°νν©λλ€.
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
# ν
μ€νΈ μ²ν¬λ€λ‘λΆν° λ²‘ν° μ€ν μ΄λ₯Ό μμ±νλ ν¨μμ
λλ€.
|
| 50 |
def get_vectorstore(text_chunks):
|
| 51 |
-
# μνλ μλ² λ© λͺ¨λΈμ λ‘λν©λλ€.
|
| 52 |
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L12-v2',
|
| 53 |
-
model_kwargs={'device': 'cpu'})
|
| 54 |
-
vectorstore = FAISS.from_documents(text_chunks, embeddings)
|
| 55 |
-
return vectorstore
|
| 56 |
-
|
| 57 |
|
|
|
|
| 58 |
def get_conversation_chain(vectorstore):
|
| 59 |
model_name_or_path = 'TheBloke/Llama-2-7B-chat-GGUF'
|
| 60 |
model_basename = 'llama-2-7b-chat.Q2_K.gguf'
|
|
@@ -64,23 +62,19 @@ def get_conversation_chain(vectorstore):
|
|
| 64 |
n_ctx=4086,
|
| 65 |
input={"temperature": 0.75, "max_length": 2000, "top_p": 1},
|
| 66 |
verbose=True, )
|
| 67 |
-
# λν κΈ°λ‘μ μ μ₯νκΈ° μν λ©λͺ¨λ¦¬λ₯Ό μμ±ν©λλ€.
|
| 68 |
memory = ConversationBufferMemory(
|
| 69 |
memory_key='chat_history', return_messages=True)
|
| 70 |
-
# λν κ²μ 체μΈμ μμ±ν©λλ€.
|
| 71 |
conversation_chain = ConversationalRetrievalChain.from_llm(
|
| 72 |
llm=llm,
|
| 73 |
retriever=vectorstore.as_retriever(),
|
| 74 |
memory=memory
|
| 75 |
)
|
| 76 |
-
return conversation_chain
|
| 77 |
|
| 78 |
-
# μ¬μ©μ
|
| 79 |
def handle_userinput(user_question):
|
| 80 |
print('user_question => ', user_question)
|
| 81 |
-
# λν 체μΈμ μ¬μ©νμ¬ μ¬μ©μ μ§λ¬Έμ λν μλ΅μ μμ±ν©λλ€.
|
| 82 |
response = st.session_state.conversation({'question': user_question})
|
| 83 |
-
# λν κΈ°λ‘μ μ μ₯ν©λλ€.
|
| 84 |
st.session_state.chat_history = response['chat_history']
|
| 85 |
|
| 86 |
for i, message in enumerate(st.session_state.chat_history):
|
|
@@ -91,7 +85,6 @@ def handle_userinput(user_question):
|
|
| 91 |
st.write(bot_template.replace(
|
| 92 |
"{{MSG}}", message.content), unsafe_allow_html=True)
|
| 93 |
|
| 94 |
-
|
| 95 |
def main():
|
| 96 |
load_dotenv()
|
| 97 |
st.set_page_config(page_title="Chat with multiple Files",
|
|
@@ -142,6 +135,5 @@ def main():
|
|
| 142 |
st.session_state.conversation = get_conversation_chain(
|
| 143 |
vectorstore)
|
| 144 |
|
| 145 |
-
|
| 146 |
if __name__ == '__main__':
|
| 147 |
-
main()
|
|
|
|
| 8 |
from htmlTemplates import css, bot_template, user_template
|
| 9 |
from langchain.llms import LlamaCpp # For loading transformer models.
|
| 10 |
from langchain.document_loaders import PyPDFLoader, TextLoader, JSONLoader, CSVLoader
|
| 11 |
+
import tempfile
|
| 12 |
import os
|
| 13 |
+
from huggingface_hub import hf_hub_download
|
| 14 |
|
| 15 |
+
# PDF λ¬Έμλ‘λΆν° ν
μ€νΈλ₯Ό μΆμΆνλ ν¨μ
|
| 16 |
def get_pdf_text(pdf_docs):
|
| 17 |
+
temp_dir = tempfile.TemporaryDirectory()
|
| 18 |
+
temp_filepath = os.path.join(temp_dir.name, pdf_docs.name)
|
| 19 |
+
with open(temp_filepath, "wb") as f:
|
| 20 |
+
f.write(pdf_docs.getvalue())
|
| 21 |
+
pdf_loader = PyPDFLoader(temp_filepath)
|
| 22 |
+
pdf_doc = pdf_loader.load()
|
| 23 |
+
return pdf_doc
|
| 24 |
+
|
| 25 |
+
# κ³Όμ λΆλΆ
|
|
|
|
| 26 |
def get_text_file(docs):
|
| 27 |
+
text_loader = TextLoader(docs.name)
|
| 28 |
+
return text_loader.load()
|
| 29 |
+
|
| 30 |
def get_csv_file(docs):
|
| 31 |
+
csv_loader = CSVLoader(docs.name)
|
| 32 |
+
return csv_loader.load()
|
| 33 |
|
| 34 |
def get_json_file(docs):
|
| 35 |
+
json_loader = JSONLoader(docs.name)
|
| 36 |
+
return json_loader.load()
|
| 37 |
|
| 38 |
+
# λ¬Έμλ€μ μ²λ¦¬νμ¬ ν
μ€νΈ μ²ν¬λ‘ λλλ ν¨μ
|
|
|
|
| 39 |
def get_text_chunks(documents):
|
| 40 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 41 |
+
chunk_size=1000,
|
| 42 |
+
chunk_overlap=200,
|
| 43 |
+
length_function=len
|
| 44 |
)
|
| 45 |
+
documents = text_splitter.split_documents(documents)
|
| 46 |
+
return documents
|
| 47 |
|
| 48 |
+
# ν
μ€νΈ μ²ν¬λ€λ‘λΆν° λ²‘ν° μ€ν μ΄λ₯Ό μμ±νλ ν¨μ
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
def get_vectorstore(text_chunks):
|
|
|
|
| 50 |
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L12-v2',
|
| 51 |
+
model_kwargs={'device': 'cpu'})
|
| 52 |
+
vectorstore = FAISS.from_documents(text_chunks, embeddings)
|
| 53 |
+
return vectorstore
|
|
|
|
| 54 |
|
| 55 |
+
# λν μ²΄μΈ μ€μ ν¨μ
|
| 56 |
def get_conversation_chain(vectorstore):
|
| 57 |
model_name_or_path = 'TheBloke/Llama-2-7B-chat-GGUF'
|
| 58 |
model_basename = 'llama-2-7b-chat.Q2_K.gguf'
|
|
|
|
| 62 |
n_ctx=4086,
|
| 63 |
input={"temperature": 0.75, "max_length": 2000, "top_p": 1},
|
| 64 |
verbose=True, )
|
|
|
|
| 65 |
memory = ConversationBufferMemory(
|
| 66 |
memory_key='chat_history', return_messages=True)
|
|
|
|
| 67 |
conversation_chain = ConversationalRetrievalChain.from_llm(
|
| 68 |
llm=llm,
|
| 69 |
retriever=vectorstore.as_retriever(),
|
| 70 |
memory=memory
|
| 71 |
)
|
| 72 |
+
return conversation_chain
|
| 73 |
|
| 74 |
+
# μ¬μ©μ μ
λ ₯ μ²λ¦¬ ν¨μ
|
| 75 |
def handle_userinput(user_question):
|
| 76 |
print('user_question => ', user_question)
|
|
|
|
| 77 |
response = st.session_state.conversation({'question': user_question})
|
|
|
|
| 78 |
st.session_state.chat_history = response['chat_history']
|
| 79 |
|
| 80 |
for i, message in enumerate(st.session_state.chat_history):
|
|
|
|
| 85 |
st.write(bot_template.replace(
|
| 86 |
"{{MSG}}", message.content), unsafe_allow_html=True)
|
| 87 |
|
|
|
|
| 88 |
def main():
|
| 89 |
load_dotenv()
|
| 90 |
st.set_page_config(page_title="Chat with multiple Files",
|
|
|
|
| 135 |
st.session_state.conversation = get_conversation_chain(
|
| 136 |
vectorstore)
|
| 137 |
|
|
|
|
| 138 |
if __name__ == '__main__':
|
| 139 |
+
main()
|