Update app.py
Browse files
app.py
CHANGED
@@ -29,54 +29,42 @@ def get_pdf_text(pdf_docs):
|
|
29 |
# μλ ν
μ€νΈ μΆμΆ ν¨μλ₯Ό μμ±
|
30 |
|
31 |
def get_text_file(docs):
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
37 |
|
38 |
def get_csv_file(docs):
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
else:
|
46 |
-
st.warning("Unsupported file type for get_csv_file")
|
47 |
|
48 |
def get_json_file(docs):
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
else:
|
56 |
-
st.warning("Unsupported file type for get_json_file")
|
57 |
|
58 |
|
59 |
# λ¬Έμλ€μ μ²λ¦¬νμ¬ ν
μ€νΈ μ²ν¬λ‘ λλλ ν¨μμ
λλ€.
|
60 |
def get_text_chunks(documents):
|
61 |
text_splitter = RecursiveCharacterTextSplitter(
|
62 |
-
chunk_size=1000,
|
63 |
-
chunk_overlap=200,
|
64 |
-
length_function=len
|
65 |
)
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
for doc in documents:
|
70 |
-
if hasattr(doc, 'page_content'):
|
71 |
-
# λ¬Έμ κ°μ²΄μΈ κ²½μ°μλ§ μΆκ°
|
72 |
-
texts.append(doc.page_content)
|
73 |
-
elif isinstance(doc, str):
|
74 |
-
# λ¬Έμμ΄μΈ κ²½μ° κ·Έλλ‘ μΆκ°
|
75 |
-
texts.append(doc)
|
76 |
-
|
77 |
-
# λλ μ²ν¬λ₯Ό λ°ν
|
78 |
-
return text_splitter.split_documents(texts)
|
79 |
-
|
80 |
|
81 |
|
82 |
# ν
μ€νΈ μ²ν¬λ€λ‘λΆν° λ²‘ν° μ€ν μ΄λ₯Ό μμ±νλ ν¨μμ
λλ€.
|
@@ -90,30 +78,19 @@ def get_vectorstore(text_chunks):
|
|
90 |
|
91 |
|
92 |
def get_conversation_chain(vectorstore):
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
retriever=vectorstore.as_retriever(),
|
107 |
-
memory=memory
|
108 |
-
)
|
109 |
-
st.session_state.conversation = conversation_chain
|
110 |
-
|
111 |
-
except Exception as e:
|
112 |
-
print(f"Error during conversation initialization: {e}")
|
113 |
-
|
114 |
-
print(f"DEBUG: session_state.conversation after initialization: {st.session_state.conversation}")
|
115 |
-
|
116 |
-
return st.session_state.conversation if st.session_state.conversation else ConversationalRetrievalChain()
|
117 |
|
118 |
# μ¬μ©μ μ
λ ₯μ μ²λ¦¬νλ ν¨μμ
λλ€.
|
119 |
def handle_userinput(user_question):
|
@@ -133,12 +110,13 @@ def handle_userinput(user_question):
|
|
133 |
|
134 |
def main():
|
135 |
load_dotenv()
|
136 |
-
st.set_page_config(page_title="Chat with multiple Files
|
137 |
page_icon=":books:")
|
138 |
st.write(css, unsafe_allow_html=True)
|
139 |
|
140 |
-
if "conversation" not in st.session_state
|
141 |
st.session_state.conversation = None
|
|
|
142 |
st.session_state.chat_history = None
|
143 |
|
144 |
st.header("Chat with multiple Files :")
|
@@ -153,35 +131,36 @@ def main():
|
|
153 |
|
154 |
st.subheader("Your documents")
|
155 |
docs = st.file_uploader(
|
156 |
-
"Upload your
|
157 |
if st.button("Process"):
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
|
|
184 |
|
185 |
|
186 |
if __name__ == '__main__':
|
187 |
-
main()
|
|
|
29 |
# μλ ν
μ€νΈ μΆμΆ ν¨μλ₯Ό μμ±
|
30 |
|
31 |
def get_text_file(docs):
|
32 |
+
text_list = []
|
33 |
+
for file in docs:
|
34 |
+
if file.type == 'text/plain':
|
35 |
+
# file is .txt
|
36 |
+
text_list.append(file.getvalue().decode('utf-8'))
|
37 |
+
return text_list
|
38 |
+
|
39 |
+
|
40 |
|
41 |
def get_csv_file(docs):
|
42 |
+
csv_list = []
|
43 |
+
for file in docs:
|
44 |
+
if file.type == 'text/csv':
|
45 |
+
# file is .csv
|
46 |
+
csv_list.extend(csv.reader(file.getvalue().decode('utf-8').splitlines()))
|
47 |
+
return csv_list
|
|
|
|
|
48 |
|
49 |
def get_json_file(docs):
|
50 |
+
json_list = []
|
51 |
+
for file in docs:
|
52 |
+
if file.type == 'application/json':
|
53 |
+
# file is .json
|
54 |
+
json_list.extend(json.load(file))
|
55 |
+
return json_list
|
|
|
|
|
56 |
|
57 |
|
58 |
# λ¬Έμλ€μ μ²λ¦¬νμ¬ ν
μ€νΈ μ²ν¬λ‘ λλλ ν¨μμ
λλ€.
|
59 |
def get_text_chunks(documents):
|
60 |
text_splitter = RecursiveCharacterTextSplitter(
|
61 |
+
chunk_size=1000, # μ²ν¬μ ν¬κΈ°λ₯Ό μ§μ ν©λλ€.
|
62 |
+
chunk_overlap=200, # μ²ν¬ μ¬μ΄μ μ€λ³΅μ μ§μ ν©λλ€.
|
63 |
+
length_function=len # ν
μ€νΈμ κΈΈμ΄λ₯Ό μΈ‘μ νλ ν¨μλ₯Ό μ§μ ν©λλ€.
|
64 |
)
|
65 |
|
66 |
+
documents = text_splitter.split_documents(documents) # λ¬Έμλ€μ μ²ν¬λ‘ λλλλ€
|
67 |
+
return documents # λλ μ²ν¬λ₯Ό λ°νν©λλ€.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
|
70 |
# ν
μ€νΈ μ²ν¬λ€λ‘λΆν° λ²‘ν° μ€ν μ΄λ₯Ό μμ±νλ ν¨μμ
λλ€.
|
|
|
78 |
|
79 |
|
80 |
def get_conversation_chain(vectorstore):
|
81 |
+
gpt_model_name = 'gpt-3.5-turbo'
|
82 |
+
llm = ChatOpenAI(model_name = gpt_model_name) #gpt-3.5 λͺ¨λΈ λ‘λ
|
83 |
+
|
84 |
+
# λν κΈ°λ‘μ μ μ₯νκΈ° μν λ©λͺ¨λ¦¬λ₯Ό μμ±ν©λλ€.
|
85 |
+
memory = ConversationBufferMemory(
|
86 |
+
memory_key='chat_history', return_messages=True)
|
87 |
+
# λν κ²μ 체μΈμ μμ±ν©λλ€.
|
88 |
+
conversation_chain = ConversationalRetrievalChain.from_llm(
|
89 |
+
llm=llm,
|
90 |
+
retriever=vectorstore.as_retriever(),
|
91 |
+
memory=memory
|
92 |
+
)
|
93 |
+
return conversation_chain
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
# μ¬μ©μ μ
λ ₯μ μ²λ¦¬νλ ν¨μμ
λλ€.
|
96 |
def handle_userinput(user_question):
|
|
|
110 |
|
111 |
def main():
|
112 |
load_dotenv()
|
113 |
+
st.set_page_config(page_title="Chat with multiple Files",
|
114 |
page_icon=":books:")
|
115 |
st.write(css, unsafe_allow_html=True)
|
116 |
|
117 |
+
if "conversation" not in st.session_state:
|
118 |
st.session_state.conversation = None
|
119 |
+
if "chat_history" not in st.session_state:
|
120 |
st.session_state.chat_history = None
|
121 |
|
122 |
st.header("Chat with multiple Files :")
|
|
|
131 |
|
132 |
st.subheader("Your documents")
|
133 |
docs = st.file_uploader(
|
134 |
+
"Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
|
135 |
if st.button("Process"):
|
136 |
+
with st.spinner("Processing"):
|
137 |
+
# get pdf text
|
138 |
+
doc_list = []
|
139 |
+
|
140 |
+
for file in docs:
|
141 |
+
print('file - type : ', file.type)
|
142 |
+
if file.type == 'text/plain':
|
143 |
+
# file is .txt
|
144 |
+
doc_list.extend(get_text_file([file]))
|
145 |
+
elif file.type in ['application/octet-stream', 'application/pdf']:
|
146 |
+
# file is .pdf
|
147 |
+
doc_list.extend(get_pdf_text(file))
|
148 |
+
elif file.type == 'text/csv':
|
149 |
+
# file is .csv
|
150 |
+
doc_list.extend(get_csv_file([file]))
|
151 |
+
elif file.type == 'application/json':
|
152 |
+
# file is .json
|
153 |
+
doc_list.extend(get_json_file([file]))
|
154 |
+
|
155 |
+
# get the text chunks
|
156 |
+
text_chunks = get_text_chunks(doc_list)
|
157 |
+
|
158 |
+
# create vector store
|
159 |
+
vectorstore = get_vectorstore(text_chunks)
|
160 |
+
|
161 |
+
# create conversation chain
|
162 |
+
st.session_state.conversation = get_conversation_chain(vectorstore)
|
163 |
|
164 |
|
165 |
if __name__ == '__main__':
|
166 |
+
main()
|