cloud_end / app.py
05prey's picture
Update app.py
4866ec9 verified
Raw History Blame Contribute Delete
1.79 kB
import os, time, json
from pathlib import Path
from fastapi import FastAPI, UploadFile, File
from fastapi.responses import HTMLResponse, JSONResponse
app = FastAPI()
POLICY_PATH = Path("./policy.json")
@app.get("/policy")
def get_policy():
if not POLICY_PATH.exists():
return JSONResponse({"error": "policy.json not found"}, status_code=404)
data = json.loads(POLICY_PATH.read_text(encoding="utf-8"))
return JSONResponse(data)
@app.get("/", response_class=HTMLResponse)
def home():
return """
<h2>Dummy Upload Sink</h2>
<p>POST to <code>/upload</code></p>
"""
@app.post("/upload")
async def upload_sink(file: UploadFile = File(...)):
start = time.time()
total = 0
while True:
chunk = await file.read(1024 * 1024)
if not chunk:
break
total += len(chunk)
dt = time.time() - start
mb = total / (1024**2)
stats = {
"filename": file.filename,
"received_mb": round(mb, 2),
"duration_sec": round(dt, 2),
"throughput_mb_s": round(mb / dt, 2) if dt > 0 else 0.0,
}
print("receiving:", stats)
# # (Optional) save if you want
# os.makedirs("uploads", exist_ok=True)
# with open(os.path.join("uploads", file.filename), "wb") as f:
# f.write(data)
# ✅ return what the frontend expects
resp = {
"birads": "BIRADS 2",
"molecular_subtype": "Luminal A",
"report": f"Received {file.filename} ({mb:.2f} MB). Upload time {dt:.2f}s.",
"segmentedImageSrc": None,
# You can also include your perf stats; UI will ignore unknown keys
"stats": stats
}
return resp
# Optional: print PORT for debugging in container logs
print("DEBUG PORT env:", os.environ.get("PORT"), flush=True)