Spaces:
Sleeping
Sleeping
Commit
Β·
9989377
1
Parent(s):
1315281
edit app.py
Browse files
app.py
CHANGED
@@ -1,100 +1,90 @@
|
|
1 |
import streamlit as st
|
2 |
from dotenv import load_dotenv
|
|
|
3 |
from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
|
4 |
-
from langchain.
|
|
|
5 |
from langchain.embeddings import HuggingFaceEmbeddings # General embeddings from HuggingFace models.
|
|
|
6 |
from langchain.memory import ConversationBufferMemory
|
7 |
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 # Hugging Face Hubμμ λͺ¨λΈμ λ€μ΄λ‘λνκΈ° μν ν¨μμ
λλ€.
|
14 |
|
15 |
|
16 |
# PDF λ¬Έμλ‘λΆν° ν
μ€νΈλ₯Ό μΆμΆνλ ν¨μμ
λλ€.
|
17 |
def get_pdf_text(pdf_docs):
|
18 |
-
temp_dir = tempfile.TemporaryDirectory()
|
19 |
-
temp_filepath = os.path.join(temp_dir.name, pdf_docs.name)
|
20 |
with open(temp_filepath, "wb") as f: # μμ νμΌμ λ°μ΄λ리 μ°κΈ° λͺ¨λλ‘ μ½λλ€.
|
21 |
-
f.write(pdf_docs.getvalue())
|
22 |
-
pdf_loader = PyPDFLoader(temp_filepath)
|
23 |
-
pdf_doc = pdf_loader.load()
|
24 |
-
return pdf_doc
|
25 |
-
|
26 |
|
27 |
# κ³Όμ
|
28 |
# μλ ν
μ€νΈ μΆμΆ ν¨μλ₯Ό μμ±
|
|
|
|
|
29 |
def get_text_file(docs):
|
30 |
temp_dir = tempfile.TemporaryDirectory()
|
31 |
temp_filepath = os.path.join(temp_dir.name, docs.name)
|
32 |
-
|
33 |
-
with open(temp_filepath, 'wb') as f:
|
34 |
f.write(docs.getvalue())
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
return txt_doc
|
40 |
|
41 |
|
42 |
-
def get_csv_file(
|
43 |
temp_dir = tempfile.TemporaryDirectory()
|
44 |
-
temp_filepath = os.path.join(temp_dir.name,
|
45 |
-
|
46 |
-
|
47 |
-
f.write(docs.getvalue())
|
48 |
-
|
49 |
csv_loader = CSVLoader(temp_filepath)
|
50 |
csv_doc = csv_loader.load()
|
51 |
-
|
52 |
return csv_doc
|
53 |
|
54 |
|
55 |
-
def get_json_file(
|
56 |
temp_dir = tempfile.TemporaryDirectory()
|
57 |
-
temp_filepath = os.path.join(temp_dir.name,
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
json_loader = JSONLoader(temp_filepath, jq_scheama="μ¬μ")
|
63 |
json_doc = json_loader.load()
|
64 |
-
|
65 |
return json_doc
|
66 |
|
67 |
|
68 |
# λ¬Έμλ€μ μ²λ¦¬νμ¬ ν
μ€νΈ μ²ν¬λ‘ λλλ ν¨μμ
λλ€.
|
69 |
def get_text_chunks(documents):
|
70 |
text_splitter = RecursiveCharacterTextSplitter(
|
71 |
-
chunk_size=1000,
|
72 |
-
chunk_overlap=200,
|
73 |
-
length_function=len
|
74 |
)
|
75 |
|
76 |
-
documents = text_splitter.split_documents(documents)
|
77 |
-
return documents
|
78 |
|
79 |
|
80 |
# ν
μ€νΈ μ²ν¬λ€λ‘λΆν° λ²‘ν° μ€ν μ΄λ₯Ό μμ±νλ ν¨μμ
λλ€.
|
81 |
def get_vectorstore(text_chunks):
|
82 |
-
#
|
83 |
-
|
84 |
-
|
85 |
-
vectorstore = FAISS.from_documents(text_chunks, embeddings)
|
86 |
-
|
|
|
87 |
|
88 |
|
89 |
def get_conversation_chain(vectorstore):
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
llm = LlamaCpp(model_path=model_path,
|
95 |
-
n_ctx=4086,
|
96 |
-
input={"temperature": 0.75, "max_length": 2000, "top_p": 1},
|
97 |
-
verbose=True, )
|
98 |
# λν κΈ°λ‘μ μ μ₯νκΈ° μν λ©λͺ¨λ¦¬λ₯Ό μμ±ν©λλ€.
|
99 |
memory = ConversationBufferMemory(
|
100 |
memory_key='chat_history', return_messages=True)
|
@@ -104,12 +94,10 @@ def get_conversation_chain(vectorstore):
|
|
104 |
retriever=vectorstore.as_retriever(),
|
105 |
memory=memory
|
106 |
)
|
107 |
-
return conversation_chain
|
108 |
-
|
109 |
|
110 |
# μ¬μ©μ μ
λ ₯μ μ²λ¦¬νλ ν¨μμ
λλ€.
|
111 |
def handle_userinput(user_question):
|
112 |
-
print('user_question => ', user_question)
|
113 |
# λν 체μΈμ μ¬μ©νμ¬ μ¬μ©μ μ§λ¬Έμ λν μλ΅μ μμ±ν©λλ€.
|
114 |
response = st.session_state.conversation({'question': user_question})
|
115 |
# λν κΈ°λ‘μ μ μ₯ν©λλ€.
|
@@ -135,12 +123,16 @@ def main():
|
|
135 |
if "chat_history" not in st.session_state:
|
136 |
st.session_state.chat_history = None
|
137 |
|
138 |
-
st.header("Chat with multiple Files:")
|
139 |
user_question = st.text_input("Ask a question about your documents:")
|
140 |
if user_question:
|
141 |
handle_userinput(user_question)
|
142 |
|
143 |
with st.sidebar:
|
|
|
|
|
|
|
|
|
144 |
st.subheader("Your documents")
|
145 |
docs = st.file_uploader(
|
146 |
"Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
|
@@ -176,4 +168,4 @@ def main():
|
|
176 |
|
177 |
|
178 |
if __name__ == '__main__':
|
179 |
-
main()
|
|
|
1 |
import streamlit as st
|
2 |
from dotenv import load_dotenv
|
3 |
+
from PyPDF2 import PdfReader
|
4 |
from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
|
5 |
+
from langchain.embeddings import OpenAIEmbeddings, HuggingFaceInstructEmbeddings
|
6 |
+
from langchain.vectorstores import FAISS, Chroma
|
7 |
from langchain.embeddings import HuggingFaceEmbeddings # General embeddings from HuggingFace models.
|
8 |
+
from langchain.chat_models import ChatOpenAI
|
9 |
from langchain.memory import ConversationBufferMemory
|
10 |
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 |
# PDF λ¬Έμλ‘λΆν° ν
μ€νΈλ₯Ό μΆμΆνλ ν¨μμ
λλ€.
|
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 |
# μλ ν
μ€νΈ μΆμΆ ν¨μλ₯Ό μμ±
|
30 |
+
|
31 |
+
|
32 |
def get_text_file(docs):
|
33 |
temp_dir = tempfile.TemporaryDirectory()
|
34 |
temp_filepath = os.path.join(temp_dir.name, docs.name)
|
35 |
+
with open(temp_filepath, "wb") as f:
|
|
|
36 |
f.write(docs.getvalue())
|
37 |
+
text_loader = TextLoader(temp_filepath)
|
38 |
+
text_doc = text_loader.load()
|
39 |
+
return text_doc
|
|
|
|
|
40 |
|
41 |
|
42 |
+
def get_csv_file(csv_docs):
|
43 |
temp_dir = tempfile.TemporaryDirectory()
|
44 |
+
temp_filepath = os.path.join(temp_dir.name, csv_docs.name)
|
45 |
+
with open(temp_filepath, "wb") as f:
|
46 |
+
f.write(csv_docs.getvalue())
|
|
|
|
|
47 |
csv_loader = CSVLoader(temp_filepath)
|
48 |
csv_doc = csv_loader.load()
|
|
|
49 |
return csv_doc
|
50 |
|
51 |
|
52 |
+
def get_json_file(json_docs):
|
53 |
temp_dir = tempfile.TemporaryDirectory()
|
54 |
+
temp_filepath = os.path.join(temp_dir.name, json_docs.name)
|
55 |
+
with open(temp_filepath, "wb") as f:
|
56 |
+
f.write(json_docs.getvalue())
|
57 |
+
json_loader = JSONLoader(temp_filepath, jq_schema="ν
μ€νΈ")
|
|
|
|
|
58 |
json_doc = json_loader.load()
|
|
|
59 |
return json_doc
|
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 |
# ν
μ€νΈ μ²ν¬λ€λ‘λΆν° λ²‘ν° μ€ν μ΄λ₯Ό μμ±νλ ν¨μμ
λλ€.
|
75 |
def get_vectorstore(text_chunks):
|
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)
|
|
|
94 |
retriever=vectorstore.as_retriever(),
|
95 |
memory=memory
|
96 |
)
|
97 |
+
return conversation_chain
|
|
|
98 |
|
99 |
# μ¬μ©μ μ
λ ₯μ μ²λ¦¬νλ ν¨μμ
λλ€.
|
100 |
def handle_userinput(user_question):
|
|
|
101 |
# λν 체μΈμ μ¬μ©νμ¬ μ¬μ©μ μ§λ¬Έμ λν μλ΅μ μμ±ν©λλ€.
|
102 |
response = st.session_state.conversation({'question': user_question})
|
103 |
# λν κΈ°λ‘μ μ μ₯ν©λλ€.
|
|
|
123 |
if "chat_history" not in st.session_state:
|
124 |
st.session_state.chat_history = None
|
125 |
|
126 |
+
st.header("Chat with multiple Files :")
|
127 |
user_question = st.text_input("Ask a question about your documents:")
|
128 |
if user_question:
|
129 |
handle_userinput(user_question)
|
130 |
|
131 |
with st.sidebar:
|
132 |
+
openai_key = st.text_input("Paste your OpenAI API key (sk-...)")
|
133 |
+
if openai_key:
|
134 |
+
os.environ["OPENAI_API_KEY"] = openai_key
|
135 |
+
|
136 |
st.subheader("Your documents")
|
137 |
docs = st.file_uploader(
|
138 |
"Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
|
|
|
168 |
|
169 |
|
170 |
if __name__ == '__main__':
|
171 |
+
main()
|