Spaces:
Sleeping
Sleeping
| """ | |
| 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("<", "<") | |
| .replace(">", ">")) | |
| 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()} | 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() | |