Spaces:
Running
Add /fold endpoint proxying NVIDIA NIM ESMFold
Browse filesFrontend §5 (the "From sequence to structure" section) needs predicted
3D structures to render. We proxy through the backend so the NIM API key
stays on the server.
Body: {"sequence": "<AA>"}
Returns: {"pdb": str, "n_residues": int, "plddt_mean": float, "cached": bool}
or {"error": str} on failure.
Implementation notes:
- httpx (already pulled in transitively by openai) replaces urllib so SSL
works out of the box on macOS without certifi gymnastics. Declared
explicitly in requirements.txt.
- Sequence is filtered to the 20 standard AAs before submission — NIM
rejects anything outside that charset (incl. stop codons "*"), and
callers shouldn't need to know the exact regex.
- Truncated to 1024 aa (NIM's hard cap).
- pLDDT mean is extracted from the B-factor column on CA atoms so the
frontend can show a global confidence score without re-parsing the PDB.
- sha1(sequence) → result FIFO cache (max 256 entries) since ESMFold is
deterministic. Demo viewers re-folding the same gene over and over
cost zero NIM calls.
Smoke-tested locally:
47 aa peptide → 0.55s, pLDDT 66.9
HBB 147 aa → 0.62s, pLDDT 93.8
cache hit → 0.037s (16x speedup)
Co-authored-by: Cursor <cursoragent@cursor.com>
Files changed (2)
- app.py+108
- requirements.txt+1
1234568 unmodified lines151617181920156 unmodified lines177178179import jsonimport osfrom fastapi import FastAPI, Requestfrom fastapi.responses import FileResponse, StreamingResponsefrom fastapi.staticfiles import StaticFiles8 unmodified lines"hf-carbon/carbon-3B-hybrid-loss-1T-mix2-v1",)HERE = os.path.dirname(os.path.abspath(__file__))156 unmodified linesyield f"data: {json.dumps({'error': str(e)})}\n\n"return StreamingResponse(stream(), media_type="text/event-stream")123456788 unmodified lines17181920212223242526272829303132333435363738156 unmodified lines195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287import hashlibimport jsonimport osimport httpxfrom fastapi import FastAPI, Requestfrom fastapi.responses import FileResponse, StreamingResponsefrom fastapi.staticfiles import StaticFiles8 unmodified lines"hf-carbon/carbon-3B-hybrid-loss-1T-mix2-v1",)# NVIDIA NIM ESMFold endpoint (alignment-free protein structure prediction).# Schema: POST {"sequence": "<AA>"} → {"pdbs": ["<PDB string>"]}.# Constraints: max 1024 aa, charset = 20 standard AAs only.NIM_FOLD_URL = os.environ.get("NIM_FOLD_URL","https://health.api.nvidia.com/v1/biology/nvidia/esmfold",)FOLD_MAX_LEN = 1024FOLD_AA_ALPHABET = "ARNDCQEGHILKMFPSTWYV"# In-memory cache: sha1(sequence) → result dict. ESMFold is deterministic at# temperature 0, so caching is safe and lets demo viewers replay the same# protein for free. Bounded to keep memory predictable on long-running Spaces._FOLD_CACHE: dict[str, dict] = {}_FOLD_CACHE_MAX = 256HERE = os.path.dirname(os.path.abspath(__file__))156 unmodified linesyield f"data: {json.dumps({'error': str(e)})}\n\n"return StreamingResponse(stream(), media_type="text/event-stream")def _extract_plddt(pdb: str) -> list[float]:"""Pull the per-residue pLDDT confidence out of the PDB B-factor column.ESMFold writes its pLDDT score (0-100) into the B-factor field of everyatom. We sample CA atoms only so we get exactly one value per residue."""plddts: list[float] = []for line in pdb.split("\n"):if not line.startswith("ATOM"):continueif line[12:16].strip() != "CA":continuetry:plddts.append(float(line[60:66]))except (ValueError, IndexError):passreturn plddts@app.post("/fold")async def fold(request: Request):"""Predict a protein's 3D structure from its amino-acid sequence.Body: {"sequence": "<AA>"}Returns on success: {"pdb": str, "n_residues": int, "plddt_mean": float}Returns on failure: {"error": str}Implementation: thin proxy in front of NVIDIA NIM's ESMFold endpoint.We strip non-standard characters (e.g. stop codons), enforce the 1024 aacap, and cache results by sha1(sequence) — ESMFold is deterministic socaching is safe and free."""body = await request.json()raw = (body.get("sequence") or "").upper()# NIM rejects anything outside the 20 standard AAs; strip eagerly so the# caller doesn't need to know the exact regex.seq = "".join(c for c in raw if c in FOLD_AA_ALPHABET)if not seq:return {"error": "sequence empty after filtering to standard amino acids"}if len(seq) > FOLD_MAX_LEN:seq = seq[:FOLD_MAX_LEN]key = hashlib.sha1(seq.encode()).hexdigest()cached = _FOLD_CACHE.get(key)if cached is not None:return {**cached, "cached": True}api_key = os.environ.get("NVIDIA_API_KEY")if not api_key:return {"error": "no NVIDIA_API_KEY env var — set it in .env"}try:with httpx.Client(timeout=120.0) as client:resp = client.post(NIM_FOLD_URL,json={"sequence": seq},headers={"Authorization": f"Bearer {api_key}","Accept": "application/json",},)except httpx.RequestError as e:return {"error": f"NIM call failed: {e}"}if resp.status_code != 200:return {"error": f"NIM HTTP {resp.status_code}: {resp.text[:300]}"}try:data = resp.json()except json.JSONDecodeError as e:return {"error": f"NIM returned non-JSON: {e}"}pdb = (data.get("pdbs") or [None])[0]if not pdb:return {"error": "NIM response had no PDB payload"}plddts = _extract_plddt(pdb)result = {"pdb": pdb,"n_residues": len(plddts),"plddt_mean": (sum(plddts) / len(plddts)) if plddts else None,}# FIFO eviction. Dicts preserve insertion order in Python 3.7+ so the# oldest entry is always next(iter(...)). Crude but the cache is a perf# nicety, not a correctness mechanism.if len(_FOLD_CACHE) >= _FOLD_CACHE_MAX:_FOLD_CACHE.pop(next(iter(_FOLD_CACHE)), None)_FOLD_CACHE[key] = resultreturn result
1 unmodified line2341 unmodified lineuvicorn[standard]>=0.27openai>=1.40huggingface_hub>=0.241 unmodified line23451 unmodified lineuvicorn[standard]>=0.27openai>=1.40huggingface_hub>=0.24httpx>=0.27