Spaces:
Sleeping
Sleeping
Commit ·
1ed94de
1
Parent(s): b4e3a70
fix: wait for llama-server before starting uvicorn, handle connection error in UI
Browse files- app/blocks_ui.py +28 -23
- app/start.sh +16 -12
app/blocks_ui.py
CHANGED
|
@@ -28,6 +28,7 @@ if str(REPO_ROOT) not in sys.path:
|
|
| 28 |
sys.path.insert(0, str(REPO_ROOT))
|
| 29 |
|
| 30 |
import gradio as gr # noqa: E402
|
|
|
|
| 31 |
|
| 32 |
from engine import governance_demo, recompile # noqa: E402
|
| 33 |
from engine.loop import SLUG, finish_turn, step_stream # noqa: E402
|
|
@@ -401,29 +402,33 @@ def _respond(message: str, history: list[dict]):
|
|
| 401 |
|
| 402 |
thinking_text = ""
|
| 403 |
content_text = ""
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
if
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 427 |
|
| 428 |
|
| 429 |
def _post_process(user_message: str, reply: str):
|
|
|
|
| 28 |
sys.path.insert(0, str(REPO_ROOT))
|
| 29 |
|
| 30 |
import gradio as gr # noqa: E402
|
| 31 |
+
from openai import APIConnectionError # noqa: E402
|
| 32 |
|
| 33 |
from engine import governance_demo, recompile # noqa: E402
|
| 34 |
from engine.loop import SLUG, finish_turn, step_stream # noqa: E402
|
|
|
|
| 402 |
|
| 403 |
thinking_text = ""
|
| 404 |
content_text = ""
|
| 405 |
+
try:
|
| 406 |
+
for kind, payload in step_stream(message, prior_history):
|
| 407 |
+
if kind == "thinking":
|
| 408 |
+
if thinking_idx is None:
|
| 409 |
+
history.insert(reply_idx, {"role": "assistant", "content": "", "metadata": {"title": "Thinking...", "status": "pending"}})
|
| 410 |
+
thinking_idx = reply_idx
|
| 411 |
+
reply_idx += 1
|
| 412 |
+
thinking_text += payload
|
| 413 |
+
history[thinking_idx]["content"] = thinking_text
|
| 414 |
+
elif kind == "content":
|
| 415 |
+
content_text += payload
|
| 416 |
+
history[reply_idx]["content"] = content_text
|
| 417 |
+
elif kind == "error":
|
| 418 |
+
content_text += f"(error: {payload})"
|
| 419 |
+
history[reply_idx]["content"] = content_text
|
| 420 |
+
elif kind == "done":
|
| 421 |
+
reply = payload or content_text
|
| 422 |
+
history[reply_idx]["content"] = reply
|
| 423 |
+
if thinking_idx is not None:
|
| 424 |
+
history[thinking_idx]["metadata"]["title"] = "Thinking"
|
| 425 |
+
history[thinking_idx]["metadata"]["status"] = "done"
|
| 426 |
+
yield history, gr.update(), gr.update(), message, reply
|
| 427 |
+
return
|
| 428 |
+
yield history, gr.update(), gr.update(), "", ""
|
| 429 |
+
except APIConnectionError:
|
| 430 |
+
history[reply_idx]["content"] = "Model server is not available. Try again in a moment."
|
| 431 |
+
yield history, gr.update(interactive=True), gr.update(interactive=True), "", ""
|
| 432 |
|
| 433 |
|
| 434 |
def _post_process(user_message: str, reply: str):
|
app/start.sh
CHANGED
|
@@ -1,22 +1,26 @@
|
|
| 1 |
#!/usr/bin/env bash
|
| 2 |
-
# Space entrypoint (F4/F6): bring up the local model server, then the app.
|
| 3 |
-
# Replaces the F0 placeholder `bash model/serve.sh` as the Dockerfile CMD.
|
| 4 |
set -euo pipefail
|
| 5 |
|
| 6 |
-
MODEL_FILE="${MODEL_FILE:-}"
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
if [ "${TEXT_MODEL_PROVIDER:-local}" = "local" ]; then
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
fi
|
| 15 |
|
| 16 |
-
# UVICORN_RELOAD=1 (local dev, e.g. `docker run -v $(pwd):/app`) hot-reloads on
|
| 17 |
-
# code edits. Unset/0 for the HF Space (F6).
|
| 18 |
if [ "${UVICORN_RELOAD:-0}" = "1" ]; then
|
| 19 |
-
|
| 20 |
else
|
| 21 |
-
|
| 22 |
fi
|
|
|
|
| 1 |
#!/usr/bin/env bash
|
|
|
|
|
|
|
| 2 |
set -euo pipefail
|
| 3 |
|
| 4 |
+
export MODEL_FILE="${MODEL_FILE:-MiniCPM5-1B-Q4_K_M.gguf}"
|
| 5 |
+
export MODEL_REPO="${MODEL_REPO:-openbmb/MiniCPM5-1B-GGUF}"
|
| 6 |
+
export MODEL_PATH="${MODEL_PATH:-model/weights/${MODEL_FILE}}"
|
| 7 |
+
export MODEL_PORT="${MODEL_PORT:-8080}"
|
| 8 |
|
| 9 |
if [ "${TEXT_MODEL_PROVIDER:-local}" = "local" ]; then
|
| 10 |
+
if [ ! -f "${MODEL_PATH}" ]; then
|
| 11 |
+
echo "Downloading ${MODEL_FILE} from ${MODEL_REPO} ..."
|
| 12 |
+
python model/download_model.py
|
| 13 |
+
fi
|
| 14 |
+
bash model/serve.sh &
|
| 15 |
+
echo "Waiting for model server on :${MODEL_PORT} ..."
|
| 16 |
+
until curl -sf "http://localhost:${MODEL_PORT}/health" >/dev/null 2>&1; do
|
| 17 |
+
sleep 2
|
| 18 |
+
done
|
| 19 |
+
echo "Model server ready."
|
| 20 |
fi
|
| 21 |
|
|
|
|
|
|
|
| 22 |
if [ "${UVICORN_RELOAD:-0}" = "1" ]; then
|
| 23 |
+
exec uvicorn app.server:app --host 0.0.0.0 --port "${PORT:-7860}" --reload
|
| 24 |
else
|
| 25 |
+
exec uvicorn app.server:app --host 0.0.0.0 --port "${PORT:-7860}"
|
| 26 |
fi
|