nyoo827 commited on
Commit
0907b5f
Β·
1 Parent(s): 79f96f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -22
app.py CHANGED
@@ -25,32 +25,31 @@ def get_pdf_text(pdf_docs):
25
  # 과제
26
  # μ•„λž˜ ν…μŠ€νŠΈ μΆ”μΆœ ν•¨μˆ˜λ₯Ό μž‘μ„±
27
  def get_text_file(text_docs):
28
- text_data = []
29
- for doc in docs:
30
- if doc.lower().endswith('.txt'):
31
- with open(doc, 'r', encoding='utf-8') as file:
32
- text = file.read()
33
- text_data.append(text)
34
- return text_data
35
 
36
  def get_csv_file(csv_docs):
37
- csv_data = []
38
- for doc in docs:
39
- if doc.lower().endswith('.csv'):
40
- with open(doc, 'r', newline='', encoding='utf-8') as file:
41
- csv_reader = csv.reader(file)
42
- data = [row for row in csv_reader]
43
- csv_data.append(data)
44
- return csv_data
45
 
46
  def get_json_file(json_docs):
47
- json_data = []
48
- for doc in docs:
49
- if doc.lower().endswith('.json'):
50
- with open(doc, 'r', encoding='utf-8') as file:
51
- data = json.load(file)
52
- json_data.append(data)
53
- return json_data
54
 
55
  # λ¬Έμ„œλ“€μ„ μ²˜λ¦¬ν•˜μ—¬ ν…μŠ€νŠΈ 청크둜 λ‚˜λˆ„λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
56
  def get_text_chunks(documents):
 
25
  # 과제
26
  # μ•„λž˜ ν…μŠ€νŠΈ μΆ”μΆœ ν•¨μˆ˜λ₯Ό μž‘μ„±
27
  def get_text_file(text_docs):
28
+ temp_dir = tempfile.TemporaryDirectory() # μž„μ‹œ 디렉토리λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
29
+ temp_filepath = os.path.join(temp_dir.name, text_docs.name) # μž„μ‹œ 파일 경둜λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
30
+ with open(temp_filepath, "wb") as f: # μž„μ‹œ νŒŒμΌμ„ λ°”μ΄λ„ˆλ¦¬ μ“°κΈ° λͺ¨λ“œλ‘œ μ—½λ‹ˆλ‹€.
31
+ f.write(text_docs.getvalue())
32
+ text_loader = DirectoryLoader(temp_filepath)
33
+ text_doc = text_loader.load()
34
+ return text_doc
35
 
36
  def get_csv_file(csv_docs):
37
+ temp_dir = tempfile.TemporaryDirectory() # μž„μ‹œ 디렉토리λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
38
+ temp_filepath = os.path.join(temp_dir.name, csv_docs.name) # μž„μ‹œ 파일 경둜λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
39
+ with open(temp_filepath, "wb") as f: # μž„μ‹œ νŒŒμΌμ„ λ°”μ΄λ„ˆλ¦¬ μ“°κΈ° λͺ¨λ“œλ‘œ μ—½λ‹ˆλ‹€.
40
+ f.write(csv_docs.getvalue())
41
+ csv_loader = CSVLoader(temp_filepath)
42
+ csv_doc = csv_loader.load()
43
+ return csv_doc
 
44
 
45
  def get_json_file(json_docs):
46
+ temp_dir = tempfile.TemporaryDirectory() # μž„μ‹œ 디렉토리λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
47
+ temp_filepath = os.path.join(temp_dir.name, json_docs.name) # μž„μ‹œ 파일 경둜λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
48
+ with open(temp_filepath, "wb") as f: # μž„μ‹œ νŒŒμΌμ„ λ°”μ΄λ„ˆλ¦¬ μ“°κΈ° λͺ¨λ“œλ‘œ μ—½λ‹ˆλ‹€.
49
+ f.write(json_docs.getvalue())
50
+ json_loader = JSONLoader(temp_filepath)
51
+ json_doc = json_loader.load()
52
+ return json_doc
53
 
54
  # λ¬Έμ„œλ“€μ„ μ²˜λ¦¬ν•˜μ—¬ ν…μŠ€νŠΈ 청크둜 λ‚˜λˆ„λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
55
  def get_text_chunks(documents):