kms commited on
Commit
0e2e238
ยท
1 Parent(s): c08a856
Files changed (1) hide show
  1. app.py +24 -21
app.py CHANGED
@@ -25,35 +25,38 @@ def get_pdf_text(pdf_docs):
25
  pdf_doc = pdf_loader.load() # ํ…์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.
26
  return pdf_doc # ์ถ”์ถœํ•œ ํ…์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
27
 
 
28
  # ํ…์ŠคํŠธ ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ํ…์ŠคํŠธ ์ถ”์ถœ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑ
29
  def get_text_file(text_docs):
30
- temp_dir = tempfile.TemporaryDirectory()
31
- temp_filepath = os.path.join(temp_dir.name, text_docs.name)
32
- with open(temp_filepath, "wb") as f:
33
- f.write(text_docs.getvalue())
34
- text_loader = TextLoader(temp_filepath)
35
- text_docs = text_loader.load()
36
- return text_docs
37
 
38
  # csv ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ํ…์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
39
  def get_csv_file(csv_docs):
40
- temp_dir = tempfile.TemporaryDirectory()
41
- temp_filepath = os.path.join(temp_dir.name, csv_docs.name)
42
- with open(temp_filepath, "wb") as f:
43
- f.write(csv_docs.getvalue())
44
- csv_loader = CSVLoader(temp_filepath)
45
- csv_docs = csv_loader.load()
46
- return csv_docs
47
 
48
  # json ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ํ…์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
49
  def get_json_file(json_docs):
50
- temp_dir = tempfile.TemporaryDirectory()
51
- temp_filepath = os.path.join(temp_dir.name, json_docs.name)
52
- with open(temp_filepath, "wb") as f:
53
- f.write(json_docs.getvalue())
54
- json_loader = JSONLoader(filepath=temp_filepath, jq_schema='.messages[].content', text_content=False)
55
- json_docs = json_loader.load()
56
- return json_docs
 
 
57
 
58
 
59
  # ๋ฌธ์„œ๋“ค์„ ์ฒ˜๋ฆฌํ•˜์—ฌ ํ…์ŠคํŠธ ์ฒญํฌ๋กœ ๋‚˜๋ˆ„๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
 
25
  pdf_doc = pdf_loader.load() # ํ…์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.
26
  return pdf_doc # ์ถ”์ถœํ•œ ํ…์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
27
 
28
+ # ๊ณผ์ œ
29
  # ํ…์ŠคํŠธ ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ํ…์ŠคํŠธ ์ถ”์ถœ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑ
30
  def get_text_file(text_docs):
31
+ temp_dir = tempfile.TemporaryDirectory() # ์ž„์‹œ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
32
+ temp_filepath = os.path.join(temp_dir.name, text_docs.name) # ์ž„์‹œ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
33
+ with open(temp_filepath, "wb") as f: # ์ž„์‹œ ํŒŒ์ผ์„ ๋ฐ”์ด๋„ˆ๋ฆฌ ์“ฐ๊ธฐ ๋ชจ๋“œ๋กœ ์—ฝ๋‹ˆ๋‹ค.
34
+ f.write(text_docs.getvalue()) # txt ๋ฌธ์„œ์˜ ๋‚ด์šฉ์„ ์ž„์‹œ ํŒŒ์ผ์— ์”๋‹ˆ๋‹ค.
35
+ text_loader = TextLoader(temp_filepath) # TextLoader๋ฅผ ์‚ฌ์šฉํ•ด text๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.
36
+ text_docs = text_loader.load() # ํ…์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.
37
+ return text_docs # ์ถ”์ถœํ•œ ํ…์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
38
 
39
  # csv ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ํ…์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
40
  def get_csv_file(csv_docs):
41
+ temp_dir = tempfile.TemporaryDirectory() # ์ž„์‹œ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
42
+ temp_filepath = os.path.join(temp_dir.name, csv_docs.name) # ์ž„์‹œ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
43
+ with open(temp_filepath, "wb") as f: # ์ž„์‹œ ํŒŒ์ผ์„ ๋ฐ”์ด๋„ˆ๋ฆฌ ์“ฐ๊ธฐ ๋ชจ๋“œ๋กœ ์—ฝ๋‹ˆ๋‹ค.
44
+ f.write(csv_docs.getvalue()) # csv ๋ฌธ์„œ์˜ ๋‚ด์šฉ์„ ์ž„์‹œ ํŒŒ์ผ์— ์”๋‹ˆ๋‹ค.
45
+ csv_loader = CSVLoader(temp_filepath) # CSVLoader๋ฅผ ์‚ฌ์šฉํ•ด csv๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.
46
+ csv_docs = csv_loader.load() # ํ…์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.
47
+ return csv_docs # ์ถ”์ถœํ•œ ํ…์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
48
 
49
  # json ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ํ…์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
50
  def get_json_file(json_docs):
51
+ temp_dir = tempfile.TemporaryDirectory() # ์ž„์‹œ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
52
+ temp_filepath = os.path.join(temp_dir.name, json_docs.name) # ์ž„์‹œ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
53
+ with open(temp_filepath, "wb") as f: # ์ž„์‹œ ํŒŒ์ผ์„ ๋ฐ”์ด๋„ˆ๋ฆฌ ์“ฐ๊ธฐ ๋ชจ๋“œ๋กœ ์—ฝ๋‹ˆ๋‹ค.
54
+ f.write(json_docs.getvalue()) # json ๋ฌธ์„œ์˜ ๋‚ด์šฉ์„ ์ž„์‹œ ํŒŒ์ผ์— ์”๋‹ˆ๋‹ค.
55
+ json_loader = JSONLoader(filepath=temp_filepath,
56
+ jq_schema='.messages[].content',
57
+ text_content=False) # JSONLoader๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ json ํŒŒ์ผ์˜ content ๋ถ€๋ถ„์„ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.
58
+ json_docs = json_loader.load() # ํ…์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.
59
+ return json_docs # ์ถ”์ถœํ•œ ํ…์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
60
 
61
 
62
  # ๋ฌธ์„œ๋“ค์„ ์ฒ˜๋ฆฌํ•˜์—ฌ ํ…์ŠคํŠธ ์ฒญํฌ๋กœ ๋‚˜๋ˆ„๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.