Update app.py
#1
by
dakkoong
- opened
app.py
CHANGED
|
@@ -28,15 +28,33 @@ def get_pdf_text(pdf_docs):
|
|
| 28 |
# κ³Όμ
|
| 29 |
# μλ ν
μ€νΈ μΆμΆ ν¨μλ₯Ό μμ±
|
| 30 |
|
| 31 |
-
def get_text_file(
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
|
| 42 |
# λ¬Έμλ€μ μ²λ¦¬νμ¬ ν
μ€νΈ μ²ν¬λ‘ λλλ ν¨μμ
λλ€.
|
|
@@ -147,5 +165,87 @@ def main():
|
|
| 147 |
vectorstore)
|
| 148 |
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
if __name__ == '__main__':
|
| 151 |
main()
|
|
|
|
| 28 |
# κ³Όμ
|
| 29 |
# μλ ν
μ€νΈ μΆμΆ ν¨μλ₯Ό μμ±
|
| 30 |
|
| 31 |
+
def get_text_file(text_docs):
|
| 32 |
+
temp_dir = tempfile.TemporaryDirectory()
|
| 33 |
+
temp_filepath = os.path.join(temp_dir.name, text_docs.name)
|
| 34 |
+
with open(temp_filepath, "wb") as f:
|
| 35 |
+
f.write(text_docs.getvalue())
|
| 36 |
+
text_loader = TextLoader(temp_filepath)
|
| 37 |
+
text_doc = text_loader.load()
|
| 38 |
+
return text_doc
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def get_csv_file(csv_docs):
|
| 42 |
+
temp_dir = tempfile.TemporaryDirectory()
|
| 43 |
+
temp_filepath = os.path.join(temp_dir.name, csv_docs.name)
|
| 44 |
+
with open(temp_filepath, "wb") as f:
|
| 45 |
+
f.write(csv_docs.getvalue())
|
| 46 |
+
csv_loader = CSVLoader(temp_filepath)
|
| 47 |
+
csv_doc = csv_loader.load()
|
| 48 |
+
return csv_doc
|
| 49 |
+
|
| 50 |
+
def get_json_file(json_docs):
|
| 51 |
+
temp_dir = tempfile.TemporaryDirectory()
|
| 52 |
+
temp_filepath = os.path.join(temp_dir.name, json_docs.name)
|
| 53 |
+
with open(temp_filepath, "wb") as f:
|
| 54 |
+
f.write(json_docs.getvalue())
|
| 55 |
+
json_loader = JSONLoader(temp_filepath)
|
| 56 |
+
json_doc = json_loader.load()
|
| 57 |
+
return json_doc
|
| 58 |
|
| 59 |
|
| 60 |
# λ¬Έμλ€μ μ²λ¦¬νμ¬ ν
μ€νΈ μ²ν¬λ‘ λλλ ν¨μμ
λλ€.
|
|
|
|
| 165 |
vectorstore)
|
| 166 |
|
| 167 |
|
| 168 |
+
if __name__ == '__main__':
|
| 169 |
+
main()
|
| 170 |
+
e)
|
| 171 |
+
# λν κ²μ 체μΈμ μμ±ν©λλ€.
|
| 172 |
+
conversation_chain = ConversationalRetrievalChain.from_llm(
|
| 173 |
+
llm=llm,
|
| 174 |
+
retriever=vectorstore.as_retriever(),
|
| 175 |
+
memory=memory
|
| 176 |
+
)
|
| 177 |
+
return conversation_chain
|
| 178 |
+
|
| 179 |
+
# μ¬μ©μ μ
λ ₯μ μ²λ¦¬νλ ν¨μμ
λλ€.
|
| 180 |
+
def handle_userinput(user_question):
|
| 181 |
+
# λν 체μΈμ μ¬μ©νμ¬ μ¬μ©μ μ§λ¬Έμ λν μλ΅μ μμ±ν©λλ€.
|
| 182 |
+
response = st.session_state.conversation({'question': user_question})
|
| 183 |
+
# λν κΈ°λ‘μ μ μ₯ν©λλ€.
|
| 184 |
+
st.session_state.chat_history = response['chat_history']
|
| 185 |
+
|
| 186 |
+
for i, message in enumerate(st.session_state.chat_history):
|
| 187 |
+
if i % 2 == 0:
|
| 188 |
+
st.write(user_template.replace(
|
| 189 |
+
"{{MSG}}", message.content), unsafe_allow_html=True)
|
| 190 |
+
else:
|
| 191 |
+
st.write(bot_template.replace(
|
| 192 |
+
"{{MSG}}", message.content), unsafe_allow_html=True)
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
def main():
|
| 196 |
+
load_dotenv()
|
| 197 |
+
st.set_page_config(page_title="Chat with multiple Files",
|
| 198 |
+
page_icon=":books:")
|
| 199 |
+
st.write(css, unsafe_allow_html=True)
|
| 200 |
+
|
| 201 |
+
if "conversation" not in st.session_state:
|
| 202 |
+
st.session_state.conversation = None
|
| 203 |
+
if "chat_history" not in st.session_state:
|
| 204 |
+
st.session_state.chat_history = None
|
| 205 |
+
|
| 206 |
+
st.header("Chat with multiple Files :")
|
| 207 |
+
user_question = st.text_input("Ask a question about your documents:")
|
| 208 |
+
if user_question:
|
| 209 |
+
handle_userinput(user_question)
|
| 210 |
+
|
| 211 |
+
with st.sidebar:
|
| 212 |
+
openai_key = st.text_input("Paste your OpenAI API key (sk-...)")
|
| 213 |
+
if openai_key:
|
| 214 |
+
os.environ["OPENAI_API_KEY"] = openai_key
|
| 215 |
+
|
| 216 |
+
st.subheader("Your documents")
|
| 217 |
+
docs = st.file_uploader(
|
| 218 |
+
"Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
|
| 219 |
+
if st.button("Process"):
|
| 220 |
+
with st.spinner("Processing"):
|
| 221 |
+
# get pdf text
|
| 222 |
+
doc_list = []
|
| 223 |
+
|
| 224 |
+
for file in docs:
|
| 225 |
+
print('file - type : ', file.type)
|
| 226 |
+
if file.type == 'text/plain':
|
| 227 |
+
# file is .txt
|
| 228 |
+
doc_list.extend(get_text_file(file))
|
| 229 |
+
elif file.type in ['application/octet-stream', 'application/pdf']:
|
| 230 |
+
# file is .pdf
|
| 231 |
+
doc_list.extend(get_pdf_text(file))
|
| 232 |
+
elif file.type == 'text/csv':
|
| 233 |
+
# file is .csv
|
| 234 |
+
doc_list.extend(get_csv_file(file))
|
| 235 |
+
elif file.type == 'application/json':
|
| 236 |
+
# file is .json
|
| 237 |
+
doc_list.extend(get_json_file(file))
|
| 238 |
+
|
| 239 |
+
# get the text chunks
|
| 240 |
+
text_chunks = get_text_chunks(doc_list)
|
| 241 |
+
|
| 242 |
+
# create vector store
|
| 243 |
+
vectorstore = get_vectorstore(text_chunks)
|
| 244 |
+
|
| 245 |
+
# create conversation chain
|
| 246 |
+
st.session_state.conversation = get_conversation_chain(
|
| 247 |
+
vectorstore)
|
| 248 |
+
|
| 249 |
+
|
| 250 |
if __name__ == '__main__':
|
| 251 |
main()
|