minji1115 commited on
Commit
d196bf7
·
1 Parent(s): fa0e437

wb로 변경

Browse files
Files changed (1) hide show
  1. app.py +21 -18
app.py CHANGED
@@ -11,19 +11,21 @@ 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 os
16
 
17
 
18
 
 
19
  def get_pdf_text(pdf_docs):
20
- temp_dir = tempfile.TemporaryDirectory() # 임시 디렉토리를 생성합니다.
21
- temp_filepath = os.path.join(temp_dir.name, pdf_docs.name) # 임시 파일 경로를 생성합니다.
22
  with open(temp_filepath, "wb") as f: # 임시 파일을 바이너리 쓰기 모드로 엽니다.
23
- f.write(pdf_docs.getvalue()) # PDF 문서의 내용을 임시 파일에 씁니다.
24
- pdf_loader = PyPDFLoader(temp_filepath) # PyPDFLoader를 사용해 PDF를 로드합니다.
25
- pdf_doc = pdf_loader.load() # 텍스트를 추출합니다.
26
- return pdf_doc # 추출한 텍스트를 반환합니다.
 
27
 
28
  # 과제
29
  # 아래 텍스트 추출 함수를 작성
@@ -56,17 +58,17 @@ def get_json_file(json_docs):
56
  json_docs = json_loader.load()
57
  return json_docs
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
  # 텍스트 청크들로부터 벡터 스토어를 생성하는 함수입니다.
@@ -74,15 +76,15 @@ def get_vectorstore(text_chunks):
74
  # OpenAI 임베딩 모델을 로드합니다. (Embedding models - Ada v2)
75
 
76
  embeddings = OpenAIEmbeddings()
77
- vectorstore = FAISS.from_documents(text_chunks, embeddings) # FAISS 벡터 스토어를 생성합니다.
78
 
79
- return vectorstore # 생성된 벡터 스토어를 반환합니다.
80
 
81
 
82
  def get_conversation_chain(vectorstore):
83
  gpt_model_name = 'gpt-3.5-turbo'
84
- llm = ChatOpenAI(model_name = gpt_model_name) #gpt-3.5 모델 로드
85
-
86
  # 대화 기록을 저장하기 위한 메모리를 생성합니다.
87
  memory = ConversationBufferMemory(
88
  memory_key='chat_history', return_messages=True)
@@ -94,6 +96,7 @@ def get_conversation_chain(vectorstore):
94
  )
95
  return conversation_chain
96
 
 
97
  # 사용자 입력을 처리하는 함수입니다.
98
  def handle_userinput(user_question):
99
  # 대화 체인을 사용하여 사용자 질문에 대한 응답을 생성합니다.
@@ -166,4 +169,4 @@ def main():
166
 
167
 
168
  if __name__ == '__main__':
169
- main()
 
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
 
19
+ # PDF 문서로부터 텍스트를 추출하는 함수입니다.
20
  def get_pdf_text(pdf_docs):
21
+ temp_dir = tempfile.TemporaryDirectory() # 임시 디렉토리를 생성합니다.
22
+ temp_filepath = os.path.join(temp_dir.name, pdf_docs.name) # 임시 파일 경로를 생성합니다.
23
  with open(temp_filepath, "wb") as f: # 임시 파일을 바이너리 쓰기 모드로 엽니다.
24
+ f.write(pdf_docs.getvalue()) # PDF 문서의 내용을 임시 파일에 씁니다.
25
+ pdf_loader = PyPDFLoader(temp_filepath) # PyPDFLoader를 사용해 PDF를 로드합니다.
26
+ pdf_doc = pdf_loader.load() # 텍스트를 추출합니다.
27
+ return pdf_doc # 추출한 텍스트를 반환합니다.
28
+
29
 
30
  # 과제
31
  # 아래 텍스트 추출 함수를 작성
 
58
  json_docs = json_loader.load()
59
  return json_docs
60
 
61
+
62
  # 문서들을 처리하여 텍스트 청크로 나누는 함수입니다.
63
  def get_text_chunks(documents):
64
  text_splitter = RecursiveCharacterTextSplitter(
65
+ chunk_size=1000, # 청크의 크기를 지정합니다.
66
+ chunk_overlap=200, # 청크 사이의 중복을 지정합니다.
67
+ length_function=len # 텍스트의 길이를 측정하는 함수를 지정합니다.
68
  )
69
 
70
+ documents = text_splitter.split_documents(documents) # 문서들을 청크로 나눕니다
71
+ return documents # 나눈 청크를 반환합니다.
72
 
73
 
74
  # 텍스트 청크들로부터 벡터 스토어를 생성하는 함수입니다.
 
76
  # OpenAI 임베딩 모델을 로드합니다. (Embedding models - Ada v2)
77
 
78
  embeddings = OpenAIEmbeddings()
79
+ vectorstore = FAISS.from_documents(text_chunks, embeddings) # FAISS 벡터 스토어를 생성합니다.
80
 
81
+ return vectorstore # 생성된 벡터 스토어를 반환합니다.
82
 
83
 
84
  def get_conversation_chain(vectorstore):
85
  gpt_model_name = 'gpt-3.5-turbo'
86
+ llm = ChatOpenAI(model_name=gpt_model_name) # gpt-3.5 모델 로드
87
+
88
  # 대화 기록을 저장하기 위한 메모리를 생성합니다.
89
  memory = ConversationBufferMemory(
90
  memory_key='chat_history', return_messages=True)
 
96
  )
97
  return conversation_chain
98
 
99
+
100
  # 사용자 입력을 처리하는 함수입니다.
101
  def handle_userinput(user_question):
102
  # 대화 체인을 사용하여 사용자 질문에 대한 응답을 생성합니다.
 
169
 
170
 
171
  if __name__ == '__main__':
172
+ main()