EverJun2 commited on
Commit
6cd0edd
ยท
verified ยท
1 Parent(s): 1cfd969

Upload streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +45 -0
src/streamlit_app.py CHANGED
@@ -39,11 +39,23 @@ def get_pdf_text(pdf_docs):
39
 
40
  def get_text_file(docs):
41
  #################### ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•  ๋ถ€๋ถ„
 
 
 
 
 
 
42
  return text_doc
43
 
44
 
45
  def get_csv_file(docs):
46
  #################### ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•  ๋ถ€๋ถ„
 
 
 
 
 
 
47
  return csv_doc
48
 
49
  # def get_json_file(docs):
@@ -188,7 +200,40 @@ def main():
188
 
189
  ################## TXT, CSV ๋ฒ„ํŠผ ๊ตฌํ˜„
190
  # TXT ๋ฒ„ํŠผ ๊ตฌํ˜„ ์ฐธ๊ณ  : if file.type == 'text/plain':
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
  # CSV ๋ฒ„ํŠผ ๊ตฌํ˜„ ์ฐธ๊ณ  : if file.type == 'text/csv':
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
 
193
  if st.button("Process[JSON]"):
194
  with st.spinner("Processing"):
 
39
 
40
  def get_text_file(docs):
41
  #################### ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•  ๋ถ€๋ถ„
42
+ temp_dir = tempfile.TemporaryDirectory()
43
+ temp_filepath = os.path.join(temp_dir.name, docs.name)
44
+ with open(temp_filepath, "wb") as f:
45
+ f.write(docs.getvalue())
46
+ docs_loader = TextLoader(temp_filepath)
47
+ text_doc = docs_loader.load()
48
  return text_doc
49
 
50
 
51
  def get_csv_file(docs):
52
  #################### ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•  ๋ถ€๋ถ„
53
+ temp_dir = tempfile.TemporaryDirectory()
54
+ temp_filepath = os.path.join(temp_dir.name, docs.name)
55
+ with open(temp_filepath, "wb") as f:
56
+ f.write(docs.getvalue())
57
+ csv_loader = CSVLoader(temp_filepath)
58
+ csv_doc = csv_loader.load()
59
  return csv_doc
60
 
61
  # def get_json_file(docs):
 
200
 
201
  ################## TXT, CSV ๋ฒ„ํŠผ ๊ตฌํ˜„
202
  # TXT ๋ฒ„ํŠผ ๊ตฌํ˜„ ์ฐธ๊ณ  : if file.type == 'text/plain':
203
+ if st.button("Process[TXT]"):
204
+ with st.spinner("Processing"):
205
+ doc_list = []
206
+ for file in docs:
207
+ print('file - type : ', file.type)
208
+ if file.type == 'text/plain':
209
+ doc_list.extend(get_text_file(file))
210
+ else:
211
+ st.error("TXT ํŒŒ์ผ์ด ์•„๋‹™๋‹ˆ๋‹ค.")
212
+ if not doc_list:
213
+ st.error("์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•œ ๋ฌธ์„œ๋ฅผ ์ฐพ์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.")
214
+ st.stop()
215
+
216
+ text_chunks = get_text_chunks(doc_list)
217
+ vectorstore = get_vectorstore(text_chunks)
218
+ st.session_state.conversation = get_conversation_chain(vectorstore)
219
+
220
  # CSV ๋ฒ„ํŠผ ๊ตฌํ˜„ ์ฐธ๊ณ  : if file.type == 'text/csv':
221
+ if st.button("Process[CSV]"):
222
+ with st.spinner("Processing"):
223
+ doc_list = []
224
+ for file in docs:
225
+ print('file - type : ', file.type)
226
+ if file.type == 'text/csv':
227
+ doc_list.extend(get_csv_file(file))
228
+ else:
229
+ st.error("CSV ํŒŒ์ผ์ด ์•„๋‹™๋‹ˆ๋‹ค.")
230
+ if not doc_list:
231
+ st.error("์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•œ ๋ฌธ์„œ๋ฅผ ์ฐพ์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.")
232
+ st.stop()
233
+
234
+ text_chunks = get_text_chunks(doc_list)
235
+ vectorstore = get_vectorstore(text_chunks)
236
+ st.session_state.conversation = get_conversation_chain(vectorstore)
237
 
238
  if st.button("Process[JSON]"):
239
  with st.spinner("Processing"):