Daoneeee commited on
Commit
3a75faa
·
1 Parent(s): df187da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -19
app.py CHANGED
@@ -11,12 +11,8 @@ from langchain.chains import ConversationalRetrievalChain
11
  from htmlTemplates import css, bot_template, user_template
12
  from langchain.llms import HuggingFaceHub, LlamaCpp, CTransformers # For loading transformer models.
13
  from langchain.document_loaders import PyPDFLoader, TextLoader, JSONLoader, CSVLoader
14
- import tempfile # 임시 파일을 생성하기 위한 라이브러리입니다.
15
- import json
16
  import os
17
- from langchain.text_splitter import RecursiveCharacterTextSplitter
18
-
19
-
20
 
21
 
22
  # PDF 문서로부터 텍스트를 추출하는 함수입니다.
@@ -51,35 +47,27 @@ def get_csv_file(csv_docs):
51
  csv_doc = csv_loader.load()
52
  return csv_doc
53
 
54
-
55
  def get_json_file(json_docs):
56
  temp_dir = tempfile.TemporaryDirectory()
57
  temp_filepath = os.path.join(temp_dir.name, json_docs.name)
58
  with open(temp_filepath, "wb") as f:
59
  f.write(json_docs.getvalue())
60
-
61
- # 파일을 열어 JSON 형식으로 읽기
62
- with open(temp_filepath, 'r', encoding='utf-8') as json_file:
63
- json_content = json.load(json_file)
64
- # JSON 데이터를 문자열로 변환
65
- json_string = json.dumps(json_content)
66
- # 변환된 문자열을 리스트에 담아 반환
67
- return [json_string]
68
-
69
 
70
 
71
  # 문서들을 처리하여 텍스트 청크로 나누는 함수입니다.
72
  def get_text_chunks(documents):
73
- # RecursiveCharacterTextSplitter를 직접 초기화합니다.
74
  text_splitter = RecursiveCharacterTextSplitter(
75
  chunk_size=1000, # 청크의 크기를 지정합니다.
76
  chunk_overlap=200, # 청크 사이의 중복을 지정합니다.
77
  length_function=len # 텍스트의 길이를 측정하는 함수를 지정합니다.
78
  )
79
 
80
- # 문서를 청크로 나눕니다.
81
- text_chunks = text_splitter.split_text(documents)
82
- return text_chunks
83
 
84
  # 텍스트 청크들로부터 벡터 스토어를 생성하는 함수입니다.
85
  def get_vectorstore(text_chunks):
 
11
  from htmlTemplates import css, bot_template, user_template
12
  from langchain.llms import HuggingFaceHub, LlamaCpp, CTransformers # For loading transformer models.
13
  from langchain.document_loaders import PyPDFLoader, TextLoader, JSONLoader, CSVLoader
14
+ import tempfile # 임시 파일을 생성하기 위한 라이브러리입니다.
 
15
  import os
 
 
 
16
 
17
 
18
  # PDF 문서로부터 텍스트를 추출하는 함수입니다.
 
47
  csv_doc = csv_loader.load()
48
  return csv_doc
49
 
 
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())
55
+ json_loader = JSONLoader(temp_filepath)
56
+ json_doc = json_loader.load()
57
+ return json_doc
 
 
 
 
 
 
58
 
59
 
60
  # 문서들을 처리하여 텍스트 청크로 나누는 함수입니다.
61
  def get_text_chunks(documents):
 
62
  text_splitter = RecursiveCharacterTextSplitter(
63
  chunk_size=1000, # 청크의 크기를 지정합니다.
64
  chunk_overlap=200, # 청크 사이의 중복을 지정합니다.
65
  length_function=len # 텍스트의 길이를 측정하는 함수를 지정합니다.
66
  )
67
 
68
+ documents = text_splitter.split_documents(documents) # 문서들을 청크로 나눕니다
69
+ return documents # 나눈 청크를 반환합니다.
70
+
71
 
72
  # 텍스트 청크들로부터 벡터 스토어를 생성하는 함수입니다.
73
  def get_vectorstore(text_chunks):