Sairesh commited on
Commit
e092e15
·
verified ·
1 Parent(s): fba28db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -16
app.py CHANGED
@@ -1,32 +1,52 @@
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)
 
1
+ import time, requests, os, threading
2
+ from flask import Flask, request, render_template_string
 
 
 
3
 
 
4
  app = Flask(__name__)
5
 
6
+ # This will store your Colab link while the app is running
7
+ target_config = {"url": None}
8
+
9
+ # Simple HTML Dashboard
10
+ HTML_PAGE = '''
11
+ <!DOCTYPE html>
12
+ <html>
13
+ <head><title>Colab Keep-Alive</title></head>
14
+ <body style="font-family: sans-serif; text-align: center; padding-top: 50px;">
15
+ <h2>🚀 Colab Heartbeat Dashboard</h2>
16
+ <form method="POST">
17
+ <input type="text" name="colab_url" placeholder="Paste Localtunnel URL here..."
18
+ style="width: 300px; padding: 10px;" required>
19
+ <button type="submit" style="padding: 10px 20px; cursor: pointer;">Start Heartbeat</button>
20
+ </form>
21
+ <br>
22
+ <div style="color: blue;">
23
+ <strong>Current Status:</strong> {{ status }}
24
+ </div>
25
+ </body>
26
+ </html>
27
+ '''
28
+
29
+ @app.route('/', methods=['GET', 'POST'])
30
  def home():
31
+ if request.method == 'POST':
32
+ target_config["url"] = request.form.get('colab_url')
33
+ return render_template_string(HTML_PAGE, status=f"Active: {target_config['url']}")
34
+
35
+ status = target_config["url"] if target_config["url"] else "Waiting for link..."
36
+ return render_template_string(HTML_PAGE, status=status)
37
 
38
  def heartbeat_loop():
 
39
  while True:
40
+ url = target_config.get("url")
41
  if url:
42
  try:
43
+ # Add /heartbeat or just / to the end if needed
44
  res = requests.get(url, timeout=10)
45
+ print(f"[{time.ctime()}] Pinging: {url} | Status: {res.status_code}")
46
  except Exception as e:
47
+ print(f"[{time.ctime()}] Ping failed: {e}")
48
  time.sleep(60)
49
 
50
  if __name__ == "__main__":
 
51
  threading.Thread(target=heartbeat_loop, daemon=True).start()
 
 
 
52
  app.run(host='0.0.0.0', port=7860)