Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
|
| 36 |
def get_csv_file(docs):
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
def get_json_file(docs):
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
jq_schema='.scans[].relationships',
|
| 52 |
text_content=False)
|
| 53 |
-
|
| 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 |
-
#
|
| 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()
|