Sairesh commited on
Commit
3baf80b
·
verified ·
1 Parent(s): 451e8a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -10
app.py CHANGED
@@ -1,24 +1,32 @@
1
  import time
2
  import requests
3
  import os
 
 
4
 
5
- def heartbeat():
 
 
 
 
 
 
 
6
  print("Heartbeat service started...")
7
  while True:
8
- # This looks for the URL you will provide in the "Settings"
9
  url = os.environ.get("TARGET_URL")
10
-
11
  if url:
12
  try:
13
  res = requests.get(url, timeout=10)
14
  print(f"[{time.ctime()}] Pinging Colab: {res.status_code}")
15
  except Exception as e:
16
- print(f"[{time.ctime()}] Waiting for Colab to be online... {e}")
17
- else:
18
- print(f"[{time.ctime()}] No TARGET_URL found in Settings > Secrets.")
19
-
20
- time.sleep(60) # Pings every 60 seconds
21
 
22
  if __name__ == "__main__":
23
- heartbeat()
24
-
 
 
 
 
 
1
  import time
2
  import requests
3
  import os
4
+ import threading
5
+ from flask import Flask
6
 
7
+ # 1. Create a tiny web server to satisfy Hugging Face
8
+ app = Flask(__name__)
9
+
10
+ @app.route('/')
11
+ def home():
12
+ return "Heartbeat is active!"
13
+
14
+ def heartbeat_loop():
15
  print("Heartbeat service started...")
16
  while True:
 
17
  url = os.environ.get("TARGET_URL")
 
18
  if url:
19
  try:
20
  res = requests.get(url, timeout=10)
21
  print(f"[{time.ctime()}] Pinging Colab: {res.status_code}")
22
  except Exception as e:
23
+ print(f"[{time.ctime()}] Waiting for Colab... {e}")
24
+ time.sleep(60)
 
 
 
25
 
26
  if __name__ == "__main__":
27
+ # 2. Run the heartbeat in the background
28
+ threading.Thread(target=heartbeat_loop, daemon=True).start()
29
+
30
+ # 3. Run the web server on port 7860 (HF default)
31
+ # The host must be 0.0.0.0
32
+ app.run(host='0.0.0.0', port=7860)