s-ttp commited on
Commit
cdf0657
·
verified ·
1 Parent(s): e01e80e

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +18 -2
app.py CHANGED
@@ -9,7 +9,7 @@ import os
9
  import threading
10
  from datetime import datetime, timezone
11
 
12
- from fastapi import FastAPI, Header, HTTPException
13
  from fastapi.responses import FileResponse, StreamingResponse
14
  from fastapi.staticfiles import StaticFiles
15
 
@@ -63,7 +63,20 @@ def health():
63
 
64
 
65
  @app.post("/api/analyst/{ticker}")
66
- def analyst_start(ticker: str):
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  # Resolve FIRST (ticker or company name -> verified symbol) so a typo neither burns a
68
  # daily-cap slot nor launches an engine run that dies with a raw "failed (exit 1)".
69
  try:
@@ -78,6 +91,9 @@ def analyst_start(ticker: str):
78
  detail=f"Daily analysis limit reached ({cap}/day). This resets at 00:00 UTC — please try again then.")
79
  try:
80
  out = {"job_id": analyst.start(res["symbol"]), "ticker": res["symbol"]}
 
 
 
81
  if res.get("resolved_from"):
82
  out["resolved"] = {"from": res["resolved_from"], "name": res.get("name")}
83
  return out
 
9
  import threading
10
  from datetime import datetime, timezone
11
 
12
+ from fastapi import FastAPI, Header, HTTPException, Request
13
  from fastapi.responses import FileResponse, StreamingResponse
14
  from fastapi.staticfiles import StaticFiles
15
 
 
63
 
64
 
65
  @app.post("/api/analyst/{ticker}")
66
+ def analyst_start(ticker: str, request: Request, x_crucible_client: str = Header(default=""),
67
+ x_prewarm_token: str = Header(default="")):
68
+ # Only the Crucible web page (or the token-holding prewarm job) may start a run. The
69
+ # Space is public and every run spends LLM budget; an anonymous script posted an AAPL
70
+ # analysis on 2026-09-06 with no page load and never read the result. The header is
71
+ # trivial to copy, but it stops crawlers and API guessers, and every run now records
72
+ # who started it (see job["client"] / ledger "client").
73
+ _tok = os.getenv("PREWARM_TOKEN", "")
74
+ if x_crucible_client != "ui" and not (_tok and x_prewarm_token == _tok):
75
+ raise HTTPException(status_code=403, detail="analyses can only be started from the Crucible web page")
76
+ _client = {"ua": (request.headers.get("user-agent") or "")[:120],
77
+ "referer": (request.headers.get("referer") or "")[:120],
78
+ "ip": (request.headers.get("x-forwarded-for") or (request.client.host if request.client else "") or "")[:64],
79
+ "via": ("ui" if x_crucible_client == "ui" else "token")}
80
  # Resolve FIRST (ticker or company name -> verified symbol) so a typo neither burns a
81
  # daily-cap slot nor launches an engine run that dies with a raw "failed (exit 1)".
82
  try:
 
91
  detail=f"Daily analysis limit reached ({cap}/day). This resets at 00:00 UTC — please try again then.")
92
  try:
93
  out = {"job_id": analyst.start(res["symbol"]), "ticker": res["symbol"]}
94
+ _j = analyst._jobs.get(out["job_id"])
95
+ if _j is not None and not _j.get("client"):
96
+ _j["client"] = _client; _j["source"] = _client["via"]
97
  if res.get("resolved_from"):
98
  out["resolved"] = {"from": res["resolved_from"], "name": res.get("name")}
99
  return out