Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -178,6 +178,18 @@ def to_llm_mode():
|
|
| 178 |
st.session_state["llm_mode"] = True
|
| 179 |
st.session_state["llm_intro_needed"] = True
|
| 180 |
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
def _build_structured_user_prompt(user_text: str) -> str:
|
| 183 |
# ๋ถํ์ํ ๋ํ ์์ด, ๋ชจ๋ธ์ด JSON๋ง ๋ด๋๋ก ๊น๋ํ ์ ๋ฌ
|
|
@@ -240,13 +252,11 @@ def _ensure_llm_state():
|
|
| 240 |
st.session_state.setdefault("llm_intro_needed", False)
|
| 241 |
st.session_state.setdefault("llm_input", "")
|
| 242 |
|
| 243 |
-
def render_llm_followup(chat_container):
|
| 244 |
-
"""
|
| 245 |
-
์ถ์ฒ ํ๋ก์ฐ ์ข
๋ฃ ํ ์๋ ์ง์
ํ๋ ์์ ์ง๋ฌธ(LLM) ๋ชจ๋ UI.
|
| 246 |
-
"""
|
| 247 |
_ensure_llm_state()
|
| 248 |
MAX_TURNS = 6
|
| 249 |
|
|
|
|
| 250 |
if st.session_state.get("llm_intro_needed"):
|
| 251 |
log_and_render(
|
| 252 |
"๐ง ์ด์ ๋ถํฐ๋ ์์ ์ง๋ฌธ ๋ชจ๋์์. ์ฌํ ์ธ์๋ ๋ญ๋ ํ๊ตญ์ด๋ก ๋ฌผ์ด๋ณด์ธ์!",
|
|
@@ -256,75 +266,70 @@ def render_llm_followup(chat_container):
|
|
| 256 |
)
|
| 257 |
st.session_state["llm_intro_needed"] = False
|
| 258 |
|
| 259 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
q = st.text_input(
|
| 261 |
"LLM ์ง๋ฌธ",
|
| 262 |
placeholder="๋ฌด์์ด๋ ๋ฌผ์ด๋ณด์ธ์ (์ข
๋ฃํ๋ ค๋ฉด '์ข
๋ฃ' ์
๋ ฅ)",
|
| 263 |
key="llm_input"
|
| 264 |
)
|
| 265 |
|
| 266 |
-
# ๋์
|
| 267 |
if q:
|
| 268 |
# ์ข
๋ฃ ํค์๋
|
| 269 |
if q.strip() in ("์ข
๋ฃ", "quit", "exit"):
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
q,
|
| 284 |
-
sender="user",
|
| 285 |
-
chat_container=chat_container,
|
| 286 |
-
key=f"llm_user_{random.randint(1,999999)}"
|
| 287 |
-
)
|
| 288 |
st.session_state.llm_history.append({"role": "user", "content": q})
|
| 289 |
|
| 290 |
-
# ์ต๊ทผ ํ์คํ ๋ฆฌ๋ก LLM ํธ์ถ
|
| 291 |
msgs = st.session_state.llm_history[-(MAX_TURNS-1):]
|
| 292 |
a = _call_ollama_chat(
|
| 293 |
messages=msgs,
|
| 294 |
system_prompt=KOREAN_SYSTEM_PROMPT,
|
| 295 |
temperature=0.8, top_p=0.9, top_k=40, repeat_penalty=1.1
|
| 296 |
)
|
| 297 |
-
|
| 298 |
if not a:
|
| 299 |
-
log_and_render(
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
chat_container=chat_container,
|
| 303 |
-
key=f"llm_err_{random.randint(1,999999)}"
|
| 304 |
-
)
|
| 305 |
else:
|
| 306 |
-
log_and_render(
|
| 307 |
-
|
| 308 |
-
sender="bot",
|
| 309 |
-
chat_container=chat_container,
|
| 310 |
-
key=f"llm_bot_{random.randint(1,999999)}"
|
| 311 |
-
)
|
| 312 |
st.session_state.llm_history.append({"role": "assistant", "content": a})
|
| 313 |
-
|
| 314 |
-
# ์
๋ ฅ ์ด๊ธฐํ
|
| 315 |
st.session_state["llm_input"] = ""
|
| 316 |
|
| 317 |
-
#
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
|
|
|
|
|
|
| 321 |
st.session_state["llm_mode"] = False
|
| 322 |
-
log_and_render(
|
| 323 |
-
"LLM ๋ชจ๋๋ฅผ ์ข
๋ฃํ ๊ฒ์. ๋ค์์ ๋ ๋ง๋์! ๐",
|
| 324 |
-
sender="bot",
|
| 325 |
-
chat_container=chat_container,
|
| 326 |
-
key=f"llm_end_btn_{random.randint(1,999999)}"
|
| 327 |
-
)
|
| 328 |
st.rerun()
|
| 329 |
|
| 330 |
# ์ง์ฐ ์ด๊ธฐํ: import ์์ ์๋ ๋ฐ์ดํฐ ์ ๊ทผ ๊ธ์ง, ์ฌ๊ธฐ์ ํ ๋ฒ๋ง ์ฃผ์
|
|
@@ -1695,8 +1700,9 @@ def main():
|
|
| 1695 |
init_session()
|
| 1696 |
chat_container = st.container()
|
| 1697 |
|
| 1698 |
-
|
| 1699 |
-
|
|
|
|
| 1700 |
return
|
| 1701 |
|
| 1702 |
# ๐๏ธ ๋งํ์ /ํ์ ์ต์
(โข, โฃ)
|
|
|
|
| 178 |
st.session_state["llm_mode"] = True
|
| 179 |
st.session_state["llm_intro_needed"] = True
|
| 180 |
st.rerun()
|
| 181 |
+
|
| 182 |
+
def _ensure_llm_state():
|
| 183 |
+
st.session_state.setdefault("llm_mode", False) # ํ์คํฌ๋ฆฐ ๋ชจ๋์ฉ(๊ธฐ์กด)
|
| 184 |
+
st.session_state.setdefault("llm_inline", False) # โ
์ธ๋ผ์ธ ํ์์ฉ
|
| 185 |
+
st.session_state.setdefault("llm_history", [])
|
| 186 |
+
st.session_state.setdefault("llm_intro_needed", False)
|
| 187 |
+
st.session_state.setdefault("llm_input", "")
|
| 188 |
+
|
| 189 |
+
def show_llm_inline():
|
| 190 |
+
_ensure_llm_state()
|
| 191 |
+
st.session_state["llm_inline"] = True
|
| 192 |
+
st.session_state["llm_intro_needed"] = True
|
| 193 |
|
| 194 |
def _build_structured_user_prompt(user_text: str) -> str:
|
| 195 |
# ๋ถํ์ํ ๋ํ ์์ด, ๋ชจ๋ธ์ด JSON๋ง ๋ด๋๋ก ๊น๋ํ ์ ๋ฌ
|
|
|
|
| 252 |
st.session_state.setdefault("llm_intro_needed", False)
|
| 253 |
st.session_state.setdefault("llm_input", "")
|
| 254 |
|
| 255 |
+
def render_llm_followup(chat_container, inline=False):
|
|
|
|
|
|
|
|
|
|
| 256 |
_ensure_llm_state()
|
| 257 |
MAX_TURNS = 6
|
| 258 |
|
| 259 |
+
# ์ธ๋ผ์ธ์ด๋ฉด ์๋ด๋ ์ฑํ
์ฐฝ ๋งํ์ ํํ๋ก ๋ ๋
|
| 260 |
if st.session_state.get("llm_intro_needed"):
|
| 261 |
log_and_render(
|
| 262 |
"๐ง ์ด์ ๋ถํฐ๋ ์์ ์ง๋ฌธ ๋ชจ๋์์. ์ฌํ ์ธ์๋ ๋ญ๋ ํ๊ตญ์ด๋ก ๋ฌผ์ด๋ณด์ธ์!",
|
|
|
|
| 266 |
)
|
| 267 |
st.session_state["llm_intro_needed"] = False
|
| 268 |
|
| 269 |
+
# ์ธ๋ผ์ธ ํจ๋ ๊ตฌ์ญ
|
| 270 |
+
if inline:
|
| 271 |
+
st.divider()
|
| 272 |
+
st.markdown("#### ๐ค LLM ์ง๋ฌธ")
|
| 273 |
+
else:
|
| 274 |
+
st.markdown("### ๐ค LLM ์ง๋ฌธ")
|
| 275 |
+
|
| 276 |
q = st.text_input(
|
| 277 |
"LLM ์ง๋ฌธ",
|
| 278 |
placeholder="๋ฌด์์ด๋ ๋ฌผ์ด๋ณด์ธ์ (์ข
๋ฃํ๋ ค๋ฉด '์ข
๋ฃ' ์
๋ ฅ)",
|
| 279 |
key="llm_input"
|
| 280 |
)
|
| 281 |
|
|
|
|
| 282 |
if q:
|
| 283 |
# ์ข
๋ฃ ํค์๋
|
| 284 |
if q.strip() in ("์ข
๋ฃ", "quit", "exit"):
|
| 285 |
+
if inline:
|
| 286 |
+
st.session_state["llm_inline"] = False # โ
์ธ๋ผ์ธ๋ง ๋ซ๊ธฐ
|
| 287 |
+
st.session_state["llm_input"] = ""
|
| 288 |
+
log_and_render(
|
| 289 |
+
"LLM ํจ๋์ ๋ซ์๊ฒ์. ํ์ํ์ค ๋ ๋ค์ ์ง๋ฌธํด ์ฃผ์ธ์! โจ",
|
| 290 |
+
sender="bot", chat_container=chat_container,
|
| 291 |
+
key=f"llm_end_inline_{random.randint(1,999999)}"
|
| 292 |
+
)
|
| 293 |
+
return
|
| 294 |
+
else:
|
| 295 |
+
st.session_state["llm_mode"] = False
|
| 296 |
+
st.session_state["llm_input"] = ""
|
| 297 |
+
log_and_render(
|
| 298 |
+
"LLM ๋ชจ๋๋ฅผ ์ข
๋ฃํ ๊ฒ์. ํ์ํ์ค ๋ ๋ค์ ์ง๋ฌธํด ์ฃผ์ธ์! โจ",
|
| 299 |
+
sender="bot", chat_container=chat_container,
|
| 300 |
+
key=f"llm_end_full_{random.randint(1,999999)}"
|
| 301 |
+
)
|
| 302 |
+
# ํ์คํฌ๋ฆฐ ๋ชจ๋๋ง rerun
|
| 303 |
+
st.rerun()
|
| 304 |
+
return
|
| 305 |
|
| 306 |
+
log_and_render(q, sender="user", chat_container=chat_container,
|
| 307 |
+
key=f"llm_user_{random.randint(1,999999)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
st.session_state.llm_history.append({"role": "user", "content": q})
|
| 309 |
|
|
|
|
| 310 |
msgs = st.session_state.llm_history[-(MAX_TURNS-1):]
|
| 311 |
a = _call_ollama_chat(
|
| 312 |
messages=msgs,
|
| 313 |
system_prompt=KOREAN_SYSTEM_PROMPT,
|
| 314 |
temperature=0.8, top_p=0.9, top_k=40, repeat_penalty=1.1
|
| 315 |
)
|
|
|
|
| 316 |
if not a:
|
| 317 |
+
log_and_render("โ ๏ธ LLM ์๋ต์ ๋ฐ์ง ๋ชปํ์ต๋๋ค. Ollama ์๋ฒ๋ฅผ ํ์ธํด ์ฃผ์ธ์.",
|
| 318 |
+
sender="bot", chat_container=chat_container,
|
| 319 |
+
key=f"llm_err_{random.randint(1,999999)}")
|
|
|
|
|
|
|
|
|
|
| 320 |
else:
|
| 321 |
+
log_and_render(a, sender="bot", chat_container=chat_container,
|
| 322 |
+
key=f"llm_bot_{random.randint(1,999999)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
st.session_state.llm_history.append({"role": "assistant", "content": a})
|
|
|
|
|
|
|
| 324 |
st.session_state["llm_input"] = ""
|
| 325 |
|
| 326 |
+
# ๋ซ๊ธฐ ๋ฒํผ
|
| 327 |
+
if inline:
|
| 328 |
+
if st.button("๐ฝ LLM ํจ๋ ๋ซ๊ธฐ", key="llm_close_inline"):
|
| 329 |
+
st.session_state["llm_inline"] = False
|
| 330 |
+
else:
|
| 331 |
+
if st.button("๐ LLM ๋ชจ๋ ์ข
๋ฃ", key="llm_close_full"):
|
| 332 |
st.session_state["llm_mode"] = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
st.rerun()
|
| 334 |
|
| 335 |
# ์ง์ฐ ์ด๊ธฐํ: import ์์ ์๋ ๋ฐ์ดํฐ ์ ๊ทผ ๊ธ์ง, ์ฌ๊ธฐ์ ํ ๋ฒ๋ง ์ฃผ์
|
|
|
|
| 1700 |
init_session()
|
| 1701 |
chat_container = st.container()
|
| 1702 |
|
| 1703 |
+
# โ
ํ์คํฌ๋ฆฐ์ผ ๋๋ง ์กฐ๊ธฐ ๋ฆฌํด
|
| 1704 |
+
if st.session_state.get("llm_mode") and not st.session_state.get("llm_inline", False):
|
| 1705 |
+
render_llm_followup(chat_container, inline=False)
|
| 1706 |
return
|
| 1707 |
|
| 1708 |
# ๐๏ธ ๋งํ์ /ํ์ ์ต์
(โข, โฃ)
|