ntdservices commited on
Commit
7ac8c92
Β·
verified Β·
1 Parent(s): e99d450

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -18
app.py CHANGED
@@ -1,16 +1,16 @@
1
  # app.py
2
  #
3
  # Hugging Face Space – Live U.S. Debt Clock
4
- # Β· Pulls the last 10 Treasury β€œDebt to the Penny” points
5
- # Β· Derives a $/sec rate from the two most-recent *different* days
6
- # Β· Computes the *current* debt by projecting from the timestamp
7
- # of the latest record β†’ now
8
- # Β· Refreshes every 5 min so the rate/value stay current
9
  #
10
  # Endpoints
11
  # / – simple text β€œrunning” check
12
  # /api/debt – JSON { startingDebt, ratePerSecond, asOf }
13
  # /api/ping – lightweight uptime probe
 
 
14
 
15
  import os, time, threading, requests
16
  from flask import Flask, jsonify
@@ -19,10 +19,10 @@ app = Flask(__name__)
19
 
20
  # ─────────────────── runtime state ────────────────────
21
  DEBT_STATE = {
22
- "debt_at_record": 0.0, # value at latest record date
23
- "record_time": 0.0, # epoch seconds for that record date (00:00 UTC)
24
- "rate_per_sec": 0.0, # $/s derived from last-two-points
25
- "last_refresh": 0.0 # when we last hit the API
26
  }
27
 
28
  # ─────────────────── helpers ──────────────────────────
@@ -42,7 +42,7 @@ def load_debt_records():
42
  ]
43
 
44
  def epoch(date_str):
45
- # record_date is YYYY-MM-DD (assume 00:00 UTC)
46
  return time.mktime(time.strptime(date_str, "%Y-%m-%d"))
47
 
48
  # ─────────────────── background refresher ────────────
@@ -52,7 +52,7 @@ def refresher():
52
  recs = load_debt_records() # newest first
53
  (d0_date, d0_val) = recs[0]
54
 
55
- # find previous day with a *different* value
56
  prev = next((d for d in recs[1:] if d[1] != d0_val), None)
57
  if prev is None:
58
  raise ValueError("Could not find two distinct points")
@@ -60,7 +60,7 @@ def refresher():
60
  (d1_date, d1_val) = prev
61
 
62
  t0, t1 = epoch(d0_date), epoch(d1_date)
63
- rate = (d0_val - d1_val) / (t0 - t1) # $/sec
64
 
65
  DEBT_STATE.update(
66
  debt_at_record = d0_val,
@@ -70,14 +70,14 @@ def refresher():
70
  )
71
 
72
  print(
73
- f"[refresh] {d0_date=} "
74
- f"debt={d0_val:,.2f} rate={rate:,.2f}$/s"
75
  )
76
 
77
  except Exception as e:
78
  print("Debt refresh error:", e)
79
 
80
- time.sleep(300) # five-minute cycle
81
 
82
  threading.Thread(target=refresher, daemon=True).start()
83
 
@@ -88,9 +88,11 @@ def root():
88
 
89
  @app.route("/api/debt")
90
  def api_debt():
91
- now = time.time()
92
- elapsed = now - DEBT_STATE["record_time"] - 86400
93
- current = DEBT_STATE["debt_at_record"] + DEBT_STATE["rate_per_sec"] * elapsed
 
 
94
  return jsonify(
95
  startingDebt = current,
96
  ratePerSecond = DEBT_STATE["rate_per_sec"],
 
1
  # app.py
2
  #
3
  # Hugging Face Space – Live U.S. Debt Clock
4
+ # β€’ Pulls the last 10 Treasury β€œDebt to the Penny” points
5
+ # β€’ Derives a $/sec rate from the last two *different* days
6
+ # β€’ Projects the current debt from the timestamp of the latest record
 
 
7
  #
8
  # Endpoints
9
  # / – simple text β€œrunning” check
10
  # /api/debt – JSON { startingDebt, ratePerSecond, asOf }
11
  # /api/ping – lightweight uptime probe
12
+ #
13
+ # ────────────────────────────────────────────────────────────
14
 
15
  import os, time, threading, requests
16
  from flask import Flask, jsonify
 
19
 
20
  # ─────────────────── runtime state ────────────────────
21
  DEBT_STATE = {
22
+ "debt_at_record": 0.0, # value at latest record date
23
+ "record_time": 0.0, # epoch seconds for that record date (00:00 UTC)
24
+ "rate_per_sec": 0.0, # $/s derived from last-two-points
25
+ "last_refresh": 0.0
26
  }
27
 
28
  # ─────────────────── helpers ──────────────────────────
 
42
  ]
43
 
44
  def epoch(date_str):
45
+ # record_date is YYYY-MM-DD (interpreted as 00:00 UTC)
46
  return time.mktime(time.strptime(date_str, "%Y-%m-%d"))
47
 
48
  # ─────────────────── background refresher ────────────
 
52
  recs = load_debt_records() # newest first
53
  (d0_date, d0_val) = recs[0]
54
 
55
+ # find previous day with *different* value
56
  prev = next((d for d in recs[1:] if d[1] != d0_val), None)
57
  if prev is None:
58
  raise ValueError("Could not find two distinct points")
 
60
  (d1_date, d1_val) = prev
61
 
62
  t0, t1 = epoch(d0_date), epoch(d1_date)
63
+ rate = (d0_val - d1_val) / (t0 - t1) # $/sec (may be negative)
64
 
65
  DEBT_STATE.update(
66
  debt_at_record = d0_val,
 
70
  )
71
 
72
  print(
73
+ f"[refresh] {d0_date=} debt={d0_val:,.2f} "
74
+ f"rate={rate:,.2f}$/s"
75
  )
76
 
77
  except Exception as e:
78
  print("Debt refresh error:", e)
79
 
80
+ time.sleep(300) # five-minute cycle
81
 
82
  threading.Thread(target=refresher, daemon=True).start()
83
 
 
88
 
89
  @app.route("/api/debt")
90
  def api_debt():
91
+ now = time.time()
92
+ # subtract 12 h guard so we never β€œproject into the future” for the
93
+ # current calendar day, yet keep full projection when record is >1 day old
94
+ elapsed = max(0.0, now - DEBT_STATE["record_time"] - 12*3600)
95
+ current = DEBT_STATE["debt_at_record"] + DEBT_STATE["rate_per_sec"] * elapsed
96
  return jsonify(
97
  startingDebt = current,
98
  ratePerSecond = DEBT_STATE["rate_per_sec"],