petter2025 commited on
Commit
765a3c3
·
verified ·
1 Parent(s): 99c36b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -2
app.py CHANGED
@@ -54,13 +54,16 @@ def keep_alive():
54
  url = f"https://{space_id.replace('/', '-')}.hf.space/"
55
  logging.info(f"Using external URL for keep‑alive: {url}")
56
  else:
 
 
57
  url = "http://127.0.0.1:7860/"
58
  logging.warning("No SPACE_ID found, using localhost – will not prevent sleep!")
59
 
60
  while True:
61
- time.sleep(300)
62
  try:
63
- with urllib.request.urlopen(url, timeout=10) as response:
 
64
  status = response.getcode()
65
  logging.info(f"Keep‑alive ping: {status}")
66
  except Exception as e:
 
54
  url = f"https://{space_id.replace('/', '-')}.hf.space/"
55
  logging.info(f"Using external URL for keep‑alive: {url}")
56
  else:
57
+ # Fallback: try to guess the URL from the Space's name?
58
+ # We'll use localhost as a last resort.
59
  url = "http://127.0.0.1:7860/"
60
  logging.warning("No SPACE_ID found, using localhost – will not prevent sleep!")
61
 
62
  while True:
63
+ time.sleep(300) # every 5 minutes
64
  try:
65
+ req = urllib.request.Request(url, headers={'User-Agent': 'ARF-KeepAlive/1.0'})
66
+ with urllib.request.urlopen(req, timeout=30) as response:
67
  status = response.getcode()
68
  logging.info(f"Keep‑alive ping: {status}")
69
  except Exception as e: