Update src/streamlit_app.py
Browse files- src/streamlit_app.py +258 -213
src/streamlit_app.py
CHANGED
|
@@ -1,214 +1,259 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from dotenv import load_dotenv
|
| 3 |
-
# from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
|
| 4 |
-
# from langchain.vectorstores import FAISS
|
| 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 |
-
# ν
μ€νΈ μ€ν리ν°
|
| 12 |
-
from langchain_text_splitters import CharacterTextSplitter, RecursiveCharacterTextSplitter
|
| 13 |
-
|
| 14 |
-
# 벑ν°μ€ν μ΄/μλ² λ©/LLM
|
| 15 |
-
from langchain_community.vectorstores import FAISS
|
| 16 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 17 |
-
|
| 18 |
-
#
|
| 19 |
-
from langchain_community.document_loaders.pdf import PyPDFLoader
|
| 20 |
-
from langchain_community.document_loaders.text import TextLoader
|
| 21 |
-
from langchain_community.document_loaders.csv_loader import CSVLoader
|
| 22 |
-
from langchain_community.document_loaders.json_loader import JSONLoader
|
| 23 |
-
import tempfile # μμ νμΌμ μμ±νκΈ° μν λΌμ΄λΈλ¬λ¦¬μ
λλ€.
|
| 24 |
-
import os
|
| 25 |
-
import json
|
| 26 |
-
from langchain.docstore.document import Document
|
| 27 |
-
from langchain_groq import ChatGroq
|
| 28 |
-
|
| 29 |
-
# PDF λ¬Έμλ‘λΆν° ν
μ€νΈλ₯Ό μΆμΆνλ ν¨μμ
λλ€.
|
| 30 |
-
def get_pdf_text(pdf_docs):
|
| 31 |
-
temp_dir = tempfile.TemporaryDirectory() # μμ λλ ν 리λ₯Ό μμ±ν©λλ€.
|
| 32 |
-
temp_filepath = os.path.join(temp_dir.name, pdf_docs.name) # μμ νμΌ κ²½λ‘λ₯Ό μμ±ν©λλ€.
|
| 33 |
-
with open(temp_filepath, "wb") as f: # μμ νμΌμ λ°μ΄λ리 μ°κΈ° λͺ¨λλ‘ μ½λλ€.
|
| 34 |
-
f.write(pdf_docs.getvalue()) # PDF λ¬Έμμ λ΄μ©μ μμ νμΌμ μλλ€.
|
| 35 |
-
pdf_loader = PyPDFLoader(temp_filepath) # PyPDFLoaderλ₯Ό μ¬μ©ν΄ PDFλ₯Ό λ‘λν©λλ€.
|
| 36 |
-
pdf_doc = pdf_loader.load() # ν
μ€νΈλ₯Ό μΆμΆν©λλ€.
|
| 37 |
-
return pdf_doc # μΆμΆν ν
μ€νΈλ₯Ό λ°νν©λλ€.
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
def get_text_file(docs):
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
)
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
#
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
main()
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
# from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
|
| 4 |
+
# from langchain.vectorstores import FAISS
|
| 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 |
+
# ν
μ€νΈ μ€ν리ν°
|
| 12 |
+
from langchain_text_splitters import CharacterTextSplitter, RecursiveCharacterTextSplitter
|
| 13 |
+
|
| 14 |
+
# 벑ν°μ€ν μ΄/μλ² λ©/LLM
|
| 15 |
+
from langchain_community.vectorstores import FAISS
|
| 16 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 17 |
+
|
| 18 |
+
# λ‘οΏ½οΏ½οΏ½λ€ (pebblo/pwd λλ €μ€μ§ μκ² μλΈλͺ¨λλ‘)
|
| 19 |
+
from langchain_community.document_loaders.pdf import PyPDFLoader
|
| 20 |
+
from langchain_community.document_loaders.text import TextLoader
|
| 21 |
+
from langchain_community.document_loaders.csv_loader import CSVLoader
|
| 22 |
+
from langchain_community.document_loaders.json_loader import JSONLoader
|
| 23 |
+
import tempfile # μμ νμΌμ μμ±νκΈ° μν λΌμ΄λΈλ¬λ¦¬μ
λλ€.
|
| 24 |
+
import os
|
| 25 |
+
import json
|
| 26 |
+
from langchain.docstore.document import Document
|
| 27 |
+
from langchain_groq import ChatGroq
|
| 28 |
+
|
| 29 |
+
# PDF λ¬Έμλ‘λΆν° ν
μ€νΈλ₯Ό μΆμΆνλ ν¨μμ
λλ€.
|
| 30 |
+
def get_pdf_text(pdf_docs):
|
| 31 |
+
temp_dir = tempfile.TemporaryDirectory() # μμ λλ ν 리λ₯Ό μμ±ν©λλ€.
|
| 32 |
+
temp_filepath = os.path.join(temp_dir.name, pdf_docs.name) # μμ νμΌ κ²½λ‘λ₯Ό μμ±ν©λλ€.
|
| 33 |
+
with open(temp_filepath, "wb") as f: # μμ νμΌμ λ°μ΄λ리 μ°κΈ° λͺ¨λλ‘ μ½λλ€.
|
| 34 |
+
f.write(pdf_docs.getvalue()) # PDF λ¬Έμμ λ΄μ©μ μμ νμΌμ μλλ€.
|
| 35 |
+
pdf_loader = PyPDFLoader(temp_filepath) # PyPDFLoaderλ₯Ό μ¬μ©ν΄ PDFλ₯Ό λ‘λν©λλ€.
|
| 36 |
+
pdf_doc = pdf_loader.load() # ν
μ€νΈλ₯Ό μΆμΆν©λλ€.
|
| 37 |
+
return pdf_doc # μΆμΆν ν
μ€νΈλ₯Ό λ°νν©λλ€.
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def get_text_file(docs):
|
| 41 |
+
# μ
λ‘λλ νμΌμ μμ λλ ν 리μ μ μ₯
|
| 42 |
+
temp_dir = tempfile.TemporaryDirectory()
|
| 43 |
+
temp_filepath = os.path.join(temp_dir.name, docs.name)
|
| 44 |
+
with open(temp_filepath, "wb") as f:
|
| 45 |
+
f.write(docs.getvalue())
|
| 46 |
+
# TextLoaderλ‘ λΆλ¬μ€κΈ°
|
| 47 |
+
text_loader = TextLoader(temp_filepath, encoding="utf-8")
|
| 48 |
+
text_doc = text_loader.load()
|
| 49 |
+
return text_doc
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def get_csv_file(docs):
|
| 53 |
+
# μ
λ‘λλ νμΌμ μμ λλ ν 리μ μ μ₯
|
| 54 |
+
temp_dir = tempfile.TemporaryDirectory()
|
| 55 |
+
temp_filepath = os.path.join(temp_dir.name, docs.name)
|
| 56 |
+
with open(temp_filepath, "wb") as f:
|
| 57 |
+
f.write(docs.getvalue())
|
| 58 |
+
# CSVLoaderλ‘ λΆλ¬μ€κΈ°
|
| 59 |
+
csv_loader = CSVLoader(temp_filepath, encoding="utf-8")
|
| 60 |
+
csv_doc = csv_loader.load()
|
| 61 |
+
return csv_doc
|
| 62 |
+
|
| 63 |
+
# def get_json_file(docs):
|
| 64 |
+
# temp_dir = tempfile.TemporaryDirectory()
|
| 65 |
+
# temp_filepath = os.path.join(temp_dir.name, docs.name)
|
| 66 |
+
# with open(temp_filepath, "wb") as f:
|
| 67 |
+
# f.write(docs.getvalue())
|
| 68 |
+
# json_loader = JSONLoader(temp_filepath,
|
| 69 |
+
# jq_schema='.scans[].relationships',
|
| 70 |
+
# text_content=False)
|
| 71 |
+
#
|
| 72 |
+
# json_doc = json_loader.load()
|
| 73 |
+
# # print('json_doc = ',json_doc)
|
| 74 |
+
# return json_doc
|
| 75 |
+
|
| 76 |
+
def get_json_file(file) -> list[Document]:
|
| 77 |
+
# Streamlit UploadedFile -> str
|
| 78 |
+
raw = file.getvalue().decode("utf-8", errors="ignore")
|
| 79 |
+
data = json.loads(raw)
|
| 80 |
+
|
| 81 |
+
docs = []
|
| 82 |
+
|
| 83 |
+
# μμ jq κ²½λ‘κ° '.scans[].relationships'μλ€λ©΄, λμΌν μλ―Έλ‘ νμ±:
|
| 84 |
+
# μ‘΄μ¬νλ©΄ κ·Έκ²λ§ λ½κ³ , μμΌλ©΄ ν΅μΌλ‘ λ¬Έμν
|
| 85 |
+
def add_doc(x):
|
| 86 |
+
docs.append(Document(page_content=json.dumps(x, ensure_ascii=False)))
|
| 87 |
+
|
| 88 |
+
if isinstance(data, dict) and "scans" in data and isinstance(data["scans"], list):
|
| 89 |
+
for s in data["scans"]:
|
| 90 |
+
rels = s.get("relationships", [])
|
| 91 |
+
if isinstance(rels, list) and rels:
|
| 92 |
+
for r in rels:
|
| 93 |
+
add_doc(r)
|
| 94 |
+
if not docs: # κ·Έλλ λͺ» λ½μμΌλ©΄ μ 체λ₯Ό νλλ‘
|
| 95 |
+
add_doc(data)
|
| 96 |
+
elif isinstance(data, list):
|
| 97 |
+
for item in data:
|
| 98 |
+
add_doc(item)
|
| 99 |
+
else:
|
| 100 |
+
add_doc(data)
|
| 101 |
+
|
| 102 |
+
return docs
|
| 103 |
+
|
| 104 |
+
# λ¬Έμλ€μ μ²λ¦¬νμ¬ ν
μ€νΈ μ²ν¬λ‘ λλλ ν¨μμ
λλ€.
|
| 105 |
+
def get_text_chunks(documents):
|
| 106 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 107 |
+
chunk_size=1000, # μ²ν¬μ ν¬κΈ°λ₯Ό μ§μ ν©λλ€.
|
| 108 |
+
chunk_overlap=200, # μ²ν¬ μ¬μ΄μ μ€λ³΅μ μ§μ ν©λλ€.
|
| 109 |
+
length_function=len # ν
μ€νΈμ κΈΈμ΄λ₯Ό μΈ‘μ νλ ν¨μλ₯Ό μ§μ ν©λλ€.
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
documents = text_splitter.split_documents(documents) # λ¬Έμλ€μ μ²ν¬λ‘ λλλλ€.
|
| 113 |
+
return documents # λλ μ²ν¬λ₯Ό λ°νν©λλ€.
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
# ν
μ€νΈ μ²ν¬λ€λ‘λΆν° λ²‘ν° μ€ν μ΄λ₯Ό μμ±νλ ν¨μμ
λλ€.
|
| 117 |
+
def get_vectorstore(text_chunks):
|
| 118 |
+
# μνλ μλ² λ© λͺ¨λΈμ λ‘λν©λλ€.
|
| 119 |
+
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L12-v2',
|
| 120 |
+
model_kwargs={'device': 'cpu'}) # μλ² λ© λͺ¨λΈμ μ€μ ν©λλ€.
|
| 121 |
+
vectorstore = FAISS.from_documents(text_chunks, embeddings) # FAISS λ²‘ν° μ€ν μ΄λ₯Ό μμ±ν©λλ€.
|
| 122 |
+
return vectorstore # μμ±λ λ²‘ν° μ€ν μ΄λ₯Ό λ°νν©λλ€.
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def get_conversation_chain(vectorstore):
|
| 126 |
+
# Groq LLM
|
| 127 |
+
llm = ChatGroq(
|
| 128 |
+
groq_api_key=os.environ.get("GROQ_API_KEY"),
|
| 129 |
+
model_name="llama-3.1-8b-instant",
|
| 130 |
+
temperature=0.75, # νμμ λ§κ² νλ
|
| 131 |
+
max_tokens=512 # 컨ν
μ€νΈ μ΄κ³Ό λ°©μ§μ© (νμμ μ‘°μ )
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
memory = ConversationBufferMemory(
|
| 135 |
+
memory_key="chat_history",
|
| 136 |
+
return_messages=True
|
| 137 |
+
)
|
| 138 |
+
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 139 |
+
|
| 140 |
+
conversation_chain = ConversationalRetrievalChain.from_llm(
|
| 141 |
+
llm=llm,
|
| 142 |
+
retriever=retriever,
|
| 143 |
+
memory=memory,
|
| 144 |
+
)
|
| 145 |
+
return conversation_chain
|
| 146 |
+
|
| 147 |
+
# μ¬μ©μ μ
λ ₯μ μ²λ¦¬νλ ν¨μμ
λλ€.
|
| 148 |
+
def handle_userinput(user_question):
|
| 149 |
+
print('user_question => ', user_question)
|
| 150 |
+
# λν 체μΈμ μ¬μ©νμ¬ μ¬μ©μ μ§λ¬Έμ λν μλ΅μ μμ±ν©λλ€.
|
| 151 |
+
response = st.session_state.conversation({'question': user_question})
|
| 152 |
+
# λν κΈ°λ‘μ μ μ₯ν©λλ€.
|
| 153 |
+
st.session_state.chat_history = response['chat_history']
|
| 154 |
+
|
| 155 |
+
for i, message in enumerate(st.session_state.chat_history):
|
| 156 |
+
if i % 2 == 0:
|
| 157 |
+
st.write(user_template.replace(
|
| 158 |
+
"{{MSG}}", message.content), unsafe_allow_html=True)
|
| 159 |
+
else:
|
| 160 |
+
st.write(bot_template.replace(
|
| 161 |
+
"{{MSG}}", message.content), unsafe_allow_html=True)
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def main():
|
| 165 |
+
load_dotenv()
|
| 166 |
+
st.set_page_config(page_title="Basic_RAG_AI_Chatbot_with_Llama",
|
| 167 |
+
page_icon=":books:")
|
| 168 |
+
st.write(css, unsafe_allow_html=True)
|
| 169 |
+
|
| 170 |
+
if "conversation" not in st.session_state:
|
| 171 |
+
st.session_state.conversation = None
|
| 172 |
+
if "chat_history" not in st.session_state:
|
| 173 |
+
st.session_state.chat_history = None
|
| 174 |
+
|
| 175 |
+
st.header("Basic_RAG_AI_Chatbot_with_Llama3 :books:")
|
| 176 |
+
user_question = st.text_input("Ask a question about your documents:")
|
| 177 |
+
if user_question:
|
| 178 |
+
handle_userinput(user_question)
|
| 179 |
+
|
| 180 |
+
with st.sidebar:
|
| 181 |
+
st.subheader("Your documents")
|
| 182 |
+
docs = st.file_uploader(
|
| 183 |
+
"Upload your Files here and click on 'Process'", accept_multiple_files=True)
|
| 184 |
+
if st.button("Process[PDF]"):
|
| 185 |
+
with st.spinner("Processing"):
|
| 186 |
+
# get pdf text
|
| 187 |
+
doc_list = []
|
| 188 |
+
for file in docs:
|
| 189 |
+
print('file - type : ', file.type)
|
| 190 |
+
if file.type in ['application/octet-stream', 'application/pdf']:
|
| 191 |
+
# file is .pdf
|
| 192 |
+
doc_list.extend(get_pdf_text(file))
|
| 193 |
+
else:
|
| 194 |
+
st.error("PDF νμΌμ΄ μλλλ€.")
|
| 195 |
+
if not doc_list:
|
| 196 |
+
st.error("μ²λ¦¬ κ°λ₯ν λ¬Έμλ₯Ό μ°Ύμ§ λͺ»νμ΅λλ€.")
|
| 197 |
+
st.stop()
|
| 198 |
+
|
| 199 |
+
text_chunks = get_text_chunks(doc_list)
|
| 200 |
+
vectorstore = get_vectorstore(text_chunks)
|
| 201 |
+
st.session_state.conversation = get_conversation_chain(vectorstore)
|
| 202 |
+
|
| 203 |
+
################## TXT, CSV λ²νΌ ꡬν
|
| 204 |
+
if st.button("Process[TXT]"):
|
| 205 |
+
with st.spinner("Processing"):
|
| 206 |
+
doc_list = []
|
| 207 |
+
for file in docs:
|
| 208 |
+
if file.type == 'text/plain':
|
| 209 |
+
doc_list.extend(get_text_file(file))
|
| 210 |
+
else:
|
| 211 |
+
st.error("TXT νμΌμ΄ μλλλ€.")
|
| 212 |
+
|
| 213 |
+
if not doc_list:
|
| 214 |
+
st.error("μ²λ¦¬ κ°λ₯ν TXT λ¬Έμλ₯Ό μ°Ύμ§ λͺ»νμ΅λλ€.")
|
| 215 |
+
st.stop()
|
| 216 |
+
|
| 217 |
+
text_chunks = get_text_chunks(doc_list)
|
| 218 |
+
vectorstore = get_vectorstore(text_chunks)
|
| 219 |
+
st.session_state.conversation = get_conversation_chain(vectorstore)
|
| 220 |
+
|
| 221 |
+
if st.button("Process[CSV]"):
|
| 222 |
+
with st.spinner("Processing"):
|
| 223 |
+
doc_list = []
|
| 224 |
+
for file in docs:
|
| 225 |
+
if file.type == 'text/csv':
|
| 226 |
+
doc_list.extend(get_csv_file(file))
|
| 227 |
+
else:
|
| 228 |
+
st.error("CSV νμΌμ΄ μλλλ€.")
|
| 229 |
+
|
| 230 |
+
if not doc_list:
|
| 231 |
+
st.error("μ²λ¦¬ κ°λ₯ν CSV λ¬Έμλ₯Ό μ°Ύμ§ λͺ»νμ΅λλ€.")
|
| 232 |
+
st.stop()
|
| 233 |
+
|
| 234 |
+
text_chunks = get_text_chunks(doc_list)
|
| 235 |
+
vectorstore = get_vectorstore(text_chunks)
|
| 236 |
+
st.session_state.conversation = get_conversation_chain(vectorstore)
|
| 237 |
+
|
| 238 |
+
if st.button("Process[JSON]"):
|
| 239 |
+
with st.spinner("Processing"):
|
| 240 |
+
# get txt text
|
| 241 |
+
doc_list = []
|
| 242 |
+
for file in docs:
|
| 243 |
+
print('file - type : ', file.type)
|
| 244 |
+
if file.type == 'application/json':
|
| 245 |
+
# file is .json
|
| 246 |
+
doc_list.extend(get_json_file(file))
|
| 247 |
+
else:
|
| 248 |
+
st.error("JSON νμΌμ΄ μλλλ€.")
|
| 249 |
+
if not doc_list:
|
| 250 |
+
st.error("μ²λ¦¬ κ°λ₯ν λ¬Έμλ₯Ό μ°Ύμ§ λͺ»νμ΅λλ€.")
|
| 251 |
+
st.stop()
|
| 252 |
+
|
| 253 |
+
text_chunks = get_text_chunks(doc_list)
|
| 254 |
+
vectorstore = get_vectorstore(text_chunks)
|
| 255 |
+
st.session_state.conversation = get_conversation_chain(vectorstore)
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
if __name__ == '__main__':
|
| 259 |
main()
|