Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -29,34 +29,17 @@ def get_pdf_text(pdf_docs):
|
|
| 29 |
# 과제
|
| 30 |
# 아래 텍스트 추출 함수를 작성
|
| 31 |
|
| 32 |
-
def get_text_file(
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
with open(temp_filepath, "wb") as f: # 임시 파일을 바이너리 쓰기 모드로 엽니다.
|
| 36 |
-
f.write(text_docs.getvalue()) # PDF 문서의 내용을 임시 파일에 씁니다.
|
| 37 |
-
text_loader = PyPDFLoader(temp_filepath) # PyPDFLoader를 사용해 PDF를 로드합니다.
|
| 38 |
-
text_doc = text_loader.load() # 텍스트를 추출합니다.
|
| 39 |
-
return text_doc # 추출한 텍스트를 반환합니다.
|
| 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()) # PDF 문서의 내용을 임시 파일에 씁니다.
|
| 46 |
-
csv_loader = PyPDFLoader(temp_filepath) # PyPDFLoader를 사용해 PDF를 로드합니다.
|
| 47 |
-
csv_doc = csv_loader.load() # 텍스트를 추출합니다.
|
| 48 |
-
return csv_doc # 추출한 텍스트를 반환합니다.
|
| 49 |
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
def get_json_file(
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
with open(temp_filepath, "wb") as f: # 임시 파일을 바이너리 쓰기 모드로 엽니다.
|
| 55 |
-
f.write(json_docs.getvalue()) # PDF 문서의 내용을 임시 파일에 씁니다.
|
| 56 |
-
json_loader = PyPDFLoader(temp_filepath) # PyPDFLoader를 사용해 PDF를 로드합니다.
|
| 57 |
-
json_doc = json_loader.load() # 텍스트를 추출합니다.
|
| 58 |
-
return json_doc # 추출한 텍스트를 반환합니다.
|
| 59 |
-
|
| 60 |
|
| 61 |
|
| 62 |
# 문서들을 처리하여 텍스트 청크로 나누는 함수입니다.
|
|
@@ -137,35 +120,24 @@ def main():
|
|
| 137 |
st.subheader("Your documents")
|
| 138 |
docs = st.file_uploader(
|
| 139 |
"Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
|
|
|
|
| 140 |
if st.button("Process"):
|
| 141 |
with st.spinner("Processing"):
|
| 142 |
-
# get pdf text
|
| 143 |
doc_list = []
|
| 144 |
|
| 145 |
for file in docs:
|
| 146 |
-
print('file - type : ', file.type)
|
| 147 |
if file.type == 'text/plain':
|
| 148 |
-
# file is .txt
|
| 149 |
doc_list.extend(get_text_file(file))
|
| 150 |
elif file.type in ['application/octet-stream', 'application/pdf']:
|
| 151 |
-
# file is .pdf
|
| 152 |
doc_list.extend(get_pdf_text(file))
|
| 153 |
elif file.type == 'text/csv':
|
| 154 |
-
# file is .csv
|
| 155 |
doc_list.extend(get_csv_file(file))
|
| 156 |
elif file.type == 'application/json':
|
| 157 |
-
# file is .json
|
| 158 |
doc_list.extend(get_json_file(file))
|
| 159 |
|
| 160 |
-
# get the text chunks
|
| 161 |
text_chunks = get_text_chunks(doc_list)
|
| 162 |
-
|
| 163 |
-
# create vector store
|
| 164 |
vectorstore = get_vectorstore(text_chunks)
|
| 165 |
-
|
| 166 |
-
# create conversation chain
|
| 167 |
-
st.session_state.conversation = get_conversation_chain(
|
| 168 |
-
vectorstore)
|
| 169 |
|
| 170 |
|
| 171 |
if __name__ == '__main__':
|
|
|
|
| 29 |
# 과제
|
| 30 |
# 아래 텍스트 추출 함수를 작성
|
| 31 |
|
| 32 |
+
def get_text_file(docs):
|
| 33 |
+
text_loader = TextLoader(docs) # 텍스트 파일을 처리할 TextLoader 사용
|
| 34 |
+
return text_loader.load()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
def get_csv_file(docs):
|
| 37 |
+
csv_loader = CSVLoader(docs) # CSV 파일을 처리할 CSVLoader 사용
|
| 38 |
+
return csv_loader.load()
|
| 39 |
|
| 40 |
+
def get_json_file(docs):
|
| 41 |
+
json_loader = JSONLoader(docs) # JSON 파일을 처리할 JSONLoader 사용
|
| 42 |
+
return json_loader.load()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
|
| 45 |
# 문서들을 처리하여 텍스트 청크로 나누는 함수입니다.
|
|
|
|
| 120 |
st.subheader("Your documents")
|
| 121 |
docs = st.file_uploader(
|
| 122 |
"Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
|
| 123 |
+
# 'Process' 버튼 블록 내에서
|
| 124 |
if st.button("Process"):
|
| 125 |
with st.spinner("Processing"):
|
|
|
|
| 126 |
doc_list = []
|
| 127 |
|
| 128 |
for file in docs:
|
|
|
|
| 129 |
if file.type == 'text/plain':
|
|
|
|
| 130 |
doc_list.extend(get_text_file(file))
|
| 131 |
elif file.type in ['application/octet-stream', 'application/pdf']:
|
|
|
|
| 132 |
doc_list.extend(get_pdf_text(file))
|
| 133 |
elif file.type == 'text/csv':
|
|
|
|
| 134 |
doc_list.extend(get_csv_file(file))
|
| 135 |
elif file.type == 'application/json':
|
|
|
|
| 136 |
doc_list.extend(get_json_file(file))
|
| 137 |
|
|
|
|
| 138 |
text_chunks = get_text_chunks(doc_list)
|
|
|
|
|
|
|
| 139 |
vectorstore = get_vectorstore(text_chunks)
|
| 140 |
+
st.session_state.conversation = get_conversation_chain(vectorstore)
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
|
| 143 |
if __name__ == '__main__':
|