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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from PyPDF2 import PdfReader
|
| 4 |
+
from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
|
| 5 |
+
from langchain.embeddings import OpenAIEmbeddings, HuggingFaceInstructEmbeddings
|
| 6 |
+
from langchain.vectorstores import FAISS, Chroma
|
| 7 |
+
from langchain.embeddings import HuggingFaceEmbeddings # General embeddings from HuggingFace models.
|
| 8 |
+
from langchain.chat_models import ChatOpenAI
|
| 9 |
+
from langchain.memory import ConversationBufferMemory
|
| 10 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 11 |
+
from htmlTemplates import css, bot_template, user_template
|
| 12 |
+
from langchain.llms import HuggingFaceHub, LlamaCpp, CTransformers # For loading transformer models.
|
| 13 |
+
from langchain.document_loaders import PyPDFLoader, TextLoader, JSONLoader, CSVLoader
|
| 14 |
+
import tempfile # ์์ ํ์ผ์ ์์ฑํ๊ธฐ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค.
|
| 15 |
+
import os
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# PDF ๋ฌธ์๋ก๋ถํฐ ํ
์คํธ๋ฅผ ์ถ์ถํ๋ ํจ์์
๋๋ค.
|
| 19 |
+
def get_pdf_text(pdf_docs):
|
| 20 |
+
temp_dir = tempfile.TemporaryDirectory() # ์์ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
|
| 21 |
+
temp_filepath = os.path.join(temp_dir.name, pdf_docs.name) # ์์ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์์ฑํฉ๋๋ค.
|
| 22 |
+
with open(temp_filepath, "wb") as f: # ์์ ํ์ผ์ ๋ฐ์ด๋๋ฆฌ ์ฐ๊ธฐ ๋ชจ๋๋ก ์ฝ๋๋ค.
|
| 23 |
+
f.write(pdf_docs.getvalue()) # PDF ๋ฌธ์์ ๋ด์ฉ์ ์์ ํ์ผ์ ์๋๋ค.
|
| 24 |
+
pdf_loader = PyPDFLoader(temp_filepath) # PyPDFLoader๋ฅผ ์ฌ์ฉํด PDF๋ฅผ ๋ก๋ํฉ๋๋ค.
|
| 25 |
+
pdf_doc = pdf_loader.load() # ํ
์คํธ๋ฅผ ์ถ์ถํฉ๋๋ค.
|
| 26 |
+
return pdf_doc # ์ถ์ถํ ํ
์คํธ๋ฅผ ๋ฐํํฉ๋๋ค.
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# ๊ณผ์
|
| 30 |
+
# ์๋ ํ
์คํธ ์ถ์ถ ํจ์๋ฅผ ์์ฑ
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def get_text_file(docs):
|
| 34 |
+
temp_dir = tempfile.TemporaryDirectory() # ์์ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
|
| 35 |
+
temp_filepath = os.path.join(temp_dir.name, docs.name) # ์์ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์์ฑํฉ๋๋ค.
|
| 36 |
+
with open(temp_filepath, "wb") as f: # ์์ ํ์ผ์ ๋ฐ์ด๋๋ฆฌ ์ฐ๊ธฐ ๋ชจ๋๋ก ์ฝ๋๋ค.
|
| 37 |
+
f.write(docs.getvalue()) # ๋ฌธ์์ ๋ด์ฉ์ ์์ ํ์ผ์ ์๋๋ค.
|
| 38 |
+
loader = TextLoader(temp_filepath) # TextLoader๋ฅผ ์ฌ์ฉํด text๋ฅผ ๋ก๋ํฉ๋๋ค.
|
| 39 |
+
doc = loader.load() # ํ
์คํธ๋ฅผ ์ถ์ถํฉ๋๋ค.
|
| 40 |
+
return doc # ์ถ์ถํ ํ
์คํธ๋ฅผ ๋ฐํํฉ๋๋ค.
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def get_csv_file(docs):
|
| 44 |
+
temp_dir = tempfile.TemporaryDirectory() # ์์ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
|
| 45 |
+
temp_filepath = os.path.join(temp_dir.name, docs.name) # ์์ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์์ฑํฉ๋๋ค.
|
| 46 |
+
with open(temp_filepath, "wb") as f: # ์์ ํ์ผ์ ๋ฐ์ด๋๋ฆฌ ์ฐ๊ธฐ ๋ชจ๋๋ก ์ฝ๋๋ค.
|
| 47 |
+
f.write(docs.getvalue()) # ๋ฌธ์์ ๋ด์ฉ์ ์์ ํ์ผ์ ์๋๋ค.
|
| 48 |
+
loader = CSVLoader(temp_filepath) # csvLoader๋ฅผ ์ฌ์ฉํด csv๋ฅผ ๋ก๋ํฉ๋๋ค.
|
| 49 |
+
doc = loader.load() # ํ
์คํธ๋ฅผ ์ถ์ถํฉ๋๋ค.
|
| 50 |
+
return doc # ์ถ์ถํ ํ
์คํธ๋ฅผ ๋ฐํํฉ๋๋ค.
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def get_json_file(docs):
|
| 54 |
+
temp_dir = tempfile.TemporaryDirectory() # ์์ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
|
| 55 |
+
temp_filepath = os.path.join(temp_dir.name, docs.name) # ์์ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์์ฑํฉ๋๋ค.
|
| 56 |
+
with open(temp_filepath, "wb") as f:
|
| 57 |
+
f.write(docs.getvalue())
|
| 58 |
+
f.seek(0)
|
| 59 |
+
json_loader = JSONLoader(f.name,
|
| 60 |
+
jq_schema='.scans[].relationships',
|
| 61 |
+
text_content=False)
|
| 62 |
+
json_doc = json_loader.load()
|
| 63 |
+
|
| 64 |
+
return json_doc
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# ๋ฌธ์๋ค์ ์ฒ๋ฆฌํ์ฌ ํ
์คํธ ์ฒญํฌ๋ก ๋๋๋ ํจ์์
๋๋ค.
|
| 68 |
+
def get_text_chunks(documents):
|
| 69 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 70 |
+
chunk_size=1000, # ์ฒญํฌ์ ํฌ๊ธฐ๋ฅผ ์ง์ ํฉ๋๋ค.
|
| 71 |
+
chunk_overlap=200, # ์ฒญํฌ ์ฌ์ด์ ์ค๋ณต์ ์ง์ ํฉ๋๋ค.
|
| 72 |
+
length_function=len # ํ
์คํธ์ ๊ธธ์ด๋ฅผ ์ธก์ ํ๋ ํจ์๋ฅผ ์ง์ ํฉ๋๋ค.
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
documents = text_splitter.split_documents(documents) # ๋ฌธ์๋ค์ ์ฒญํฌ๋ก ๋๋๋๋ค
|
| 76 |
+
return documents # ๋๋ ์ฒญํฌ๋ฅผ ๋ฐํํฉ๋๋ค.
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# ํ
์คํธ ์ฒญํฌ๋ค๋ก๋ถํฐ ๋ฒกํฐ ์คํ ์ด๋ฅผ ์์ฑํ๋ ํจ์์
๋๋ค.
|
| 80 |
+
def get_vectorstore(text_chunks):
|
| 81 |
+
# OpenAI ์๋ฒ ๋ฉ ๋ชจ๋ธ์ ๋ก๋ํฉ๋๋ค. (Embedding models - Ada v2)
|
| 82 |
+
|
| 83 |
+
embeddings = OpenAIEmbeddings()
|
| 84 |
+
vectorstore = FAISS.from_documents(text_chunks, embeddings) # FAISS ๋ฒกํฐ ์คํ ์ด๋ฅผ ์์ฑํฉ๋๋ค.
|
| 85 |
+
|
| 86 |
+
return vectorstore # ์์ฑ๋ ๋ฒกํฐ ์คํ ์ด๋ฅผ ๋ฐํํฉ๋๋ค.
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def get_conversation_chain(vectorstore):
|
| 90 |
+
gpt_model_name = 'gpt-3.5-turbo'
|
| 91 |
+
llm = ChatOpenAI(model_name=gpt_model_name) # gpt-3.5 ๋ชจ๋ธ ๋ก๋
|
| 92 |
+
|
| 93 |
+
# ๋ํ ๊ธฐ๋ก์ ์ ์ฅํ๊ธฐ ์ํ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
|
| 94 |
+
memory = ConversationBufferMemory(
|
| 95 |
+
memory_key='chat_history', return_messages=True)
|
| 96 |
+
# ๋ํ ๊ฒ์ ์ฒด์ธ์ ์์ฑํฉ๋๋ค.
|
| 97 |
+
conversation_chain = ConversationalRetrievalChain.from_llm(
|
| 98 |
+
llm=llm,
|
| 99 |
+
retriever=vectorstore.as_retriever(),
|
| 100 |
+
memory=memory
|
| 101 |
+
)
|
| 102 |
+
return conversation_chain
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
# ์ฌ์ฉ์ ์
๋ ฅ์ ์ฒ๋ฆฌํ๋ ํจ์์
๋๋ค.
|
| 106 |
+
def handle_userinput(user_question):
|
| 107 |
+
# ๋ํ ์ฒด์ธ์ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์ ์ง๋ฌธ์ ๋ํ ์๋ต์ ์์ฑํฉ๋๋ค.
|
| 108 |
+
response = st.session_state.conversation({'question': user_question})
|
| 109 |
+
# ๋ํ ๊ธฐ๋ก์ ์ ์ฅํฉ๋๋ค.
|
| 110 |
+
st.session_state.chat_history = response['chat_history']
|
| 111 |
+
|
| 112 |
+
for i, message in enumerate(st.session_state.chat_history):
|
| 113 |
+
if i % 2 == 0:
|
| 114 |
+
st.write(user_template.replace(
|
| 115 |
+
"{{MSG}}", message.content), unsafe_allow_html=True)
|
| 116 |
+
else:
|
| 117 |
+
st.write(bot_template.replace(
|
| 118 |
+
"{{MSG}}", message.content), unsafe_allow_html=True)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def main():
|
| 122 |
+
load_dotenv()
|
| 123 |
+
st.set_page_config(page_title="Chat with multiple Files",
|
| 124 |
+
page_icon=":books:")
|
| 125 |
+
st.write(css, unsafe_allow_html=True)
|
| 126 |
+
|
| 127 |
+
if "conversation" not in st.session_state:
|
| 128 |
+
st.session_state.conversation = None
|
| 129 |
+
if "chat_history" not in st.session_state:
|
| 130 |
+
st.session_state.chat_history = None
|
| 131 |
+
|
| 132 |
+
st.header("Chat with multiple Files :")
|
| 133 |
+
user_question = st.text_input("Ask a question about your documents:")
|
| 134 |
+
if user_question:
|
| 135 |
+
handle_userinput(user_question)
|
| 136 |
+
|
| 137 |
+
with st.sidebar:
|
| 138 |
+
openai_key = st.text_input("Paste your OpenAI API key (sk-...)")
|
| 139 |
+
if openai_key:
|
| 140 |
+
os.environ["OPENAI_API_KEY"] = openai_key
|
| 141 |
+
|
| 142 |
+
st.subheader("Your documents")
|
| 143 |
+
docs = st.file_uploader(
|
| 144 |
+
"Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
|
| 145 |
+
if st.button("Process"):
|
| 146 |
+
with st.spinner("Processing"):
|
| 147 |
+
# get pdf text
|
| 148 |
+
doc_list = []
|
| 149 |
+
|
| 150 |
+
for file in docs:
|
| 151 |
+
print('file - type : ', file.type)
|
| 152 |
+
if file.type == 'text/plain':
|
| 153 |
+
# file is .txt
|
| 154 |
+
doc_list.extend(get_text_file(file))
|
| 155 |
+
elif file.type in ['application/octet-stream', 'application/pdf']:
|
| 156 |
+
# file is .pdf
|
| 157 |
+
doc_list.extend(get_pdf_text(file))
|
| 158 |
+
elif file.type == 'text/csv':
|
| 159 |
+
# file is .csv
|
| 160 |
+
doc_list.extend(get_csv_file(file))
|
| 161 |
+
elif file.type == 'application/json':
|
| 162 |
+
# file is .json
|
| 163 |
+
doc_list.extend(get_json_file(file))
|
| 164 |
+
|
| 165 |
+
# get the text chunks
|
| 166 |
+
text_chunks = get_text_chunks(doc_list)
|
| 167 |
+
|
| 168 |
+
# create vector store
|
| 169 |
+
vectorstore = get_vectorstore(text_chunks)
|
| 170 |
+
|
| 171 |
+
# create conversation chain
|
| 172 |
+
st.session_state.conversation = get_conversation_chain(
|
| 173 |
+
vectorstore)
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
if __name__ == '__main__':
|
| 177 |
+
main()
|