Spaces:
Paused
Paused
| set -e | |
| echo "π¦ Installing Python dependencies..." | |
| # Removed --break-system-packages (not supported on Debian 11 / Python 3.9) | |
| pip install requests | |
| # --- GENERATE server.py (Foreground Web Server for HF Health Check) --- | |
| cat > /app/server.py << 'PYEOF' | |
| from http.server import HTTPServer, BaseHTTPRequestHandler | |
| from datetime import datetime | |
| class Handler(BaseHTTPRequestHandler): | |
| def do_GET(self): | |
| self.send_response(200) | |
| self.send_header('Content-type', 'text/html') | |
| self.end_headers() | |
| html = f""" | |
| <html><body style="font-family:sans-serif;text-align:center;padding-top:50px;"> | |
| <h1>β€οΈ Running</h1> | |
| <p>Space active. Keep-alive & SSHX tunnel operational.</p> | |
| <small>{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}</small> | |
| </body></html>""" | |
| self.wfile.write(html.encode()) | |
| def log_message(self, format, *args): | |
| pass | |
| if __name__ == "__main__": | |
| print("β Web server running on port 7860") | |
| HTTPServer(("0.0.0.0", 7860), Handler).serve_forever() | |
| PYEOF | |
| # --- GENERATE keep_alive.py (Background Randomized Pinger) --- | |
| cat > /app/keep_alive.py << 'PYEOF' | |
| import requests, time, random | |
| from datetime import datetime, timedelta | |
| SPACE_URL = "https://awesome-developer-meow.hf.space" | |
| MIN_HOURS, MAX_HOURS = 20, 46 # Must be < 48h to prevent pause | |
| LOG_FILE = "/data/ping.log" | |
| def log_ping(status, msg=""): | |
| ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
| try: | |
| with open(LOG_FILE, "a") as f: | |
| f.write(f"[{ts}] {status} {msg}\n") | |
| except Exception: | |
| pass | |
| def ping_space(): | |
| try: | |
| r = requests.get(SPACE_URL, timeout=30) | |
| if r.status_code == 200: | |
| log_ping("SUCCESS", f"Status: {r.status_code}") | |
| return True | |
| else: | |
| log_ping("ERROR", f"Unexpected status: {r.status_code}") | |
| return False | |
| except Exception as e: | |
| log_ping("ERROR", str(e)) | |
| return False | |
| def main(): | |
| log_ping("STARTUP", "Keep-alive initialized") | |
| ping_space() | |
| while True: | |
| hours = random.uniform(MIN_HOURS, MAX_HOURS) | |
| sleep_secs = int(hours * 3600) | |
| next_time = datetime.now() + timedelta(seconds=sleep_secs) | |
| log_ping("SLEEPING", f"Next in {hours:.2f}h at {next_time.strftime('%H:%M:%S')}") | |
| time.sleep(sleep_secs) | |
| ping_space() | |
| if __name__ == "__main__": | |
| main() | |
| PYEOF | |
| # Start SSH service (required for sshx) | |
| service ssh start 2>/dev/null || true | |
| # --- START SSHX AND CAPTURE URL --- | |
| echo " Starting sshx tunnel..." | |
| nohup sshx > /tmp/sshx_output.log 2>&1 & | |
| SSHX_PID=$! | |
| disown $SSHX_PID | |
| for i in {1..15}; do | |
| if grep -q "sshx.io" /tmp/sshx_output.log; then | |
| SSHX_URL=$(grep -oP 'https://sshx\.io/[^\s]+' /tmp/sshx_output.log | head -1) | |
| echo "$SSHX_URL" > /data/sshx_url.txt | |
| echo "β SSHX URL saved to /data/sshx_url.txt: $SSHX_URL" | |
| break | |
| fi | |
| sleep 1 | |
| done | |
| if [ ! -f /data/sshx_url.txt ]; then | |
| echo "β οΈ SSHX did not connect within 15s. Check /tmp/sshx_output.log" | |
| fi | |
| # --- START SILENT BACKGROUND KEEP-ALIVE PINGER --- | |
| echo "π Starting silent background keep-alive..." | |
| nohup python3 /app/keep_alive.py > /dev/null 2>&1 & | |
| disown | |
| # --- START FOREGROUND WEB SERVER (HF HEALTH CHECK) --- | |
| echo "π Starting main web server on :7860..." | |
| exec python3 /app/server.py |