File size: 3,486 Bytes
9d6ab7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e27c01a
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""
status_server.py  –  tiny HTTP server on port 7860
Serves a live-updating log page so you can watch conversion progress
directly from the HF Space URL.

Usage: python status_server.py /path/to/conversion.log
"""

import sys
import os
import time
from http.server import BaseHTTPRequestHandler, HTTPServer
from pathlib import Path

LOG_FILE = Path(sys.argv[1]) if len(sys.argv) > 1 else Path("/workspace/output/conversion.log")
START_TIME = time.time()


def read_log() -> str:
    try:
        return LOG_FILE.read_text(errors="replace") if LOG_FILE.exists() else "(log not yet created)"
    except Exception as e:
        return f"(error reading log: {e})"


def elapsed() -> str:
    s = int(time.time() - START_TIME)
    h, m = divmod(s, 3600)
    m, s = divmod(m, 60)
    return f"{h:02d}:{m:02d}:{s:02d}"


class Handler(BaseHTTPRequestHandler):
    def log_message(self, *args):
        pass  # suppress access log noise

    def do_GET(self):
        log_content = read_log()
        # Detect done/failed for status badge
        if "✔ Conversion complete" in log_content or "✔ All done" in log_content:
            status = "✔ COMPLETE"
            status_color = "#2ecc71"
        elif "✘ Conversion FAILED" in log_content:
            status = "✘ FAILED"
            status_color = "#e74c3c"
        else:
            status = "⟳ RUNNING"
            status_color = "#3498db"

        # Escape for HTML
        safe_log = (log_content
                    .replace("&", "&")
                    .replace("<", "&lt;")
                    .replace(">", "&gt;"))

        html = f"""<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="refresh" content="15">
  <title>DeepSeek-Math-V2 Converter</title>
  <style>
    body {{ background:#111; color:#eee; font-family:monospace; margin:0; padding:16px; }}
    h1   {{ font-size:1.2em; margin:0 0 8px; }}
    .badge {{ display:inline-block; padding:4px 12px; border-radius:4px;
              background:{status_color}; color:#fff; font-weight:bold; margin-bottom:12px; }}
    .meta  {{ color:#888; font-size:0.85em; margin-bottom:12px; }}
    pre    {{ background:#1a1a1a; padding:12px; border-radius:6px;
              overflow-x:auto; white-space:pre-wrap; word-break:break-all;
              font-size:0.82em; max-height:80vh; overflow-y:auto; }}
  </style>
</head>
<body>
  <h1>DeepSeek-Math-V2 → GGUF Converter</h1>
  <div class="badge">{status}</div>
  <div class="meta">Elapsed: {elapsed()} &nbsp;|&nbsp; Page auto-refreshes every 15 s</div>
  <pre>{safe_log}</pre>
  <script>window.scrollTo(0, document.querySelector('pre').scrollHeight);</script>
</body>
</html>"""

        body = html.encode()
        self.send_response(200)
        self.send_header("Content-Type", "text/html; charset=utf-8")
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)


if __name__ == "__main__":
    server = HTTPServer(("0.0.0.0", 7860), Handler)
    print(f"Status server listening on :7860  (log: {LOG_FILE})")
    server.serve_forever()


# Keep-alive ping logged every 10 minutes so HF Spaces sees activity
import threading
def keepalive():
    while True:
        time.sleep(600)
        try:
            with open(LOG_FILE, "a") as f:
                f.write(f"[keepalive] {time.strftime('%H:%M:%S')} – container alive\n")
        except Exception:
            pass
threading.Thread(target=keepalive, daemon=True).start()