bin20 commited on
Commit
a7da2ba
·
1 Parent(s): 0b3d8c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -8
app.py CHANGED
@@ -24,14 +24,30 @@ def get_pdf_text(pdf_docs):
24
 
25
  # 과제
26
  # 아래 텍스트 추출 함수를 작성
27
- def get_text_file(docs):
28
- pass
29
-
30
- def get_csv_file(docs):
31
- pass
32
-
33
- def get_json_file(docs):
34
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
 
37
  # 문서들을 처리하여 텍스트 청크로 나누는 함수입니다.
 
24
 
25
  # 과제
26
  # 아래 텍스트 추출 함수를 작성
27
+ def get_text_file(text_contents):
28
+ temp_dir = tempfile.TemporaryDirectory() # 임시 디렉토리를 생성합니다.
29
+ temp_filepath = os.path.join(temp_dir.name, text_contents.name) # 임시 텍스트 파일 경로를 생성합니다.
30
+ with open(temp_filepath, "w", encoding="utf-8") as f: # 텍스트 파일을 쓰기 모드로 엽니다.
31
+ f.write(text_docs.getvalue()) # 텍스트 데이터를 파일에 씁니다.
32
+ text_loader = TextLoader(temp_filepath) # TextLoader를 사용해 텍스트 파일을 로드합니다.
33
+ text_content = text_loader.load() # 텍스트를 추출합니다.
34
+ return text_content # 추출한 텍스트를 반환합니다.
35
+
36
+ def get_csv_file(csv_datas):
37
+ temp_dir = tempfile.TemporaryDirectory() # 임시 디렉토리를 생성합니다.
38
+ temp_filepath = os.path.join(temp_dir.name, csv_datas.name) # 임시 CSV 파일 경로를 생성합니다.
39
+ with open(temp_filepath, "w", newline="", encoding="utf-8") as csvfile: # CSV 파일을 쓰기 모드로 엽니다.
40
+ csv_loader = CSVLoader(csvfile)
41
+ csv_loader.load(csv_data) # CSV 데이터를 파일에 씁니다.
42
+ return temp_filepath # 생성된 임시 CSV 파일 경로를 반환합니다.
43
+
44
+ def get_json_file(json_data):
45
+ temp_dir = tempfile.TemporaryDirectory() # 임시 디렉토리를 생성합니다.
46
+ temp_filepath = os.path.join(temp_dir.name, json_datas.name) # 임시 JSON 파일 경로를 생성합니다.
47
+ with open(temp_filepath, "w", encoding="utf-8") as jsonfile: # JSON 파일을 쓰기 모드로 엽니다.
48
+ json_loader = JSONLoader(jsonfile)
49
+ json_loader.load(json_data) # JSON 데이터를 파일에 씁니다.
50
+ return temp_filepath # 생성된 임시 JSON 파일 경로를 반환합니다
51
 
52
 
53
  # 문서들을 처리하여 텍스트 청크로 나누는 함수입니다.