File size: 10,681 Bytes
03677d0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | """HF Spaces server: static experience plus a bounded, single-model job queue."""
import logging
import os
import queue
import re
import threading
import time
import uuid
from contextlib import asynccontextmanager
from pathlib import Path
from typing import Literal
from fastapi import FastAPI, HTTPException, Request
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from starlette.middleware.gzip import GZipMiddleware
from pydantic import BaseModel, Field, field_validator
from atlas_service import AtlasService
ROOT = Path(__file__).resolve().parent
log = logging.getLogger('ouroboros')
ORDER = ['similarity', 'function', 'subunit', 'catalytic activity', 'pathway', 'cofactor', 'tissue specificity', 'domain', 'subcellular location', 'PTM', 'miscellaneous', 'induction', 'disruption phenotype', 'activity regulation', 'developmental stage', 'sequence caution', 'caution', 'biophysicochemical properties', 'disease', 'alternative products', 'online information', 'biotechnology', 'polymorphism', 'mass spectrometry', 'allergen', 'toxic dose', 'RNA editing', 'pharmaceutical', 'interaction']
class GenerationRequest(BaseModel):
direction: Literal['t2p', 'p2t']
input: str = Field(min_length=1, max_length=8000)
temperature: float = Field(default=1, ge=0.1, le=2)
greedy: bool = False
max_length: int = Field(default=512, ge=32, le=512)
@field_validator('input')
@classmethod
def nonempty(cls, value):
if not value.strip():
raise ValueError('Enter a description or a protein sequence.')
return value.strip()
def normalize_input(request):
if request.direction == 't2p':
return request.input, None
lines = request.input.splitlines()
headers = [i for i, s in enumerate(lines) if s.lstrip().startswith('>')]
if headers and (len(headers) != 1 or headers[0] != 0):
raise HTTPException(422, 'Please submit one protein sequence at a time.')
sequence = re.sub(r'\s+', '', ''.join(lines[1:] if headers else lines)).upper()
if not sequence or re.search(r'[^ACDEFGHIKLMNPQRSTVWYXBZOU]', sequence):
raise HTTPException(422, 'Use amino-acid letters only, optionally with one FASTA header.')
if len(sequence) > 511:
raise HTTPException(422, 'The original model accepts at most 511 residues. Please shorten the sequence.')
normalized = re.sub('[BZOU]', 'X', sequence)
return normalized, 'Non-standard residues B/Z/O/U were converted to X.' if normalized != sequence else None
class ModelRunner:
def __init__(self):
self.model = None
def load(self):
import torch
from ouroboros_model import OuroborosModel
from huggingface_hub import hf_hub_download
torch.set_num_threads(int(os.environ.get('OUROBOROS_CPU_THREADS', '4')))
checkpoint = os.environ.get('CHECKPOINT_PATH')
if checkpoint:
if not Path(checkpoint).is_file():
raise FileNotFoundError('Configured checkpoint is unavailable')
else:
checkpoint = hf_hub_download(repo_id=os.environ.get('OUROBOROS_MODEL_REPO', 'REXWind/ouroboros-weights'), filename='dual_model.pt')
device = os.environ.get('OUROBOROS_DEVICE', 'cpu')
model = OuroborosModel(
t5config_path=str(ROOT / 't5config_large.json'),
protein_tokenizer_path=os.environ.get('PROTEIN_TOKENIZER_PATH'),
text_tokenizer_path=os.environ.get('TEXT_TOKENIZER_PATH'),
text_share_param=True, protein_share_param=True, share_fc=True, use_mean_pooling=False,
)
model.load_checkpoint(checkpoint)
model.to(device).eval()
self.model = model
def generate(self, request, normalized):
import torch
if request.direction == 't2p':
# Reject silent text truncation before invoking the archived model.
tokens = self.model.text_tokenizer(normalized, add_special_tokens=True)['input_ids']
if len(tokens) > 512:
raise ValueError('This description exceeds 512 text tokens. Please shorten it.')
self.model.max_generation_length = request.max_length
args = {'use_greedy': request.greedy, 'temperature': request.temperature, 'top_k': 40, 'top_p': 0.9, 'num_beams': 1 if request.greedy else 4}
with torch.inference_mode():
if request.direction == 'p2t':
return self.model.protein_to_text([' '.join(normalized)], **args)[0]
return self.model.text_to_protein([normalized], num_return_sequences=1, **args)[0]
class InferenceEngine:
def __init__(self, runner=None, disabled=None):
self.runner = runner or ModelRunner()
self.disabled = os.environ.get('OUROBOROS_INFERENCE') == 'disabled' if disabled is None else disabled
self.state = 'disabled' if self.disabled else 'cold'
self.jobs = {}
self.lock = threading.RLock()
self.pending = queue.Queue(maxsize=5)
self.stop = threading.Event()
self.thread = None
def start(self):
if not self.disabled:
self.thread = threading.Thread(target=self.work, name='model-worker', daemon=True)
self.thread.start()
def close(self):
self.stop.set()
if self.thread:
self.thread.join(timeout=1)
def submit(self, request):
normalized, note = normalize_input(request)
if self.disabled:
raise HTTPException(503, 'Live inference is unavailable. The Core Idea section contains a recorded demonstration.')
with self.lock:
now = time.time()
self.jobs = {k: v for k, v in self.jobs.items() if now - v['created'] < 1800 or v['status'] in ['queued', 'loading', 'running']}
if len(self.jobs) >= 100:
oldest = next((k for k, v in self.jobs.items() if v['status'] in ['completed', 'failed']), None)
if oldest:
del self.jobs[oldest]
key = uuid.uuid4().hex
job = {'id': key, 'status': 'queued', 'created': now, 'direction': request.direction, 'normalized_input': normalized, 'normalization_note': note, 'model': 'Ouroboros large-v5', 'output': None, 'error': None}
self.jobs[key] = job
try:
self.pending.put_nowait((key, request))
except queue.Full:
del self.jobs[key]
raise HTTPException(429, 'The model queue is full. Please try again shortly.')
return {'id': key, 'status': 'queued'}
def read(self, key):
with self.lock:
if key not in self.jobs:
raise HTTPException(404, 'This job does not exist or has expired.')
return dict(self.jobs[key])
def work(self):
while not self.stop.is_set():
try:
key, request = self.pending.get(timeout=0.2)
except queue.Empty:
continue
try:
with self.lock:
job = self.jobs[key]
job['status'] = 'loading' if self.state != 'ready' else 'running'
if self.state != 'ready':
self.state = 'loading'
self.runner.load()
self.state = 'ready'
with self.lock:
job['status'] = 'running'
output = self.runner.generate(request, job['normalized_input'])
if not output or not output.strip():
raise ValueError('The model returned an empty result. Try another input or sampling.')
with self.lock:
job.update(status='completed', output=output, finished=time.time())
except Exception as exc:
log.exception('Generation job failed')
if self.state == 'loading':
self.state = 'error'
with self.lock:
job.update(status='failed', error=str(exc) if isinstance(exc, ValueError) else 'Inference is unavailable. Please retry later.', finished=time.time())
finally:
self.pending.task_done()
def create_app(engine=None, atlas=None):
worker = engine or InferenceEngine()
atlas = atlas or AtlasService()
@asynccontextmanager
async def lifespan(app):
worker.start()
yield
worker.close()
app = FastAPI(title='Ouroboros', lifespan=lifespan)
app.add_middleware(GZipMiddleware, minimum_size=1024, compresslevel=5)
app.state.engine = worker
app.state.atlas = atlas
@app.get('/api/atlas/manifest')
def atlas_manifest():
return atlas.manifest()
@app.get('/api/atlas/proteins/{identifier}')
def atlas_protein(identifier: str):
return atlas.protein(identifier)
@app.get('/api/health')
def health():
return {'status': 'ok', 'model': 'Ouroboros large-v5', 'inference': worker.state, 'queue': worker.pending.qsize()}
@app.post('/api/jobs', status_code=202)
def submit(request: GenerationRequest):
return worker.submit(request)
@app.get('/api/jobs/{key}')
def get_job(key: str):
return worker.read(key)
@app.get('/api/prompt-fields')
def prompt_fields():
return {'fields': ORDER}
if (ROOT / 'dist').is_dir():
@app.get('/data/atlas-map.json', include_in_schema=False)
@app.get('/data/atlas-preview.json', include_in_schema=False)
def atlas_map_asset(request: Request):
path = ROOT / 'dist' / 'data' / ('atlas-preview.json' if request.url.path.endswith('atlas-preview.json') else 'atlas-map.json')
compressed = path.with_suffix('.json.gz')
headers = {'Vary': 'Accept-Encoding'}
if 'gzip' in request.headers.get('accept-encoding', '') and compressed.is_file():
path = compressed
headers['Content-Encoding'] = 'gzip'
response = FileResponse(path, media_type='application/json', headers=headers)
# Keep this immutable map's transfer bounded to a few large writes.
# Precompression supplies Content-Length and avoids many tiny gzip
# chunks traversing the Space gateway on every page load.
response.chunk_size = 1024 * 1024
return response
@app.get('/atlas', include_in_schema=False)
@app.get('/atlas/', include_in_schema=False)
def atlas_page():
return FileResponse(ROOT / 'dist' / 'index.html')
app.mount('/', StaticFiles(directory=ROOT / 'dist', html=True), name='frontend')
return app
app = create_app()
|