tony346 commited on
Commit
4c24534
β€’
1 Parent(s): 383414c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -44
app.py CHANGED
@@ -8,70 +8,71 @@ from langchain.chains import ConversationalRetrievalChain
8
  from htmlTemplates import css, bot_template, user_template
9
  from langchain.llms import LlamaCpp # For loading transformer models.
10
  from langchain.document_loaders import PyPDFLoader, TextLoader, JSONLoader, CSVLoader
11
- import tempfile
12
  import os
13
- from huggingface_hub import hf_hub_download
14
-
15
 
 
16
  def get_pdf_text(pdf_docs):
17
- temp_dir = tempfile.TemporaryDirectory()
18
- temp_filepath = os.path.join(temp_dir.name, pdf_docs.name)
19
- with open(temp_filepath, "wb") as f:
20
- f.write(pdf_docs.getvalue())
21
- pdf_loader = PyPDFLoader(temp_filepath)
22
- pdf_doc = pdf_loader.load()
23
- return pdf_doc
24
 
25
 
26
  def get_text_file(docs):
27
- with NamedTemporaryFile() as temp_file:
28
- temp_file.write(docs.getvalue())
29
- temp_file.seek(0)
30
- text_loader = TextLoader(temp_file.name)
31
- text_doc = text_loader.load()
32
-
33
- return text_doc
34
 
35
 
36
  def get_csv_file(docs):
37
- with NamedTemporaryFile() as temp_file:
38
- temp_file.write(docs.getvalue())
39
- temp_file.seek(0)
40
- text_loader = CSVLoader(temp_file.name)
41
- text_doc = text_loader.load()
42
-
43
- return text_doc
44
-
45
 
46
  def get_json_file(docs):
47
- with NamedTemporaryFile() as temp_file:
48
- temp_file.write(docs.getvalue())
49
- temp_file.seek(0)
50
- json_loader = JSONLoader(temp_file.name,
 
51
  jq_schema='.scans[].relationships',
52
  text_content=False)
53
- json_doc = json_loader.load()
54
-
55
- return json_doc
56
 
 
57
 
 
58
  def get_text_chunks(documents):
59
  text_splitter = RecursiveCharacterTextSplitter(
60
- chunk_size=1000,
61
- chunk_overlap=200,
62
- length_function=len
63
  )
64
 
65
- documents = text_splitter.split_documents(documents)
66
- return documents
67
 
68
 
 
69
  def get_vectorstore(text_chunks):
70
- # Load the desired embeddings model.
71
  embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L12-v2',
72
- model_kwargs={'device': 'cpu'})
73
- vectorstore = FAISS.from_documents(text_chunks, embeddings)
74
- return vectorstore
75
 
76
 
77
  def get_conversation_chain(vectorstore):
@@ -83,19 +84,23 @@ def get_conversation_chain(vectorstore):
83
  n_ctx=4086,
84
  input={"temperature": 0.75, "max_length": 2000, "top_p": 1},
85
  verbose=True, )
 
86
  memory = ConversationBufferMemory(
87
  memory_key='chat_history', return_messages=True)
 
88
  conversation_chain = ConversationalRetrievalChain.from_llm(
89
  llm=llm,
90
  retriever=vectorstore.as_retriever(),
91
  memory=memory
92
  )
93
- return conversation_chain
94
-
95
 
 
96
  def handle_userinput(user_question):
97
  print('user_question => ', user_question)
 
98
  response = st.session_state.conversation({'question': user_question})
 
99
  st.session_state.chat_history = response['chat_history']
100
 
101
  for i, message in enumerate(st.session_state.chat_history):
@@ -159,4 +164,4 @@ def main():
159
 
160
 
161
  if __name__ == '__main__':
162
- main()
 
8
  from htmlTemplates import css, bot_template, user_template
9
  from langchain.llms import LlamaCpp # For loading transformer models.
10
  from langchain.document_loaders import PyPDFLoader, TextLoader, JSONLoader, CSVLoader
11
+ import tempfile # μž„μ‹œ νŒŒμΌμ„ μƒμ„±ν•˜κΈ° μœ„ν•œ λΌμ΄λΈŒλŸ¬λ¦¬μž…λ‹ˆλ‹€.
12
  import os
13
+ from huggingface_hub import hf_hub_download # Hugging Face Hubμ—μ„œ λͺ¨λΈμ„ λ‹€μš΄λ‘œλ“œν•˜κΈ° μœ„ν•œ ν•¨μˆ˜μž…λ‹ˆλ‹€.
 
14
 
15
+ # PDF λ¬Έμ„œλ‘œλΆ€ν„° ν…μŠ€νŠΈλ₯Ό μΆ”μΆœν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
16
  def get_pdf_text(pdf_docs):
17
+ temp_dir = tempfile.TemporaryDirectory() # μž„μ‹œ 디렉토리λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
18
+ temp_filepath = os.path.join(temp_dir.name, pdf_docs.name) # μž„μ‹œ 파일 경둜λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
19
+ with open(temp_filepath, "wb") as f: # μž„μ‹œ νŒŒμΌμ„ λ°”μ΄λ„ˆλ¦¬ μ“°κΈ° λͺ¨λ“œλ‘œ μ—½λ‹ˆλ‹€.
20
+ f.write(pdf_docs.getvalue()) # PDF λ¬Έμ„œμ˜ λ‚΄μš©μ„ μž„μ‹œ νŒŒμΌμ— μ”λ‹ˆλ‹€.
21
+ pdf_loader = PyPDFLoader(temp_filepath) # PyPDFLoaderλ₯Ό μ‚¬μš©ν•΄ PDFλ₯Ό λ‘œλ“œν•©λ‹ˆλ‹€.
22
+ pdf_doc = pdf_loader.load() # ν…μŠ€νŠΈλ₯Ό μΆ”μΆœν•©λ‹ˆλ‹€.
23
+ return pdf_doc # μΆ”μΆœν•œ ν…μŠ€νŠΈλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
24
 
