Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -194,13 +194,30 @@ def _build_structured_user_prompt(user_text: str) -> str:
|
|
| 194 |
# ๋ถํ์ํ ๋ํ ์์ด, ๋ชจ๋ธ์ด JSON๋ง ๋ด๋๋ก ๊น๋ํ ์ ๋ฌ
|
| 195 |
return user_text.strip()
|
| 196 |
|
| 197 |
-
def
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
)
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
_msgs = []
|
| 205 |
if system_prompt:
|
| 206 |
_msgs.append({"role": "system", "content": system_prompt})
|
|
@@ -209,20 +226,26 @@ def _call_ollama_chat(
|
|
| 209 |
payload = {
|
| 210 |
"model": model,
|
| 211 |
"messages": _msgs,
|
| 212 |
-
"options": {
|
| 213 |
-
"temperature": temperature,
|
| 214 |
-
"top_p": top_p,
|
| 215 |
-
"top_k": top_k,
|
| 216 |
-
"repeat_penalty": repeat_penalty,
|
| 217 |
-
},
|
| 218 |
"stream": False,
|
| 219 |
}
|
| 220 |
try:
|
| 221 |
r = requests.post(url, json=payload, timeout=OLLAMA_TIMEOUT)
|
| 222 |
r.raise_for_status()
|
| 223 |
return (r.json().get("message") or {}).get("content", "") or ""
|
| 224 |
-
except requests.
|
| 225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
|
| 227 |
def _llm_structured_extract(user_text: str):
|
| 228 |
out = _call_ollama_chat(
|
|
@@ -1684,6 +1707,9 @@ def main():
|
|
| 1684 |
init_session()
|
| 1685 |
chat_container = st.container()
|
| 1686 |
|
|
|
|
|
|
|
|
|
|
| 1687 |
# โ
ํ์คํฌ๋ฆฐ์ผ ๋๋ง ์กฐ๊ธฐ ๋ฆฌํด
|
| 1688 |
if st.session_state.get("llm_mode") and not st.session_state.get("llm_inline", False):
|
| 1689 |
render_llm_followup(chat_container, inline=False)
|
|
|
|
| 194 |
# ๋ถํ์ํ ๋ํ ์์ด, ๋ชจ๋ธ์ด JSON๋ง ๋ด๋๋ก ๊น๋ํ ์ ๋ฌ
|
| 195 |
return user_text.strip()
|
| 196 |
|
| 197 |
+
def _ollama_healthcheck():
|
| 198 |
+
base = OLLAMA_HOST.rstrip("/")
|
| 199 |
+
# 1) ์๋ฒ ์ด์์๋์ง
|
| 200 |
+
try:
|
| 201 |
+
r = requests.get(f"{base}/api/version", timeout=5)
|
| 202 |
+
r.raise_for_status()
|
| 203 |
+
except requests.RequestException as e:
|
| 204 |
+
st.error(f"โ Ollama ์ฐ๊ฒฐ ์คํจ: {e} (host={OLLAMA_HOST})")
|
| 205 |
+
return False
|
| 206 |
+
|
| 207 |
+
# 2) ๋ชจ๋ธ ์ค์น ์ฌ๋ถ
|
| 208 |
+
try:
|
| 209 |
+
tags = requests.get(f"{base}/api/tags", timeout=5).json()
|
| 210 |
+
names = [m.get("name") for m in tags.get("models", [])]
|
| 211 |
+
if OLLAMA_MODEL not in names:
|
| 212 |
+
st.warning(f"โ ๏ธ ๋ชจ๋ธ ๋ฏธ์ค์น: `{OLLAMA_MODEL}`. ์๋ฒ์์ `ollama pull {OLLAMA_MODEL}` ์คํ ํ์.")
|
| 213 |
+
except requests.RequestException as e:
|
| 214 |
+
st.warning(f"๋ชจ๋ธ ๋ชฉ๋ก ์กฐํ ์คํจ: {e}")
|
| 215 |
+
|
| 216 |
+
return True
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
def _call_ollama_chat(messages, model=OLLAMA_MODEL, temperature=0.8, top_p=0.9, top_k=40, repeat_penalty=1.1, system_prompt=None):
|
| 220 |
+
url = f"{OLLAMA_HOST.rstrip('/')}/api/chat"
|
| 221 |
_msgs = []
|
| 222 |
if system_prompt:
|
| 223 |
_msgs.append({"role": "system", "content": system_prompt})
|
|
|
|
| 226 |
payload = {
|
| 227 |
"model": model,
|
| 228 |
"messages": _msgs,
|
| 229 |
+
"options": {"temperature": temperature, "top_p": top_p, "top_k": top_k, "repeat_penalty": repeat_penalty},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
"stream": False,
|
| 231 |
}
|
| 232 |
try:
|
| 233 |
r = requests.post(url, json=payload, timeout=OLLAMA_TIMEOUT)
|
| 234 |
r.raise_for_status()
|
| 235 |
return (r.json().get("message") or {}).get("content", "") or ""
|
| 236 |
+
except requests.Timeout:
|
| 237 |
+
st.error(f"โฑ๏ธ Ollama ํ์์์({OLLAMA_TIMEOUT}s). host={OLLAMA_HOST}, model={model}")
|
| 238 |
+
except requests.ConnectionError as e:
|
| 239 |
+
st.error(f"๐ ์ฐ๊ฒฐ ์คํจ: {e} (host={OLLAMA_HOST})")
|
| 240 |
+
except requests.HTTPError as e:
|
| 241 |
+
try:
|
| 242 |
+
detail = r.text[:500]
|
| 243 |
+
except Exception:
|
| 244 |
+
detail = str(e)
|
| 245 |
+
st.error(f"HTTP {r.status_code}: {detail}")
|
| 246 |
+
except requests.RequestException as e:
|
| 247 |
+
st.error(f"์์ฒญ ์ค๋ฅ: {e}")
|
| 248 |
+
return ""
|
| 249 |
|
| 250 |
def _llm_structured_extract(user_text: str):
|
| 251 |
out = _call_ollama_chat(
|
|
|
|
| 1707 |
init_session()
|
| 1708 |
chat_container = st.container()
|
| 1709 |
|
| 1710 |
+
if not _ollama_healthcheck():
|
| 1711 |
+
st.stop()
|
| 1712 |
+
|
| 1713 |
# โ
ํ์คํฌ๋ฆฐ์ผ ๋๋ง ์กฐ๊ธฐ ๋ฆฌํด
|
| 1714 |
if st.session_state.get("llm_mode") and not st.session_state.get("llm_inline", False):
|
| 1715 |
render_llm_followup(chat_container, inline=False)
|