Delete app.py
Browse files
app.py
DELETED
@@ -1,171 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from dotenv import load_dotenv
|
3 |
-
from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
|
4 |
-
from langchain.vectorstores import FAISS
|
5 |
-
from langchain.embeddings import HuggingFaceEmbeddings # General embeddings from HuggingFace models.
|
6 |
-
from langchain.memory import ConversationBufferMemory
|
7 |
-
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 # Hugging Face Hub์์ ๋ชจ๋ธ์ ๋ค์ด๋ก๋ํ๊ธฐ ์ํ ํจ์์
๋๋ค.
|
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()) # PDF ๋ฌธ์์ ๋ด์ฉ์ ์์ ํ์ผ์ ์๋๋ค.
|
21 |
-
pdf_loader = PyPDFLoader(temp_filepath) # PyPDFLoader๋ฅผ ์ฌ์ฉํด PDF๋ฅผ ๋ก๋ํฉ๋๋ค.
|
22 |
-
pdf_doc = pdf_loader.load() # ํ
์คํธ๋ฅผ ์ถ์ถํฉ๋๋ค.
|
23 |
-
return pdf_doc # ์ถ์ถํ ํ
์คํธ๋ฅผ ๋ฐํํฉ๋๋ค.
|
24 |
-
|
25 |
-
# ๊ณผ์
|
26 |
-
# ์๋ ํ
์คํธ ์ถ์ถ ํจ์๋ฅผ ์์ฑ
|
27 |
-
def get_text_file(text_docs):
|
28 |
-
text_data = []
|
29 |
-
for doc in docs:
|
30 |
-
if doc.lower().endswith('.txt'):
|
31 |
-
with open(doc, 'r', encoding='utf-8') as file:
|
32 |
-
text = file.read()
|
33 |
-
text_data.append(text)
|
34 |
-
return text_data
|
35 |
-
|
36 |
-
def get_csv_file(csv_docs):
|
37 |
-
csv_data = []
|
38 |
-
for doc in docs:
|
39 |
-
if doc.lower().endswith('.csv'):
|
40 |
-
with open(doc, 'r', newline='', encoding='utf-8') as file:
|
41 |
-
csv_reader = csv.reader(file)
|
42 |
-
data = [row for row in csv_reader]
|
43 |
-
csv_data.append(data)
|
44 |
-
return csv_data
|
45 |
-
|
46 |
-
def get_json_file(json_docs):
|
47 |
-
json_data = []
|
48 |
-
for doc in docs:
|
49 |
-
if doc.lower().endswith('.json'):
|
50 |
-
with open(doc, 'r', encoding='utf-8') as file:
|
51 |
-
data = json.load(file)
|
52 |
-
json_data.append(data)
|
53 |
-
return json_data
|
54 |
-
|
55 |
-
|
56 |
-
# ๋ฌธ์๋ค์ ์ฒ๋ฆฌํ์ฌ ํ
์คํธ ์ฒญํฌ๋ก ๋๋๋ ํจ์์
๋๋ค.
|
57 |
-
def get_text_chunks(documents):
|
58 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
59 |
-
chunk_size=1000, # ์ฒญํฌ์ ํฌ๊ธฐ๋ฅผ ์ง์ ํฉ๋๋ค.
|
60 |
-
chunk_overlap=200, # ์ฒญํฌ ์ฌ์ด์ ์ค๋ณต์ ์ง์ ํฉ๋๋ค.
|
61 |
-
length_function=len # ํ
์คํธ์ ๊ธธ์ด๋ฅผ ์ธก์ ํ๋ ํจ์๋ฅผ ์ง์ ํฉ๋๋ค.
|
62 |
-
)
|
63 |
-
|
64 |
-
documents = text_splitter.split_documents(documents) # ๋ฌธ์๋ค์ ์ฒญํฌ๋ก ๋๋๋๋ค.
|
65 |
-
return documents # ๋๋ ์ฒญํฌ๋ฅผ ๋ฐํํฉ๋๋ค.
|
66 |
-
|
67 |
-
|
68 |
-
# ํ
์คํธ ์ฒญํฌ๋ค๋ก๋ถํฐ ๋ฒกํฐ ์คํ ์ด๋ฅผ ์์ฑํ๋ ํจ์์
๋๋ค.
|
69 |
-
def get_vectorstore(text_chunks):
|
70 |
-
# ์ํ๋ ์๋ฒ ๋ฉ ๋ชจ๋ธ์ ๋ก๋ํฉ๋๋ค.
|
71 |
-
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L12-v2',
|
72 |
-
model_kwargs={'device': 'cpu'}) # ์๋ฒ ๋ฉ ๋ชจ๋ธ์ ์ค์ ํฉ๋๋ค.
|
73 |
-
vectorstore = FAISS.from_documents(text_chunks, embeddings) # FAISS ๋ฒกํฐ ์คํ ์ด๋ฅผ ์์ฑํฉ๋๋ค.
|
74 |
-
return vectorstore # ์์ฑ๋ ๋ฒกํฐ ์คํ ์ด๋ฅผ ๋ฐํํฉ๋๋ค.
|
75 |
-
|
76 |
-
|
77 |
-
def get_conversation_chain(vectorstore):
|
78 |
-
model_name_or_path = 'TheBloke/Llama-2-7B-chat-GGUF'
|
79 |
-
model_basename = 'llama-2-7b-chat.Q2_K.gguf'
|
80 |
-
model_path = hf_hub_download(repo_id=model_name_or_path, filename=model_basename)
|
81 |
-
|
82 |
-
llm = LlamaCpp(model_path=model_path,
|
83 |
-
n_ctx=4086,
|
84 |
-
input={"temperature": 0.75, "max_length": 2000, "top_p": 1},
|
85 |
-
verbose=True, )
|
86 |
-
# ๋ํ ๊ธฐ๋ก์ ์ ์ฅํ๊ธฐ ์ํ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
|
87 |
-
memory = ConversationBufferMemory(
|
88 |
-
memory_key='chat_history', return_messages=True)
|
89 |
-
# ๋ํ ๊ฒ์ ์ฒด์ธ์ ์์ฑํฉ๋๋ค.
|
90 |
-
conversation_chain = ConversationalRetrievalChain.from_llm(
|
91 |
-
llm=llm,
|
92 |
-
retriever=vectorstore.as_retriever(),
|
93 |
-
memory=memory
|
94 |
-
)
|
95 |
-
return conversation_chain # ์์ฑ๋ ๋ํ ์ฒด์ธ์ ๋ฐํํฉ๋๋ค.
|
96 |
-
|
97 |
-
# ์ฌ์ฉ์ ์
๋ ฅ์ ์ฒ๋ฆฌํ๋ ํจ์์
๋๋ค.
|
98 |
-
def handle_userinput(user_question):
|
99 |
-
#print('user_question => ', user_question)
|
100 |
-
|
101 |
-
# ๋ํ ์ฒด์ธ์ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์ ์ง๋ฌธ์ ๋ํ ์๋ต์ ์์ฑํฉ๋๋ค.
|
102 |
-
st.session_state.conversation = {'question': user_question}
|
103 |
-
|
104 |
-
#st.session_state.chat_history = response['chat_history']
|
105 |
-
#response = st.session_state.conversation
|
106 |
-
|
107 |
-
if 'chat_history' in st.session_state.conversation:
|
108 |
-
st.session_state.chat_history = st.session_state.conversation['chat_history']
|
109 |
-
for i, message in enumerate(st.session_state.chat_history):
|
110 |
-
if i % 2 == 0:
|
111 |
-
st.write(user_template.replace("{{MSG}}", message.content), unsafe_allow_html=True)
|
112 |
-
else:
|
113 |
-
st.write(bot_template.replace("{{MSG}}", message.content), unsafe_allow_html=True)
|
114 |
-
else:
|
115 |
-
# chat_history๊ฐ ์์ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ ๋๋ ๊ธฐ๋ณธ๊ฐ ์ค์
|
116 |
-
st.session_state.chat_history = [] # ์์๋ก ๋น ๋ฆฌ์คํธ๋ฅผ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ค์
|
117 |
-
st.warning("Chat history not found or empty.")
|
118 |
-
|
119 |
-
def main():
|
120 |
-
load_dotenv()
|
121 |
-
st.set_page_config(page_title="Chat with multiple Files",
|
122 |
-
page_icon=":books:")
|
123 |
-
st.write(css, unsafe_allow_html=True)
|
124 |
-
|
125 |
-
if "conversation" not in st.session_state:
|
126 |
-
st.session_state.conversation = None
|
127 |
-
if "chat_history" not in st.session_state:
|
128 |
-
st.session_state.chat_history = None
|
129 |
-
|
130 |
-
st.header("Chat with multiple Files:")
|
131 |
-
user_question = st.text_input("Ask a question about your documents:")
|
132 |
-
if user_question:
|
133 |
-
handle_userinput(user_question)
|
134 |
-
|
135 |
-
with st.sidebar:
|
136 |
-
st.subheader("Your documents")
|
137 |
-
docs = st.file_uploader(
|
138 |
-
"Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
|
139 |
-
if st.button("Process"):
|
140 |
-
with st.spinner("Processing"):
|
141 |
-
# get pdf text
|
142 |
-
doc_list = []
|
143 |
-
|
144 |
-
for file in docs:
|
145 |
-
print('file - type : ', file.type)
|
146 |
-
if file.type == 'text/plain':
|
147 |
-
# file is .txt
|
148 |
-
doc_list.extend(get_text_file(file))
|
149 |
-
elif file.type in ['application/octet-stream', 'application/pdf']:
|
150 |
-
# file is .pdf
|
151 |
-
doc_list.extend(get_pdf_text(file))
|
152 |
-
elif file.type == 'text/csv':
|
153 |
-
# file is .csv
|
154 |
-
doc_list.extend(get_csv_file(file))
|
155 |
-
elif file.type == 'application/json':
|
156 |
-
# file is .json
|
157 |
-
doc_list.extend(get_json_file(file))
|
158 |
-
|
159 |
-
# get the text chunks
|
160 |
-
text_chunks = get_text_chunks(doc_list)
|
161 |
-
|
162 |
-
# create vector store
|
163 |
-
vectorstore = get_vectorstore(text_chunks)
|
164 |
-
|
165 |
-
# create conversation chain
|
166 |
-
st.session_state.conversation = get_conversation_chain(
|
167 |
-
vectorstore)
|
168 |
-
|
169 |
-
|
170 |
-
if __name__ == '__main__':
|
171 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|