Spaces:
Runtime error
Runtime error
Upload src/app.py with huggingface_hub
Browse files- src/app.py +54 -749
src/app.py
CHANGED
|
@@ -1,771 +1,76 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import tempfile
|
| 3 |
-
from operator import itemgetter
|
| 4 |
|
| 5 |
import streamlit as st
|
| 6 |
-
|
| 7 |
-
from langchain_community.document_loaders import PyMuPDFLoader
|
| 8 |
from langchain_community.vectorstores import FAISS
|
| 9 |
-
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 10 |
-
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
| 11 |
from langchain_core.prompts import ChatPromptTemplate
|
| 12 |
from langchain_core.output_parsers import StrOutputParser
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
#
|
| 18 |
-
st.
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# =========================================================
|
| 27 |
-
# 2. OpenAI API Key ํ์ธ
|
| 28 |
-
# =========================================================
|
| 29 |
-
if not os.environ.get("OPENAI_API_KEY"):
|
| 30 |
-
st.error(
|
| 31 |
-
"OPENAI_API_KEY๊ฐ ์ค์ ๋์ด ์์ง ์์ต๋๋ค. "
|
| 32 |
-
"Hugging Face Space์ Settings โ Secrets and variables์์ OPENAI_API_KEY๋ฅผ ๋ฑ๋กํด ์ฃผ์ธ์."
|
| 33 |
-
)
|
| 34 |
-
st.stop()
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
# =========================================================
|
| 38 |
-
# 3. CSS - AIVLE ๋ฏผํธํค + ๊น๋ํ ChatGPT ์คํ์ผ
|
| 39 |
-
# =========================================================
|
| 40 |
-
st.markdown(
|
| 41 |
-
"""
|
| 42 |
-
<style>
|
| 43 |
-
:root {
|
| 44 |
-
--mint: #00B8A9;
|
| 45 |
-
--mint-dark: #008C80;
|
| 46 |
-
--mint-soft: #E9FBF8;
|
| 47 |
-
--mint-light: #F6FFFD;
|
| 48 |
-
--text-main: #1F2937;
|
| 49 |
-
--text-sub: #6B7280;
|
| 50 |
-
--border: #D7F3EF;
|
| 51 |
-
--card: #FFFFFF;
|
| 52 |
-
--bg: #F8FAFC;
|
| 53 |
-
}
|
| 54 |
-
|
| 55 |
-
html, body, [class*="css"] {
|
| 56 |
-
font-family: "Pretendard", "Noto Sans KR", sans-serif;
|
| 57 |
-
}
|
| 58 |
-
|
| 59 |
-
.stApp {
|
| 60 |
-
background: var(--bg);
|
| 61 |
-
}
|
| 62 |
-
|
| 63 |
-
.block-container {
|
| 64 |
-
max-width: 980px;
|
| 65 |
-
padding-top: 2rem;
|
| 66 |
-
padding-bottom: 4rem;
|
| 67 |
-
}
|
| 68 |
-
|
| 69 |
-
[data-testid="stSidebar"] {
|
| 70 |
-
background: linear-gradient(180deg, #F6FFFD 0%, #FFFFFF 100%);
|
| 71 |
-
border-right: 1px solid var(--border);
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
.main-header {
|
| 75 |
-
background: linear-gradient(135deg, #FFFFFF 0%, #F6FFFD 100%);
|
| 76 |
-
border: 1px solid var(--border);
|
| 77 |
-
border-radius: 26px;
|
| 78 |
-
padding: 30px 32px;
|
| 79 |
-
margin-bottom: 24px;
|
| 80 |
-
box-shadow: 0 10px 28px rgba(0, 184, 169, 0.09);
|
| 81 |
-
}
|
| 82 |
-
|
| 83 |
-
.main-kicker {
|
| 84 |
-
color: var(--mint-dark);
|
| 85 |
-
font-size: 13px;
|
| 86 |
-
font-weight: 900;
|
| 87 |
-
letter-spacing: 0.06em;
|
| 88 |
-
margin-bottom: 8px;
|
| 89 |
-
}
|
| 90 |
-
|
| 91 |
-
.main-title {
|
| 92 |
-
font-size: 35px;
|
| 93 |
-
font-weight: 900;
|
| 94 |
-
color: var(--text-main);
|
| 95 |
-
margin-bottom: 8px;
|
| 96 |
-
line-height: 1.25;
|
| 97 |
-
}
|
| 98 |
-
|
| 99 |
-
.main-title span {
|
| 100 |
-
color: var(--mint-dark);
|
| 101 |
-
}
|
| 102 |
-
|
| 103 |
-
.main-subtitle {
|
| 104 |
-
color: var(--text-sub);
|
| 105 |
-
font-size: 15px;
|
| 106 |
-
line-height: 1.7;
|
| 107 |
-
}
|
| 108 |
-
|
| 109 |
-
.status-box {
|
| 110 |
-
background: var(--mint-light);
|
| 111 |
-
border: 1px solid var(--border);
|
| 112 |
-
border-radius: 18px;
|
| 113 |
-
padding: 18px 20px;
|
| 114 |
-
margin-bottom: 18px;
|
| 115 |
-
color: var(--text-main);
|
| 116 |
-
line-height: 1.8;
|
| 117 |
-
}
|
| 118 |
-
|
| 119 |
-
.guide-grid {
|
| 120 |
-
display: grid;
|
| 121 |
-
grid-template-columns: repeat(3, 1fr);
|
| 122 |
-
gap: 12px;
|
| 123 |
-
margin-bottom: 22px;
|
| 124 |
-
}
|
| 125 |
-
|
| 126 |
-
.guide-card {
|
| 127 |
-
background: #FFFFFF;
|
| 128 |
-
border: 1px solid var(--border);
|
| 129 |
-
border-radius: 18px;
|
| 130 |
-
padding: 17px;
|
| 131 |
-
box-shadow: 0 5px 16px rgba(0, 184, 169, 0.05);
|
| 132 |
-
}
|
| 133 |
-
|
| 134 |
-
.guide-step {
|
| 135 |
-
color: var(--mint-dark);
|
| 136 |
-
font-weight: 900;
|
| 137 |
-
font-size: 14px;
|
| 138 |
-
margin-bottom: 6px;
|
| 139 |
-
}
|
| 140 |
-
|
| 141 |
-
.guide-title {
|
| 142 |
-
color: var(--text-main);
|
| 143 |
-
font-weight: 850;
|
| 144 |
-
font-size: 15px;
|
| 145 |
-
margin-bottom: 5px;
|
| 146 |
-
}
|
| 147 |
-
|
| 148 |
-
.guide-desc {
|
| 149 |
-
color: var(--text-sub);
|
| 150 |
-
font-size: 13px;
|
| 151 |
-
line-height: 1.55;
|
| 152 |
-
}
|
| 153 |
-
|
| 154 |
-
.category-label {
|
| 155 |
-
color: var(--mint-dark);
|
| 156 |
-
font-weight: 850;
|
| 157 |
-
margin-bottom: 8px;
|
| 158 |
-
font-size: 14px;
|
| 159 |
-
letter-spacing: 0.01em;
|
| 160 |
-
}
|
| 161 |
-
|
| 162 |
-
.source-badge {
|
| 163 |
-
display: inline-block;
|
| 164 |
-
background: #E9FBF8;
|
| 165 |
-
color: #00796F;
|
| 166 |
-
border: 1px solid #BFEDE7;
|
| 167 |
-
border-radius: 999px;
|
| 168 |
-
padding: 6px 12px;
|
| 169 |
-
font-size: 13px;
|
| 170 |
-
font-weight: 800;
|
| 171 |
-
margin: 3px 5px 3px 0;
|
| 172 |
-
}
|
| 173 |
-
|
| 174 |
-
.stButton > button {
|
| 175 |
-
width: 100%;
|
| 176 |
-
border-radius: 14px;
|
| 177 |
-
border: 1px solid var(--mint);
|
| 178 |
-
background-color: #FFFFFF;
|
| 179 |
-
color: #00796F;
|
| 180 |
-
font-weight: 700;
|
| 181 |
-
padding: 0.68rem 0.8rem;
|
| 182 |
-
text-align: left;
|
| 183 |
-
}
|
| 184 |
-
|
| 185 |
-
.stButton > button:hover {
|
| 186 |
-
background-color: var(--mint-soft);
|
| 187 |
-
border-color: var(--mint-dark);
|
| 188 |
-
color: #005F56;
|
| 189 |
-
}
|
| 190 |
-
|
| 191 |
-
div[data-testid="stChatMessage"] {
|
| 192 |
-
background: #FFFFFF;
|
| 193 |
-
border: 1px solid #EEF2F7;
|
| 194 |
-
border-radius: 18px;
|
| 195 |
-
padding: 8px;
|
| 196 |
-
margin-bottom: 10px;
|
| 197 |
-
}
|
| 198 |
-
|
| 199 |
-
[data-testid="stFileUploader"] {
|
| 200 |
-
background: #FFFFFF;
|
| 201 |
-
border-radius: 16px;
|
| 202 |
-
padding: 8px;
|
| 203 |
-
}
|
| 204 |
-
|
| 205 |
-
.sidebar-title {
|
| 206 |
-
font-size: 24px;
|
| 207 |
-
font-weight: 900;
|
| 208 |
-
color: #006F65;
|
| 209 |
-
margin-bottom: 4px;
|
| 210 |
-
}
|
| 211 |
-
|
| 212 |
-
.sidebar-caption {
|
| 213 |
-
font-size: 13px;
|
| 214 |
-
color: #6B7280;
|
| 215 |
-
line-height: 1.5;
|
| 216 |
-
margin-bottom: 14px;
|
| 217 |
-
}
|
| 218 |
-
|
| 219 |
-
.sidebar-info {
|
| 220 |
-
background: #FFFFFF;
|
| 221 |
-
border: 1px solid #D7F3EF;
|
| 222 |
-
border-radius: 14px;
|
| 223 |
-
padding: 12px 14px;
|
| 224 |
-
margin-bottom: 10px;
|
| 225 |
-
font-size: 13px;
|
| 226 |
-
line-height: 1.55;
|
| 227 |
-
color: #374151;
|
| 228 |
-
}
|
| 229 |
-
|
| 230 |
-
.rag-help-box {
|
| 231 |
-
background: #FFFFFF;
|
| 232 |
-
border: 1px solid #D7F3EF;
|
| 233 |
-
border-radius: 14px;
|
| 234 |
-
padding: 13px 14px;
|
| 235 |
-
margin-top: 12px;
|
| 236 |
-
font-size: 13px;
|
| 237 |
-
line-height: 1.65;
|
| 238 |
-
color: #374151;
|
| 239 |
-
}
|
| 240 |
-
|
| 241 |
-
.rag-help-box b {
|
| 242 |
-
color: #006F65;
|
| 243 |
-
}
|
| 244 |
-
|
| 245 |
-
.history-caption {
|
| 246 |
-
color: #6B7280;
|
| 247 |
-
font-size: 12px;
|
| 248 |
-
line-height: 1.5;
|
| 249 |
-
}
|
| 250 |
-
|
| 251 |
-
@media (max-width: 900px) {
|
| 252 |
-
.guide-grid {
|
| 253 |
-
grid-template-columns: 1fr;
|
| 254 |
-
}
|
| 255 |
-
|
| 256 |
-
.main-title {
|
| 257 |
-
font-size: 28px;
|
| 258 |
-
}
|
| 259 |
-
}
|
| 260 |
-
</style>
|
| 261 |
-
""",
|
| 262 |
-
unsafe_allow_html=True
|
| 263 |
-
)
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
# =========================================================
|
| 267 |
-
# 4. ์ธ์
์ํ ์ด๊ธฐํ
|
| 268 |
-
# =========================================================
|
| 269 |
-
if "messages" not in st.session_state:
|
| 270 |
-
st.session_state.messages = []
|
| 271 |
-
|
| 272 |
-
if "rag_chain" not in st.session_state:
|
| 273 |
-
st.session_state.rag_chain = None
|
| 274 |
-
|
| 275 |
-
if "retriever" not in st.session_state:
|
| 276 |
-
st.session_state.retriever = None
|
| 277 |
-
|
| 278 |
-
if "rag_ready" not in st.session_state:
|
| 279 |
-
st.session_state.rag_ready = False
|
| 280 |
-
|
| 281 |
-
if "current_file_name" not in st.session_state:
|
| 282 |
-
st.session_state.current_file_name = None
|
| 283 |
-
|
| 284 |
-
if "chunk_count" not in st.session_state:
|
| 285 |
-
st.session_state.chunk_count = 0
|
| 286 |
-
|
| 287 |
-
if "selected_question" not in st.session_state:
|
| 288 |
-
st.session_state.selected_question = None
|
| 289 |
-
|
| 290 |
-
if "last_sources" not in st.session_state:
|
| 291 |
-
st.session_state.last_sources = []
|
| 292 |
-
|
| 293 |
-
if "chat_history" not in st.session_state:
|
| 294 |
-
st.session_state.chat_history = []
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
# =========================================================
|
| 298 |
-
# 5. ํจ์ ์ ์
|
| 299 |
-
# =========================================================
|
| 300 |
-
def save_uploaded_file(uploaded_file):
|
| 301 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 302 |
-
tmp.write(uploaded_file.getvalue())
|
| 303 |
-
return tmp.name
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
def format_docs(docs):
|
| 307 |
-
result = []
|
| 308 |
-
|
| 309 |
-
for i, doc in enumerate(docs, 1):
|
| 310 |
-
page = doc.metadata.get("page", None)
|
| 311 |
-
|
| 312 |
-
if page is not None:
|
| 313 |
-
page_text = f"{page + 1}ํ์ด์ง"
|
| 314 |
-
else:
|
| 315 |
-
page_text = "ํ์ด์ง ์ ๋ณด ์์"
|
| 316 |
-
|
| 317 |
-
result.append(
|
| 318 |
-
f"[์ถ์ฒ {i}: ๋ฐฑ์ {page_text}]\n{doc.page_content}"
|
| 319 |
-
)
|
| 320 |
-
|
| 321 |
-
return "\n\n".join(result)
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
def get_source_pages(docs):
|
| 325 |
-
pages = []
|
| 326 |
-
|
| 327 |
-
for doc in docs:
|
| 328 |
-
page = doc.metadata.get("page", None)
|
| 329 |
-
if page is not None:
|
| 330 |
-
pages.append(page + 1)
|
| 331 |
-
|
| 332 |
-
return sorted(list(set(pages)))
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
def get_page_text(docs):
|
| 336 |
-
pages = get_source_pages(docs)
|
| 337 |
-
|
| 338 |
-
if not pages:
|
| 339 |
-
return "์ฐธ๊ณ ํ์ด์ง: ํ์ด์ง ์ ๋ณด๋ฅผ ํ์ธํ ์ ์์ต๋๋ค."
|
| 340 |
-
|
| 341 |
-
return "์ฐธ๊ณ ํ์ด์ง: " + ", ".join([f"{p}p" for p in pages])
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
def build_rag_pipeline(pdf_path, chunk_size=800, chunk_overlap=150, k=4):
|
| 345 |
-
# 1. PDF ๋ก๋
|
| 346 |
-
loader = PyMuPDFLoader(pdf_path)
|
| 347 |
-
docs = loader.load()
|
| 348 |
-
|
| 349 |
-
# 2. ํ
์คํธ ๋ถํ
|
| 350 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
| 351 |
-
chunk_size=chunk_size,
|
| 352 |
-
chunk_overlap=chunk_overlap
|
| 353 |
-
)
|
| 354 |
-
split_docs = text_splitter.split_documents(docs)
|
| 355 |
-
|
| 356 |
-
# 3. ์๋ฒ ๋ฉ
|
| 357 |
-
embeddings = OpenAIEmbeddings(
|
| 358 |
-
model="text-embedding-3-small"
|
| 359 |
-
)
|
| 360 |
-
|
| 361 |
-
# 4. ๋ฒกํฐDB ์์ฑ
|
| 362 |
-
vectorstore = FAISS.from_documents(
|
| 363 |
-
documents=split_docs,
|
| 364 |
-
embedding=embeddings
|
| 365 |
-
)
|
| 366 |
-
|
| 367 |
-
# 5. Retriever ๊ตฌ์ฑ
|
| 368 |
-
retriever = vectorstore.as_retriever(
|
| 369 |
-
search_type="similarity",
|
| 370 |
-
search_kwargs={"k": k}
|
| 371 |
-
)
|
| 372 |
-
|
| 373 |
-
# 6. LLM
|
| 374 |
-
llm = ChatOpenAI(
|
| 375 |
-
model="gpt-4o-mini",
|
| 376 |
-
temperature=0
|
| 377 |
-
)
|
| 378 |
-
|
| 379 |
-
# 7. ํ๋กฌํํธ
|
| 380 |
-
prompt = ChatPromptTemplate.from_template(
|
| 381 |
-
"""
|
| 382 |
-
๋น์ ์ KT AIVLE School์ ๋จผ์ ์๋ฃํ '์ ๋ฐฐ ์์ด๋ธ๋ฌ'์
๋๋ค.
|
| 383 |
-
|
| 384 |
-
์๋ [๋ฐฑ์ ๋ด์ฉ]์ ๋ฐํ์ผ๋ก ํ๋ฐฐ ๊ต์ก์์ ์ง๋ฌธ์ ๋ต๋ณํ์ธ์.
|
| 385 |
-
|
| 386 |
-
๋ต๋ณ ๋งํฌ:
|
| 387 |
-
- ๋ฑ๋ฑํ ์๋ด๋ฌธ์ฒ๋ผ ๋งํ์ง ๋ง๊ณ , ์ ๋ฐฐ๊ฐ ํ๋ฐฐ์๊ฒ ์ง์ ์กฐ์ธํ๋ฏ ๋งํ์ธ์.
|
| 388 |
-
- "๋๋ ์ฒ์์๋ ~ํ์ด", "๋ด ๊ฒฝํ์ ~๊ฐ ์ค์ํ์ด", "ํ๋ฐฐ ์
์ฅ์์๋ ~๋ถํฐ ํ๋ฉด ์ข์"์ฒ๋ผ ์์ฐ์ค๋ฝ๊ฒ ๋งํ์ธ์.
|
| 389 |
-
- ๋จ, ๋ฌธ์์ ์๋ ๋ด์ฉ์ ์ง์ด๋ด๋ฉด ์ ๋ฉ๋๋ค.
|
| 390 |
-
- ๋ฐฑ์์ ๊ทผ๊ฑฐ๊ฐ ์๋ ๋ด์ฉ์ "๋ฐฑ์ nํ์ด์ง์ ๋ฐ๋ฅด๏ฟฝ๏ฟฝ"์ฒ๋ผ ํ์ด์ง๋ฅผ ์์ฐ์ค๋ฝ๊ฒ ์ธ๊ธํ์ธ์.
|
| 391 |
-
- ๋ฌธ์์์ ํ์ธํ ์ ์๋ ๋ด์ฉ์ ๋ฐ๋์ ์๋ ๋ฌธ์ฅ์ผ๋ก ์๋ดํ์ธ์.
|
| 392 |
-
"๋ฐฑ์์์ ํ์ธํ๊ธฐ ์ด๋ ต์ต๋๋ค. ๊ณต์ ํํ์ด์ง ๋๋ AIVLE-EDU ๊ณต์ง์ฌํญ์ ํ์ธํด ์ฃผ์ธ์."
|
| 393 |
-
|
| 394 |
-
๋ต๋ณ ๊ท์น:
|
| 395 |
-
1. ๋ฐ๋์ [๋ฐฑ์ ๋ด์ฉ]์ ๊ทผ๊ฑฐํด์ ๋ต๋ณํ์ธ์.
|
| 396 |
-
2. ํต์ฌ ๋ด์ฉ์ ๋จผ์ ๋งํ๊ณ , ํ์ํ ๊ฒฝ์ฐ ํญ๋ชฉ๋ณ๋ก ์ ๋ฆฌํ์ธ์.
|
| 397 |
-
3. ์ถ๊ฒฐ, ์๋ฃ, ํ๋ จ์ฅ๋ ค๊ธ, ๋ชจ์ง ์ผ์ ์ฒ๋ผ ๋ณ๋ ๊ฐ๋ฅ์ฑ์ด ์๋ ๋ด์ฉ์ ๊ณต์ ๊ณต์ง ํ์ธ์ ํจ๊ป ์๋ดํ์ธ์.
|
| 398 |
-
4. ๋ต๋ณ ๋ง์ง๋ง์๋ ๋ฐ๋์ ์๋ ๋ฌธ์ฅ์ ํฌํจํ์ธ์.
|
| 399 |
-
"โป ๋ณธ ๋ต๋ณ์ ์
๋ก๋๋ AIVLE School ๋ฐฑ์๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์์ฑ๋์์ต๋๋ค."
|
| 400 |
-
|
| 401 |
-
[๋ฐฑ์ ๋ด์ฉ]
|
| 402 |
-
{context}
|
| 403 |
-
|
| 404 |
-
[์ง๋ฌธ]
|
| 405 |
-
{question}
|
| 406 |
-
|
| 407 |
-
[๋ต๋ณ]
|
| 408 |
-
"""
|
| 409 |
-
)
|
| 410 |
-
|
| 411 |
rag_chain = (
|
| 412 |
{
|
| 413 |
-
"context": itemgetter("
|
| 414 |
-
"
|
| 415 |
}
|
| 416 |
| prompt
|
| 417 |
| llm
|
| 418 |
| StrOutputParser()
|
| 419 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 420 |
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
def answer_question(question):
|
| 425 |
-
if not st.session_state.rag_ready:
|
| 426 |
-
return "๋จผ์ ์ผ์ชฝ AIVLE School ๋ฉ๋ด์์ ๋ฐฑ์ PDF๋ฅผ ์
๋ก๋ํด ์ฃผ์ธ์.", []
|
| 427 |
-
|
| 428 |
-
answer = st.session_state.rag_chain.invoke(
|
| 429 |
-
{
|
| 430 |
-
"question": question
|
| 431 |
-
}
|
| 432 |
-
)
|
| 433 |
-
|
| 434 |
-
source_docs = st.session_state.retriever.invoke(question)
|
| 435 |
-
page_text = get_page_text(source_docs)
|
| 436 |
-
|
| 437 |
-
final_answer = f"{answer}\n\n**{page_text}**"
|
| 438 |
-
|
| 439 |
-
return final_answer, source_docs
|
| 440 |
|
|
|
|
|
|
|
|
|
|
| 441 |
|
| 442 |
-
|
|
|
|
| 443 |
st.session_state.messages = []
|
| 444 |
-
st.session_state.last_sources = []
|
| 445 |
-
st.session_state.selected_question = None
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
def save_to_history(question, answer):
|
| 449 |
-
history_item = {
|
| 450 |
-
"question": question,
|
| 451 |
-
"answer": answer
|
| 452 |
-
}
|
| 453 |
-
|
| 454 |
-
st.session_state.chat_history.insert(0, history_item)
|
| 455 |
-
|
| 456 |
-
# ์ต๊ทผ 7๊ฐ๊น์ง๋ง ์ ์ง
|
| 457 |
-
st.session_state.chat_history = st.session_state.chat_history[:7]
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
# =========================================================
|
| 461 |
-
# 6. ์ฌ์ด๋๋ฐ
|
| 462 |
-
# =========================================================
|
| 463 |
-
with st.sidebar:
|
| 464 |
-
st.markdown(
|
| 465 |
-
"""
|
| 466 |
-
<div class="sidebar-title">๐ AIVLE School</div>
|
| 467 |
-
<div class="sidebar-caption">
|
| 468 |
-
AI ๊ธฐ๋ฐ ํ์ต Assistant
|
| 469 |
-
</div>
|
| 470 |
-
""",
|
| 471 |
-
unsafe_allow_html=True
|
| 472 |
-
)
|
| 473 |
-
|
| 474 |
-
uploaded_file = st.file_uploader(
|
| 475 |
-
"๋ฐฑ์ PDF ์
๋ก๋",
|
| 476 |
-
type=["pdf"],
|
| 477 |
-
help="AIVLE School ๋ฐฑ์ PDF๋ฅผ ์
๋ก๋ํด ์ฃผ์ธ์."
|
| 478 |
-
)
|
| 479 |
-
|
| 480 |
-
st.divider()
|
| 481 |
-
|
| 482 |
-
st.markdown("### ๋ฌธ์ ์ ๋ณด")
|
| 483 |
-
|
| 484 |
-
if st.session_state.current_file_name:
|
| 485 |
-
st.success("๋ฌธ์ ์
๋ก๋ ์๋ฃ")
|
| 486 |
-
st.markdown(
|
| 487 |
-
f"""
|
| 488 |
-
<div class="sidebar-info">
|
| 489 |
-
<b>๐ ํ์ฌ ๋ฌธ์</b><br>
|
| 490 |
-
{st.session_state.current_file_name}<br><br>
|
| 491 |
-
<b>๐งฉ ๋ฌธ์ ์ฒญํฌ ์</b><br>
|
| 492 |
-
{st.session_state.chunk_count}๊ฐ<br><br>
|
| 493 |
-
<b>๐ฌ ๋ต๋ณ ํ๋ฅด์๋</b><br>
|
| 494 |
-
์ ๋ฐฐ ์์ด๋ธ๋ฌ
|
| 495 |
-
</div>
|
| 496 |
-
""",
|
| 497 |
-
unsafe_allow_html=True
|
| 498 |
-
)
|
| 499 |
-
else:
|
| 500 |
-
st.info("์์ง ์
๋ก๋๋ ๋ฌธ์๊ฐ ์์ต๋๋ค.")
|
| 501 |
-
|
| 502 |
-
st.divider()
|
| 503 |
-
|
| 504 |
-
st.markdown("### ๋ํ ๊ด๋ฆฌ")
|
| 505 |
-
|
| 506 |
-
if st.button("๋ํ ์ด๊ธฐํ"):
|
| 507 |
-
reset_chat()
|
| 508 |
-
st.rerun()
|
| 509 |
-
|
| 510 |
-
if st.button("๋ฌธ์ ์ด๊ธฐํ"):
|
| 511 |
-
st.session_state.rag_chain = None
|
| 512 |
-
st.session_state.retriever = None
|
| 513 |
-
st.session_state.rag_ready = False
|
| 514 |
-
st.session_state.current_file_name = None
|
| 515 |
-
st.session_state.chunk_count = 0
|
| 516 |
-
reset_chat()
|
| 517 |
-
st.rerun()
|
| 518 |
-
|
| 519 |
-
st.divider()
|
| 520 |
-
|
| 521 |
-
st.markdown("### ์ต๊ทผ ๋ํ ํ์คํ ๋ฆฌ")
|
| 522 |
-
|
| 523 |
-
if st.session_state.chat_history:
|
| 524 |
-
for idx, item in enumerate(st.session_state.chat_history[:7]):
|
| 525 |
-
title = item["question"]
|
| 526 |
-
|
| 527 |
-
if len(title) > 28:
|
| 528 |
-
title = title[:28] + "..."
|
| 529 |
-
|
| 530 |
-
if st.button(title, key=f"history_{idx}"):
|
| 531 |
-
st.session_state.messages = [
|
| 532 |
-
{
|
| 533 |
-
"role": "user",
|
| 534 |
-
"content": item["question"]
|
| 535 |
-
},
|
| 536 |
-
{
|
| 537 |
-
"role": "assistant",
|
| 538 |
-
"content": item["answer"]
|
| 539 |
-
}
|
| 540 |
-
]
|
| 541 |
-
st.session_state.last_sources = []
|
| 542 |
-
st.rerun()
|
| 543 |
-
else:
|
| 544 |
-
st.caption("์์ง ์ ์ฅ๋ ๋ํ๊ฐ ์์ต๋๋ค.")
|
| 545 |
-
|
| 546 |
-
st.markdown(
|
| 547 |
-
"""
|
| 548 |
-
<div class="history-caption">
|
| 549 |
-
์ต๊ทผ ๋ํ๋ ์ต๋ 7๊ฐ๊น์ง ์ ์ฅ๋ฉ๋๋ค.
|
| 550 |
-
</div>
|
| 551 |
-
""",
|
| 552 |
-
unsafe_allow_html=True
|
| 553 |
-
)
|
| 554 |
-
|
| 555 |
-
if st.button("๋ํ ํ์คํ ๋ฆฌ ์ด๊ธฐํ"):
|
| 556 |
-
st.session_state.chat_history = []
|
| 557 |
-
st.rerun()
|
| 558 |
-
|
| 559 |
-
st.divider()
|
| 560 |
-
|
| 561 |
-
with st.expander("๊ณ ๊ธ RAG ์ค์ "):
|
| 562 |
-
chunk_size = st.slider("Chunk Size", 500, 1500, 800, 100)
|
| 563 |
-
chunk_overlap = st.slider("Chunk Overlap", 50, 300, 150, 50)
|
| 564 |
-
search_k = st.slider("๊ฒ์ ๋ฌธ์ ์", 2, 8, 4, 1)
|
| 565 |
-
|
| 566 |
-
st.markdown(
|
| 567 |
-
"""
|
| 568 |
-
<div class="rag-help-box">
|
| 569 |
-
<b>Chunk Size</b><br>
|
| 570 |
-
๋ฐฑ์ ๋ด์ฉ์ ํ ์กฐ๊ฐ๋น ์ผ๋ง๋ ํฌ๊ฒ ๋๋์ง ์ ํ๋ ๊ฐ์
๋๋ค.<br>
|
| 571 |
-
๊ฐ์ด ์์ผ๋ฉด ๋ ์ธ๋ฐํ๊ฒ ๊ฒ์ํ ์ ์์ง๋ง ๋ฌธ๋งฅ์ด ๋๊ธธ ์ ์๊ณ ,
|
| 572 |
-
๊ฐ์ด ํฌ๋ฉด ๋ฌธ๋งฅ์ ํ๋ถํ์ง๋ง ๊ด๋ จ ์๋ ๋ด์ฉ์ด ํจ๊ป ๋ค์ด๊ฐ ์ ์์ต๋๋ค.<br><br>
|
| 573 |
-
|
| 574 |
-
<b>Chunk Overlap</b><br>
|
| 575 |
-
๋ฌธ์ ์กฐ๊ฐ๋ผ๋ฆฌ ์๋ค ๋ด์ฉ์ ์ผ๋ง๋ ๊ฒน์น๊ฒ ํ ์ง ์ ํ๋ ๊ฐ์
๋๋ค.<br>
|
| 576 |
-
๋ฌธ์ฅ์ด ์ค๊ฐ์ ๋๊ธฐ๋ ๊ฒ์ ์ค์ฌ ๋ต๋ณ์ ๋ฌธ๋งฅ์ ์์ฐ์ค๋ฝ๊ฒ ์ ์งํ๋ ๋ฐ ๋์์ ์ค๋๋ค.<br><br>
|
| 577 |
-
|
| 578 |
-
<b>๊ฒ์ ๋ฌธ์ ์</b><br>
|
| 579 |
-
์ง๋ฌธ์ด ๋ค์ด์์ ๋ ๋ฐฑ์์์ ๊ด๋ จ ๋ฌธ์ ์กฐ๊ฐ์ ๋ช ๊ฐ ๊ฐ์ ธ์ ๋ต๋ณ์ ํ์ฉํ ์ง ์ ํ๋ ๊ฐ์
๋๋ค.<br>
|
| 580 |
-
๊ฐ์ด ์์ผ๋ฉด ๋ต๋ณ์ด ๊ฐ๊ฒฐํด์ง๊ณ , ๊ฐ์ด ํฌ๋ฉด ๋ ๋ง์ ๊ทผ๊ฑฐ๋ฅผ ์ฐธ๊ณ ํ์ง๋ง ๊ด๋ จ ์๋ ๋ด์ฉ์ด ์์ผ ์ ์์ต๋๋ค.<br><br>
|
| 581 |
-
|
| 582 |
-
<b>์ถ์ฒ ๊ธฐ๋ณธ๊ฐ</b><br>
|
| 583 |
-
Chunk Size 800, Chunk Overlap 150, ๊ฒ์ ๋ฌธ์ ์ 4๋ฅผ ๊ถ์ฅํฉ๋๋ค.
|
| 584 |
-
</div>
|
| 585 |
-
""",
|
| 586 |
-
unsafe_allow_html=True
|
| 587 |
-
)
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
# =========================================================
|
| 591 |
-
# 7. PDF ์
๋ก๋ ์ฒ๋ฆฌ
|
| 592 |
-
# =========================================================
|
| 593 |
-
if uploaded_file is not None:
|
| 594 |
-
if st.session_state.current_file_name != uploaded_file.name:
|
| 595 |
-
with st.spinner("๋ฐฑ์๋ฅผ ๋ถ์ํ๋ ์ค์
๋๋ค..."):
|
| 596 |
-
pdf_path = save_uploaded_file(uploaded_file)
|
| 597 |
-
|
| 598 |
-
rag_chain, retriever, chunk_count = build_rag_pipeline(
|
| 599 |
-
pdf_path=pdf_path,
|
| 600 |
-
chunk_size=chunk_size,
|
| 601 |
-
chunk_overlap=chunk_overlap,
|
| 602 |
-
k=search_k
|
| 603 |
-
)
|
| 604 |
-
|
| 605 |
-
st.session_state.rag_chain = rag_chain
|
| 606 |
-
st.session_state.retriever = retriever
|
| 607 |
-
st.session_state.chunk_count = chunk_count
|
| 608 |
-
st.session_state.current_file_name = uploaded_file.name
|
| 609 |
-
st.session_state.rag_ready = True
|
| 610 |
-
reset_chat()
|
| 611 |
-
|
| 612 |
-
st.success("๋ฐฑ์ ์
๋ก๋์ ๋ถ์์ด ์๋ฃ๋์์ต๋๋ค.")
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
# =========================================================
|
| 616 |
-
# 8. ๋ฉ์ธ ์ธํฐํ์ด์ค
|
| 617 |
-
# =========================================================
|
| 618 |
-
st.markdown(
|
| 619 |
-
"""
|
| 620 |
-
<div class="main-header">
|
| 621 |
-
<div class="main-kicker">AI LEARNING ASSISTANT</div>
|
| 622 |
-
<div class="main-title">๐ AIVLE School <span>ํ์ต ๋์ฐ๋ฏธ</span></div>
|
| 623 |
-
<div class="main-subtitle">
|
| 624 |
-
์ฌ๋ฌ๋ถ์ ๋์ ์ ์ง์ฌ์ผ๋ก ์์ํฉ๋๋ค.
|
| 625 |
-
</div>
|
| 626 |
-
</div>
|
| 627 |
-
""",
|
| 628 |
-
unsafe_allow_html=True
|
| 629 |
-
)
|
| 630 |
-
|
| 631 |
-
if not st.session_state.rag_ready:
|
| 632 |
-
st.markdown(
|
| 633 |
-
"""
|
| 634 |
-
<div class="status-box">
|
| 635 |
-
<b>์ฌ์ฉ ๋ฐฉ๋ฒ</b><br>
|
| 636 |
-
โ ์ผ์ชฝ ์ฌ์ด๋๋ฐ์์ AIVLE School ๋ฐฑ์ PDF๋ฅผ ์
๋ก๋ํ์ธ์.<br>
|
| 637 |
-
โก ์ถ์ฒ ์ง๋ฌธ์ ์ ํํ๊ฑฐ๋ ๊ถ๊ธํ ์ ์ ์ง์ ์
๋ ฅํ์ธ์.<br>
|
| 638 |
-
โข ๋ต๋ณ๊ณผ ํจ๊ป ์ฐธ๊ณ ํ ๋ฐฑ์ ํ์ด์ง๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
|
| 639 |
-
</div>
|
| 640 |
-
|
| 641 |
-
<div class="guide-grid">
|
| 642 |
-
<div class="guide-card">
|
| 643 |
-
<div class="guide-step">STEP 1</div>
|
| 644 |
-
<div class="guide-title">๋ฐฑ์ ์
๋ก๋</div>
|
| 645 |
-
<div class="guide-desc">AIVLE School ๋ฐฑ์ PDF๋ฅผ ์
๋ก๋ํ๋ฉด ๋ฌธ์๋ฅผ ์๋์ผ๋ก ๋ถ์ํฉ๋๋ค.</div>
|
| 646 |
-
</div>
|
| 647 |
-
<div class="guide-card">
|
| 648 |
-
<div class="guide-step">STEP 2</div>
|
| 649 |
-
<div class="guide-title">์ง๋ฌธ ์
๋ ฅ</div>
|
| 650 |
-
<div class="guide-desc">์ถ์ฒ ์ง๋ฌธ์ ๋๋ฅด๊ฑฐ๋ ์ถ๊ฒฐ, ํ๋ก์ ํธ, ์ทจ์
๊ด๋ จ ์ง๋ฌธ์ ์ง์ ์
๋ ฅํฉ๋๋ค.</div>
|
| 651 |
-
</div>
|
| 652 |
-
<div class="guide-card">
|
| 653 |
-
<div class="guide-step">STEP 3</div>
|
| 654 |
-
<div class="guide-title">๋ต๋ณ ํ์ธ</div>
|
| 655 |
-
<div class="guide-desc">๋ฐฑ์ ๊ธฐ๋ฐ ๋ต๋ณ๊ณผ ํจ๊ป ์ฐธ๊ณ ํ ํ์ด์ง๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.</div>
|
| 656 |
-
</div>
|
| 657 |
-
</div>
|
| 658 |
-
""",
|
| 659 |
-
unsafe_allow_html=True
|
| 660 |
-
)
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
# =========================================================
|
| 664 |
-
# 9. ์ถ์ฒ ์ง๋ฌธ
|
| 665 |
-
# =========================================================
|
| 666 |
-
st.markdown("## ์ถ์ฒ ์ง๋ฌธ")
|
| 667 |
-
|
| 668 |
-
st.markdown(
|
| 669 |
-
"""
|
| 670 |
-
<div class="category-label">
|
| 671 |
-
ํ์ต ยท ์ถ๊ฒฐ ยท ํ๋ก์ ํธ ยท ์ทจ์
์ค๋น
|
| 672 |
-
</div>
|
| 673 |
-
""",
|
| 674 |
-
unsafe_allow_html=True
|
| 675 |
-
)
|
| 676 |
-
|
| 677 |
-
recommended_questions = [
|
| 678 |
-
"๋น์ ๊ณต์๋ AIVLE School ๊ณผ์ ์ ๋ฐ๋ผ๊ฐ ์ ์๋์?",
|
| 679 |
-
"์๋ฆฌ ์ด์ ์ ์ด๋ป๊ฒ ํด์ผ ํ๋์?",
|
| 680 |
-
"์ถ์์ธ์ ์์ฒญ์ ์ด๋ป๊ฒ ํ๋์?",
|
| 681 |
-
"ํ๋ก์ ํธ๋ ์ด๋ป๊ฒ ์ค๋นํด์ผ ํ๋์?",
|
| 682 |
-
"DX์ปจ์คํดํธ ํธ๋์์๋ ์ด๋ค ์ญ๋์ด ์ค์ํ๊ฐ์?",
|
| 683 |
-
"๋น
ํ๋ก์ ํธ๋ ์ทจ์
์ค๋น์ ์ด๋ค ๋์์ด ๋๋์?",
|
| 684 |
-
"ํ๋ จ์ฅ๋ ค๊ธ์ ์ธ์ ์ง๊ธ๋๋์?",
|
| 685 |
-
"๊ต์ก ์๋ฃ๋ฅผ ๋ธ๋ก๊ทธ์ ์ฌ๋ ค๋ ๋๋์?"
|
| 686 |
-
]
|
| 687 |
-
|
| 688 |
-
cols = st.columns(2)
|
| 689 |
-
|
| 690 |
-
for i, question in enumerate(recommended_questions):
|
| 691 |
-
with cols[i % 2]:
|
| 692 |
-
if st.button(question, key=f"rec_question_{i}"):
|
| 693 |
-
st.session_state.selected_question = question
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
# =========================================================
|
| 697 |
-
# 10. ์ฑํ
์์ญ
|
| 698 |
-
# =========================================================
|
| 699 |
-
st.markdown("## AIVLE์๊ฒ ๋ฌผ์ด๋ณด๊ธฐ")
|
| 700 |
-
st.caption("์ถ๊ฒฐ, ์๋ฃ ๊ธฐ์ค, ํ๋ก์ ํธ ์ค๋น, ์ทจ์
์ ๋ต ๋ฑ AIVLE School๊ณผ ๊ด๋ จ๋ ์ง๋ฌธ์ ์
๋ ฅํด ๋ณด์ธ์.")
|
| 701 |
-
|
| 702 |
-
for msg in st.session_state.messages:
|
| 703 |
-
with st.chat_message(msg["role"]):
|
| 704 |
-
st.markdown(msg["content"])
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
user_input = st.chat_input("AIVLE School์ ๋ํด ๊ถ๊ธํ ์ ์ ์
๋ ฅํ์ธ์.")
|
| 708 |
-
|
| 709 |
-
if st.session_state.selected_question:
|
| 710 |
-
user_input = st.session_state.selected_question
|
| 711 |
-
st.session_state.selected_question = None
|
| 712 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 713 |
|
|
|
|
|
|
|
| 714 |
if user_input:
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
else:
|
| 718 |
-
st.session_state.messages.append(
|
| 719 |
-
{
|
| 720 |
-
"role": "user",
|
| 721 |
-
"content": user_input
|
| 722 |
-
}
|
| 723 |
-
)
|
| 724 |
-
|
| 725 |
-
with st.chat_message("user"):
|
| 726 |
-
st.markdown(user_input)
|
| 727 |
-
|
| 728 |
-
with st.chat_message("assistant"):
|
| 729 |
-
with st.spinner("๋ต๋ณ์ ์์ฑํ๋ ์ค์
๋๋ค..."):
|
| 730 |
-
answer, sources = answer_question(user_input)
|
| 731 |
-
st.markdown(answer)
|
| 732 |
-
|
| 733 |
-
st.session_state.messages.append(
|
| 734 |
-
{
|
| 735 |
-
"role": "assistant",
|
| 736 |
-
"content": answer
|
| 737 |
-
}
|
| 738 |
-
)
|
| 739 |
-
|
| 740 |
-
st.session_state.last_sources = sources
|
| 741 |
-
save_to_history(user_input, answer)
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
# =========================================================
|
| 745 |
-
# 11. ์ฐธ๊ณ ํ์ด์ง ๋ฐ ๊ฒ์ ๋ฌธ์
|
| 746 |
-
# =========================================================
|
| 747 |
-
if st.session_state.last_sources:
|
| 748 |
-
pages = get_source_pages(st.session_state.last_sources)
|
| 749 |
-
|
| 750 |
-
st.markdown("### ์ฐธ๊ณ ํ ๋ฐฑ์ ํ์ด์ง")
|
| 751 |
-
st.caption("๋ต๋ณ ์์ฑ์ ์ฌ์ฉ๋ ๋ฐฑ์ ๋ฌธ์ ์กฐ๊ฐ์ ํ์ด์ง์
๋๋ค.")
|
| 752 |
-
|
| 753 |
-
if pages:
|
| 754 |
-
badges = "".join(
|
| 755 |
-
[f"<span class='source-badge'>{p}p</span>" for p in pages]
|
| 756 |
-
)
|
| 757 |
-
st.markdown(badges, unsafe_allow_html=True)
|
| 758 |
-
else:
|
| 759 |
-
st.write("ํ์ด์ง ์ ๋ณด๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.")
|
| 760 |
-
|
| 761 |
-
with st.expander("๊ฒ์๋ ๋ฐฑ์ ๋ฌธ๋จ ๋ณด๊ธฐ"):
|
| 762 |
-
for i, doc in enumerate(st.session_state.last_sources, 1):
|
| 763 |
-
page = doc.metadata.get("page", None)
|
| 764 |
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
|
| 770 |
-
|
| 771 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
import streamlit as st
|
| 3 |
+
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
|
|
|
| 4 |
from langchain_community.vectorstores import FAISS
|
|
|
|
|
|
|
| 5 |
from langchain_core.prompts import ChatPromptTemplate
|
| 6 |
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
+
from langchain_core.messages import HumanMessage, AIMessage
|
| 8 |
+
from operator import itemgetter
|
| 9 |
|
| 10 |
+
# ๋ฐ์ดํฐ ๋ฐ ๋ฆฌํธ๋ฆฌ๋ฒ ๋ถ๋ฌ์ค๊ธฐ
|
| 11 |
+
FAISS_DIR = "faiss_index"
|
| 12 |
+
|
| 13 |
+
# ์ฒด์ธ ์บ์ฑ
|
| 14 |
+
@st.cache_resource
|
| 15 |
+
def get_chain():
|
| 16 |
+
s_msg = "๋น์ ์ ์ฃผ์ด์ง ๊ท์ ์ ๊ธฐ๋ฐ์ผ๋ก ๋ต๋ณํ๋ ๊ท์ ๋์ฐ๋ฏธ์
๋๋ค.\n\n[๊ท์ ]\n\n{context}"
|
| 17 |
+
h_msg = "{input}"
|
| 18 |
+
messages = [("system", s_msg), ("human", h_msg)]
|
| 19 |
+
prompt = ChatPromptTemplate.from_messages(messages)
|
| 20 |
+
llm = ChatOpenAI(model="gpt-4o-mini", streaming=True, temperature=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
rag_chain = (
|
| 22 |
{
|
| 23 |
+
"context": itemgetter("input") | retriever | format_docs,
|
| 24 |
+
"input": itemgetter("input")
|
| 25 |
}
|
| 26 |
| prompt
|
| 27 |
| llm
|
| 28 |
| StrOutputParser()
|
| 29 |
)
|
| 30 |
+
return rag_chain
|
| 31 |
+
|
| 32 |
+
# Embedding ๋ชจ๋ธ, ๊ฒ์๊ธฐ
|
| 33 |
+
@st.cache_resource
|
| 34 |
+
def get_retriever():
|
| 35 |
+
embedding = OpenAIEmbeddings(model="text-embedding-3-small")
|
| 36 |
+
vectorstore = FAISS.load_local(FAISS_DIR,
|
| 37 |
+
embedding,
|
| 38 |
+
allow_dangerous_deserialization=True)
|
| 39 |
+
return vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 40 |
+
|
| 41 |
+
# ๋ฌธ์ ํฌ๋งทํ
ํจ์
|
| 42 |
+
def format_docs(docs):
|
| 43 |
+
return "\n\n".join(doc.page_content for doc in docs)
|
| 44 |
|
| 45 |
+
# ๊ฒ์๊ธฐ ๋ฐ RAG ์ฒด์ธ ์ค๋น
|
| 46 |
+
retriever = get_retriever()
|
| 47 |
+
chain = get_chain()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# ํ์ด์ง ์ค์
|
| 50 |
+
st.set_page_config(page_title="AIVLE School ํ์ต ๋์ฐ๋ฏธ", page_icon="๐")
|
| 51 |
+
st.title("๐ AIVLE School ํ์ต ๋์ฐ๋ฏธ")
|
| 52 |
|
| 53 |
+
# ์ธ์
์ํ ์ด๊ธฐํ
|
| 54 |
+
if "messages" not in st.session_state:
|
| 55 |
st.session_state.messages = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
# ๊ธฐ์กด ๋ํ ๋ด์ฉ ์ถ๋ ฅ
|
| 58 |
+
for message in st.session_state.messages:
|
| 59 |
+
role = "user" if isinstance(message, HumanMessage) else "assistant"
|
| 60 |
+
with st.chat_message(role):
|
| 61 |
+
st.markdown(message.content)
|
| 62 |
|
| 63 |
+
# ์ฌ์ฉ์ ์
๋ ฅ ์ฒ๋ฆฌ
|
| 64 |
+
user_input = st.chat_input("๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์.")
|
| 65 |
if user_input:
|
| 66 |
+
# ์ฌ์ฉ์ ๋ฉ์์ง ํ์
|
| 67 |
+
st.chat_message("user").markdown(user_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
# ์๋ต ์ถ๋ ฅ
|
| 70 |
+
with st.chat_message("assistant"):
|
| 71 |
+
response_stream = chain.stream({"input": user_input})
|
| 72 |
+
full_response = st.write_stream(response_stream)
|
| 73 |
|
| 74 |
+
# ๋ํ ๊ธฐ๋ก ์ ์ฅ
|
| 75 |
+
st.session_state.messages.append(HumanMessage(content=user_input))
|
| 76 |
+
st.session_state.messages.append(AIMessage(content=full_response))
|