25
 
26
  def get_text_file(docs):
27
+ temp_dir = tempfile.TemporaryDirectory()
28
+ temp_filepath = os.path.join(temp_dir.name, docs.name)
29
+ with open(temp_filepath, "wb") as f:
30
+ f.write(docs.getvalue())
31
+ text_loader = TextLoader(temp_filepath)
32
+ text_doc = text_loader.load()
33
+ return text_doc
34
 
35
 
36
  def get_csv_file(docs):
37
+ temp_dir = tempfile.TemporaryDirectory()
38
+ temp_filepath = os.path.join(temp_dir.name, docs.name)
39
+ with open(temp_filepath, "wb") as f:
40
+ f.write(docs.getvalue())
41
+ csv_loader = CSVLoader(temp_filepath)
42
+ csv_doc = csv_loader.load()
43
+ return csv_doc
 
44
 
45
  def get_json_file(docs):
46
+ temp_dir = tempfile.TemporaryDirectory()
47
+ temp_filepath = os.path.join(temp_dir.name, docs.name)
48
+ with open(temp_filepath, "wb") as f:
49
+ f.write(docs.getvalue())
50
+ json_loader = JSONLoader(temp_filepath,
51
  jq_schema='.scans[].relationships',
52
  text_content=False)
53
+ json_doc = json_loader.load()
 
 
54
 
55
+ return json_doc
56
 
57
+ # λ¬Έμ„œλ“€μ„ μ²˜λ¦¬ν•˜μ—¬ ν…μŠ€νŠΈ 청크둜 λ‚˜λˆ„λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
58
  def get_text_chunks(documents):
59
  text_splitter = RecursiveCharacterTextSplitter(
60
+ chunk_size=1000, # 청크의 크기λ₯Ό μ§€μ •ν•©λ‹ˆλ‹€.
61
+ chunk_overlap=200, # 청크 μ‚¬μ΄μ˜ 쀑볡을 μ§€μ •ν•©λ‹ˆλ‹€.
62
+ length_function=len # ν…μŠ€νŠΈμ˜ 길이λ₯Ό μΈ‘μ •ν•˜λŠ” ν•¨μˆ˜λ₯Ό μ§€μ •ν•©λ‹ˆλ‹€.
63
  )
64
 
65
+ documents = text_splitter.split_documents(documents) # λ¬Έμ„œλ“€μ„ 청크둜 λ‚˜λˆ•λ‹ˆλ‹€.
66
+ return documents # λ‚˜λˆˆ 청크λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
67
 
68
 
69
+ # ν…μŠ€νŠΈ μ²­ν¬λ“€λ‘œλΆ€ν„° 벑터 μŠ€ν† μ–΄λ₯Ό μƒμ„±ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
70
  def get_vectorstore(text_chunks):
71
+ # μ›ν•˜λŠ” μž„λ² λ”© λͺ¨λΈμ„ λ‘œλ“œν•©λ‹ˆλ‹€.
72
  embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L12-v2',
73
+ model_kwargs={'device': 'cpu'}) # μž„λ² λ”© λͺ¨λΈμ„ μ„€μ •ν•©λ‹ˆλ‹€.
74
+ vectorstore = FAISS.from_documents(text_chunks, embeddings) # FAISS 벑터 μŠ€ν† μ–΄λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
75
+ return vectorstore # μƒμ„±λœ 벑터 μŠ€ν† μ–΄λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
76
 
77
 
78
  def get_conversation_chain(vectorstore):
 
84
  n_ctx=4086,
85
  input={"temperature": 0.75, "max_length": 2000, "top_p": 1},
86
  verbose=True, )
87
+ # λŒ€ν™” 기둝을 μ €μž₯ν•˜κΈ° μœ„ν•œ λ©”λͺ¨λ¦¬λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
88
  memory = ConversationBufferMemory(
89
  memory_key='chat_history', return_messages=True)
90
+ # λŒ€ν™” 검색 체인을 μƒμ„±ν•©λ‹ˆλ‹€.
91
  conversation_chain = ConversationalRetrievalChain.from_llm(
92
  llm=llm,
93
  retriever=vectorstore.as_retriever(),
94
  memory=memory
95
  )
96
+ return conversation_chain # μƒμ„±λœ λŒ€ν™” 체인을 λ°˜ν™˜ν•©λ‹ˆλ‹€.
 
97
 
98
+ # μ‚¬μš©μž μž…λ ₯을 μ²˜λ¦¬ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
99
  def handle_userinput(user_question):
100
  print('user_question => ', user_question)
101
+ # λŒ€ν™” 체인을 μ‚¬μš©ν•˜μ—¬ μ‚¬μš©μž μ§ˆλ¬Έμ— λŒ€ν•œ 응닡을 μƒμ„±ν•©λ‹ˆλ‹€.
102
  response = st.session_state.conversation({'question': user_question})
103
+ # λŒ€ν™” 기둝을 μ €μž₯ν•©λ‹ˆλ‹€.
104
  st.session_state.chat_history = response['chat_history']
105
 
106
  for i, message in enumerate(st.session_state.chat_history):
 
164
 
165
 
166
  if __name__ == '__main__':
167
+ main()