bin20 commited on
Commit
a907b29
·
1 Parent(s): 91f2adf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -18
app.py CHANGED
@@ -24,32 +24,36 @@ def get_pdf_text(pdf_docs):
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_contents.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
- csvfile.write(csv_datas.getvalue()) # CSV 데이터를 파일에 씁니다.
41
- csv_loader = CSVLoader(temp_filepath)
42
  csv_data = csv_loader.load()
43
- return csv_data
 
44
 
45
  def get_json_file(json_datas):
46
  temp_dir = tempfile.TemporaryDirectory() # 임시 디렉토리를 생성합니다.
47
  temp_filepath = os.path.join(temp_dir.name, json_datas.name) # 임시 JSON 파일 경로를 생성합니다.
48
  with open(temp_filepath, "w", encoding="utf-8") as jsonfile: # JSON 파일을 쓰기 모드로 엽니다.
49
  jsonfile.write(json_datas.getvalue()) # JSON 데이터를 파일에 씁니다.
50
- json_loader = JSONLoader(temp_filepath)
 
 
 
51
  json_data = json_loader.load()
52
- return json_data
53
 
54
  # 문서들을 처리하여 텍스트 청크로 나누는 함수입니다.
55
  def get_text_chunks(documents):
 
24
 
25
  # 과제
26
  # 아래 텍스트 추출 함수를 작성
27
+ def get_text_file(text_data):
28
+ temp_dir = tempfile.TemporaryDirectory()
29
+ temp_filepath = os.path.join(temp_dir.name, text_data.name)
30
+ with open(temp_filepath, "w", encoding="utf-8") as textfile:
31
+ textfile.write(text_data.getvalue())
32
+ text_loader = TextLoader(temp_filepath)
33
+ text = text_loader.load()
34
+ return text
35
+
36
+ def get_csv_text(csv_file):
37
+ temp_dir = tempfile.TemporaryDirectory()
38
+ temp_filepath = os.path.join(temp_dir.name, csv_file.name)
39
+ with open(temp_filepath, "w", encoding="utf-8") as csvfile:
40
+ csvfile.write(csv_file.getvalue())
41
+ csv_loader = CSVLoader(temp_filepath, text_column='text_column_name')
42
  csv_data = csv_loader.load()
43
+ text_from_csv = '\n'.join(csv_data['text_column_name']) if 'text_column_name' in csv_data else ''
44
+ return text_from_csv
45
 
46
  def get_json_file(json_datas):
47
  temp_dir = tempfile.TemporaryDirectory() # 임시 디렉토리를 생성합니다.
48
  temp_filepath = os.path.join(temp_dir.name, json_datas.name) # 임시 JSON 파일 경로를 생성합니다.
49
  with open(temp_filepath, "w", encoding="utf-8") as jsonfile: # JSON 파일을 쓰기 모드로 엽니다.
50
  jsonfile.write(json_datas.getvalue()) # JSON 데이터를 파일에 씁니다.
51
+ json_loader = JSONLoader(
52
+ temp_filepath,
53
+ jq_schema='messages[].content',
54
+ text_content=False)
55
  json_data = json_loader.load()
56
+ return json_data
57
 
58
  # 문서들을 처리하여 텍스트 청크로 나누는 함수입니다.
59
  def get_text_chunks(documents):