Spaces:
Paused
Paused
Download logs_route.py from bep40/VNEWS: direct link, hf CLI and curl.
- Browser
- Download file 3.86 kB
-
https://huggingface.co/spaces/bep40/VNEWS/resolve/main/logs_route.py
- Command line
-
hf download hf://spaces/bep40/VNEWS/logs_route.py
-
curl -L -o logs_route.py https://huggingface.co/spaces/bep40/VNEWS/resolve/main/logs_route.py
3.86 kB
| """Independent logs page for VNEWS Space. | |
| Serves /logs (HTML) and /logs.txt (raw) so build/runtime errors are visible | |
| even when the Hugging Face build-logs tab is stuck/unavailable. | |
| Mounted from _run.py. | |
| """ | |
| import os | |
| import time | |
| import json | |
| import subprocess | |
| from fastapi import Request | |
| from fastapi.responses import HTMLResponse, PlainTextResponse | |
| try: | |
| from app_v2_entry import app | |
| except Exception: | |
| from main import app | |
| BUILD_DONE = "/app/.build_done" | |
| DATA_DIR = '/data' if os.path.isdir('/data') else os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data') | |
| def _collect(): | |
| lines = [] | |
| lines.append("=== VNEWS LOGS ===") | |
| lines.append("generated: " + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())) | |
| lines.append("") | |
| # Build marker | |
| if os.path.exists(BUILD_DONE): | |
| lines.append("[BUILD] .build_done exists -> container started OK") | |
| try: | |
| lines.append("[BUILD] built at: " + open(BUILD_DONE).read().strip()) | |
| except Exception: | |
| pass | |
| else: | |
| lines.append("[BUILD] WARNING: .build_done MISSING -> uvicorn started before build finished?") | |
| lines.append("") | |
| # Space status from HF runtime file | |
| try: | |
| import json as _j | |
| mj = os.path.join(os.path.dirname(os.path.abspath(__file__)), '.huggingface', 'main.json') | |
| if os.path.exists(mj): | |
| lines.append("[RUNTIME] .huggingface/main.json present") | |
| else: | |
| lines.append("[RUNTIME] .huggingface/main.json NOT found") | |
| except Exception as e: | |
| lines.append("[RUNTIME] error: " + str(e)) | |
| lines.append("") | |
| # Data dir contents | |
| lines.append("[DATA] dir=" + DATA_DIR) | |
| try: | |
| if os.path.isdir(DATA_DIR): | |
| for f in sorted(os.listdir(DATA_DIR)): | |
| p = os.path.join(DATA_DIR, f) | |
| lines.append(" - %s (%d bytes)" % (f, os.path.getsize(p))) | |
| else: | |
| lines.append(" (data dir missing)") | |
| except Exception as e: | |
| lines.append(" error: " + str(e)) | |
| lines.append("") | |
| # Recent container logs (stdout) if captured | |
| log_paths = ["/tmp/vnews_stdout.log", os.path.join(DATA_DIR, "app.log")] | |
| for lp in log_paths: | |
| if os.path.exists(lp): | |
| lines.append("[STDOUT] tail of " + lp + ":") | |
| try: | |
| with open(lp, "r", errors="replace") as fh: | |
| tail = fh.read().splitlines()[-50:] | |
| for l in tail: | |
| lines.append(" " + l) | |
| except Exception as e: | |
| lines.append(" read error: " + str(e)) | |
| lines.append("") | |
| # Environment hints | |
| lines.append("[ENV] HF_SPACE: " + os.environ.get("HF_SPACE", "?")) | |
| lines.append("[ENV] SPACE_ID: " + os.environ.get("SPACE_ID", "?")) | |
| lines.append("[ENV] CUDA/CPU: " + ("gpu" if os.environ.get("CUDA_VISIBLE_DEVICES") else "cpu")) | |
| lines.append("") | |
| lines.append("=== END ===") | |
| return "\n".join(lines) | |
| def logs_page(request: Request): | |
| txt = _collect() | |
| html = ( | |
| "<!DOCTYPE html><html lang='vi'><head><meta charset='utf-8'>" | |
| "<meta name='viewport' content='width=device-width,initial-scale=1'>" | |
| "<title>VNEWS Logs</title>" | |
| "<style>body{background:#0d1117;color:#c9d1d9;font-family:monospace;padding:16px}" | |
| "pre{white-space:pre-wrap;word-break:break-word;font-size:13px;line-height:1.5}" | |
| "a{color:#58a6ff}</style></head><body>" | |
| "<h2>VNEWS — Build & Runtime Logs</h2>" | |
| "<p><a href='/logs.txt'>📄 raw text</a> · refresh để cập nhật</p>" | |
| "<pre>" + txt.replace("&", "&").replace("<", "<").replace(">", ">") + "</pre>" | |
| "</body></html>" | |
| ) | |
| return HTMLResponse(html) | |
| def logs_raw(request: Request): | |
| return PlainTextResponse(_collect()) | |