Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
from langchain.text_splitter import (
|
4 |
+
CharacterTextSplitter,
|
5 |
+
RecursiveCharacterTextSplitter,
|
6 |
+
)
|
7 |
+
from langchain.vectorstores import FAISS
|
8 |
+
from langchain.embeddings import (
|
9 |
+
HuggingFaceEmbeddings,
|
10 |
+
) # General embeddings from HuggingFace models.
|
11 |
+
from langchain.memory import ConversationBufferMemory
|
12 |
+
from langchain.chains import ConversationalRetrievalChain
|
13 |
+
from htmlTemplates import css, bot_template, user_template
|
14 |
+
from langchain.llms import LlamaCpp # For loading transformer models.
|
15 |
+
from langchain.document_loaders import PyPDFLoader, TextLoader, JSONLoader, CSVLoader
|
16 |
+
import tempfile # μμ νμΌμ μμ±νκΈ° μν λΌμ΄λΈλ¬λ¦¬μ
λλ€.
|
17 |
+
import os
|
18 |
+
from huggingface_hub import hf_hub_download # Hugging Face Hubμμ λͺ¨λΈμ λ€μ΄λ‘λνκΈ° μν ν¨μμ
λλ€.
|
19 |
+
|
20 |
+
|
21 |
+
# PDF λ¬Έμλ‘λΆν° ν
μ€νΈλ₯Ό μΆμΆνλ ν¨μμ
λλ€.
|
22 |
+
def get_pdf_text(pdf_docs):
|
23 |
+
temp_dir = tempfile.TemporaryDirectory() # μμ λλ ν 리λ₯Ό μμ±ν©λλ€.
|
24 |
+
temp_filepath = os.path.join(temp_dir.name, pdf_docs.name) # μμ νμΌ κ²½λ‘λ₯Ό μμ±ν©λλ€.
|
25 |
+
with open(temp_filepath, "wb") as f: # μμ νμΌμ λ°μ΄λ리 μ°κΈ° λͺ¨λλ‘ μ½λλ€.
|
26 |
+
f.write(pdf_docs.getvalue()) # PDF λ¬Έμμ λ΄μ©μ μμ νμΌμ μλλ€.
|
27 |
+
pdf_loader = PyPDFLoader(temp_filepath) # PyPDFLoaderλ₯Ό μ¬μ©ν΄ PDFλ₯Ό λ‘λν©λλ€.
|
28 |
+
pdf_doc = pdf_loader.load() # ν
μ€νΈλ₯Ό μΆμΆν©λλ€.
|
29 |
+
return pdf_doc # μΆμΆν ν
μ€νΈλ₯Ό λ°νν©λλ€.
|
30 |
+
|
31 |
+
|
32 |
+
# κ³Όμ
|
33 |
+
# μλ ν
μ€νΈ μΆμΆ ν¨μλ₯Ό μμ±
|
34 |
+
def get_text_file(docs):
|
35 |
+
temp_dir = tempfile.TemporaryDirectory() # μμ λλ ν 리λ₯Ό μμ±ν©λλ€.
|
36 |
+
temp_filepath = os.path.join(temp_dir.name, docs.name) # μμ νμΌ κ²½λ‘λ₯Ό μμ±ν©λλ€.
|
37 |
+
with open(temp_filepath, "wb") as f: # μμ νμΌμ λ°μ΄λ리 μ°κΈ° λͺ¨λλ‘ μ½λλ€.
|
38 |
+
f.write(docs.getvalue()) # ν
μ€νΈ νμΌμ λ΄μ©μ μμ νμΌμ μλλ€.
|
39 |
+
text_loader = TextLoader(temp_filepath) # TextLoaderλ₯Ό μ¬μ©ν΄ ν
μ€νΈλ₯Ό λ‘λν©λλ€.
|
40 |
+
text_doc = text_loader.load() # ν
μ€νΈλ₯Ό μΆμΆν©λλ€.
|
41 |
+
return text_doc # μΆμΆν ν
μ€νΈλ₯Ό λ°νν©λλ€.
|
42 |
+
|
43 |
+
|
44 |
+
def get_csv_file(docs):
|
45 |
+
temp_dir = tempfile.TemporaryDirectory() # μμ λλ ν 리λ₯Ό μμ±ν©λλ€.
|
46 |
+
temp_filepath = os.path.join(temp_dir.name, docs.name) # μμ νμΌ κ²½λ‘λ₯Ό μμ±ν©λλ€.
|
47 |
+
with open(temp_filepath, "wb") as f: # μμ νμΌμ λ°μ΄λ리 μ°κΈ° λͺ¨λλ‘ μ½λλ€.
|
48 |
+
f.write(docs.getvalue()) # CSV νμΌμ λ΄μ©μ μμ νμΌμ μλλ€.
|
49 |
+
csv_loader = CSVLoader(temp_filepath) # CSVLoaderλ₯Ό μ¬μ©ν΄ CSVλ₯Ό λ‘λν©λλ€.
|
50 |
+
csv_doc = csv_loader.load() # ν
μ€νΈλ₯Ό μΆμΆν©λλ€.
|
51 |
+
return csv_doc # μΆμΆν ν
μ€νΈλ₯Ό λ°νν©λλ€.
|
52 |
+
|
53 |
+
|
54 |
+
def get_json_file(docs):
|
55 |
+
temp_dir = tempfile.TemporaryDirectory() # μμ λλ ν 리λ₯Ό μμ±ν©λλ€.
|
56 |
+
temp_filepath = os.path.join(temp_dir.name, docs.name) # μμ νμΌ κ²½λ‘λ₯Ό μμ±ν©λλ€.
|
57 |
+
with open(temp_filepath, "wb") as f: # μμ νμΌμ λ°μ΄λ리 μ°κΈ° λͺ¨λλ‘ μ½λλ€.
|
58 |
+
f.write(docs.getvalue()) # JSON νμΌμ λ΄μ©μ μμ νμΌμ μλλ€.
|
59 |
+
json_loader = JSONLoader(temp_filepath) # JSONLoaderλ₯Ό μ¬μ©ν΄ JSONμ λ‘λν©λλ€.
|
60 |
+
json_doc = json_loader.load() # ν
μ€νΈλ₯Ό μΆμΆν©λλ€.
|
61 |
+
return json_doc # μΆμΆν ν
μ€νΈλ₯Ό λ°νν©λλ€.
|
62 |
+
|
63 |
+
|
64 |
+
# λ¬Έμλ€μ μ²λ¦¬νμ¬ ν
μ€νΈ μ²ν¬λ‘ λλλ ν¨μμ
λλ€.
|
65 |
+
def get_text_chunks(documents):
|
66 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
67 |
+
chunk_size=1000, # μ²ν¬μ ν¬κΈ°λ₯Ό μ§μ ν©λλ€.
|
68 |
+
chunk_overlap=200, # μ²ν¬ μ¬μ΄μ μ€λ³΅μ μ§μ ν©λλ€.
|
69 |
+
length_function=len, # ν
μ€νΈμ κΈΈμ΄λ₯Ό μΈ‘μ νλ ν¨μλ₯Ό μ§μ ν©λλ€.
|
70 |
+
)
|
71 |
+
|
72 |
+
documents = text_splitter.split_documents(documents) # λ¬Έμλ€μ μ²ν¬λ‘ λλλλ€.
|
73 |
+
return documents # λλ μ²ν¬λ₯Ό λ°νν©λλ€.
|
74 |
+
|
75 |
+
|
76 |
+
# ν
μ€νΈ μ²ν¬λ€λ‘λΆν° λ²‘ν° μ€ν μ΄λ₯Ό μμ±νλ ν¨μμ
λλ€.
|
77 |
+
def get_vectorstore(text_chunks):
|
78 |
+
# μνλ μλ² λ© λͺ¨λΈμ λ‘λν©λλ€.
|
79 |
+
embeddings = HuggingFaceEmbeddings(
|
80 |
+
model_name="bert-base-multilingual-cased",
|
81 |
+
model_kwargs={"device": "cpu"},
|
82 |
+
) # μλ² λ© λͺ¨λΈμ μ€μ ν©λλ€.
|
83 |
+
vectorstore = FAISS.from_documents(text_chunks, embeddings) # FAISS λ²‘ν° μ€ν μ΄λ₯Ό μμ±ν©λλ€.
|
84 |
+
return vectorstore # μμ±λ λ²‘ν° μ€ν μ΄λ₯Ό λ°νν©λλ€.
|
85 |
+
|
86 |
+
|
87 |
+
def get_conversation_chain(vectorstore):
|
88 |
+
model_name_or_path = "TheBloke/Llama-2-7B-chat-GGUF"
|
89 |
+
model_basename = "llama-2-7b-chat.Q2_K.gguf"
|
90 |
+
model_path = hf_hub_download(repo_id=model_name_or_path, filename=model_basename)
|
91 |
+
|
92 |
+
llm = LlamaCpp(
|
93 |
+
model_path=model_path,
|
94 |
+
n_ctx=4086,
|
95 |
+
input={"temperature": 0.75, "max_length": 2000, "top_p": 1},
|
96 |
+
verbose=True,
|
97 |
+
)
|
98 |
+
# λν κΈ°λ‘μ μ μ₯νκΈ° μν λ©λͺ¨λ¦¬λ₯Ό μμ±ν©λλ€.
|
99 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
100 |
+
# λν κ²μ 체μΈμ μμ±ν©λλ€.
|
101 |
+
conversation_chain = ConversationalRetrievalChain.from_llm(
|
102 |
+
llm=llm, retriever=vectorstore.as_retriever(), memory=memory
|
103 |
+
)
|
104 |
+
return conversation_chain # μμ±λ λν 체μΈμ λ°νν©λλ€.
|
105 |
+
|
106 |
+
|
107 |
+
# μ¬μ©μ μ
λ ₯μ μ²λ¦¬νλ ν¨μμ
λλ€.
|
108 |
+
def handle_userinput(user_question):
|
109 |
+
print("user_question => ", user_question)
|
110 |
+
# λν 체μΈμ μ¬μ©νμ¬ μ¬μ©μ μ§λ¬Έμ λν μλ΅μ μμ±ν©λλ€.
|
111 |
+
response = st.session_state.conversation({"question": user_question})
|
112 |
+
# λν κΈ°λ‘μ μ μ₯ν©λλ€.
|
113 |
+
st.session_state.chat_history = response["chat_history"]
|
114 |
+
|
115 |
+
for i, message in enumerate(st.session_state.chat_history):
|
116 |
+
if i % 2 == 0:
|
117 |
+
st.write(
|
118 |
+
user_template.replace("{{MSG}}", message.content),
|
119 |
+
unsafe_allow_html=True,
|
120 |
+
)
|
121 |
+
else:
|
122 |
+
st.write(
|
123 |
+
bot_template.replace("{{MSG}}", message.content), unsafe_allow_html=True
|
124 |
+
)
|
125 |
+
|
126 |
+
|
127 |
+
def main():
|
128 |
+
load_dotenv()
|
129 |
+
# Streamlit UIλ₯Ό μ€μ ν©λλ€.
|
130 |
+
st.title("λ¬Έμμ λν΄ μ§λ¬Ένλ μ±ν
")
|
131 |
+
st.sidebar.title("λ¬Έμ μ
λ‘λ")
|
132 |
+
st.sidebar.text("μ¬κΈ°μ μ¬λ¬λΆμ λ¬Έμλ₯Ό μ
λ‘λνμΈμ.")
|
133 |
+
st.sidebar.text("PDF, TXT, CSV, JSON νμΌ νμμ μ§μν©λλ€.")
|
134 |
+
st.write(css, unsafe_allow_html=True)
|
135 |
+
|
136 |
+
if "conversation" not in st.session_state:
|
137 |
+
st.session_state.conversation = None
|
138 |
+
if "chat_history" not in st.session_state:
|
139 |
+
st.session_state.chat_history = None
|
140 |
+
|
141 |
+
st.header("λ¬Έμμ λν΄ μ§λ¬Ένλ μ±ν
")
|
142 |
+
user_question = st.text_input("λ¬Έμ μ λν΄μ μ§λ¬Έμ ν΄λ³΄μΈμ")
|
143 |
+
if user_question:
|
144 |
+
handle_userinput(user_question)
|
145 |
+
|
146 |
+
with st.sidebar:
|
147 |
+
st.subheader("Your documents")
|
148 |
+
docs = st.file_uploader("μ¬κΈ°μ λ¬Έμλ₯Ό μ
λ‘λνκ³ 'μ²λ¦¬'λ₯Ό ν΄λ¦νμΈμ", accept_multiple_files=True)
|
149 |
+
if st.button("μ²λ¦¬"):
|
150 |
+
with st.spinner("Processing"):
|
151 |
+
# get pdf text
|
152 |
+
doc_list = []
|
153 |
+
|
154 |
+
for file in docs:
|
155 |
+
print("file - type : ", file.type)
|
156 |
+
if file.type == "text/plain":
|
157 |
+
# file is .txt
|
158 |
+
doc_list.extend(get_text_file(file))
|
159 |
+
elif file.type in ["application/octet-stream", "application/pdf"]:
|
160 |
+
# file is .pdf
|
161 |
+
doc_list.extend(get_pdf_text(file))
|
162 |
+
elif file.type == "text/csv":
|
163 |
+
# file is .csv
|
164 |
+
doc_list.extend(get_csv_file(file))
|
165 |
+
elif file.type == "application/json":
|
166 |
+
# file is .json
|
167 |
+
doc_list.extend(get_json_file(file))
|
168 |
+
|
169 |
+
# get the text chunks
|
170 |
+
text_chunks = get_text_chunks(doc_list)
|
171 |
+
|
172 |
+
# create vector store
|
173 |
+
vectorstore = get_vectorstore(text_chunks)
|
174 |
+
|
175 |
+
# create conversation chain
|
176 |
+
st.session_state.conversation = get_conversation_chain(vectorstore)
|
177 |
+
|
178 |
+
|
179 |
+
if __name__ == "__main__":
|
180 |
+
main()
|