File size: 1,817 Bytes
8a0d2b8
 
 
 
18e32e1
8a0d2b8
 
 
 
 
 
 
4c1b5d1
8a0d2b8
 
 
 
 
 
 
 
 
 
 
 
 
4eb4266
 
 
 
 
 
 
 
 
 
 
 
8a0d2b8
 
 
 
 
 
e5e228d
8a0d2b8
 
 
 
 
 
 
4e3e852
4c1b5d1
8a0d2b8
18e32e1
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
"""
This is the main entry point for the FastAPI application.
The app handles the request to predict toxicity for a list of SMILES strings.
"""

#---------------------------------------------------------------------------------------
# Dependencies and global variable definition
import os
from typing import List, Dict, Optional
from fastapi import FastAPI, Header, HTTPException
from pydantic import BaseModel, Field

from predict import predict as predict_func

API_KEY = os.getenv("API_KEY")  # set via Space Secrets

#---------------------------------------------------------------------------------------
class Request(BaseModel):
    smiles: List[str] = Field(min_items=1, max_items=1000)

class Response(BaseModel):
    predictions: dict
    model_info: Dict[str, str] = {}

app = FastAPI(title="toxicity-api")

@app.get("/")
def root():
    return {
        "message": "Toxicity Prediction API",
        "endpoints": {
            "/metadata": "GET - API metadata and capabilities",
            "/healthz": "GET - Health check",
            "/predict": "POST - Predict toxicity for SMILES"
        },
        "usage": "Send POST to /predict with {'smiles': ['your_smiles_here']} and Authorization header"
    }

@app.get("/metadata")
def metadata():
    return {
        "name": "AwesomeTox",
        "version": "1.0.0",
        "max_batch_size": 256,
        "tox_endpoints": ["NR-AR", "NR-AR-LBD", "NR-AhR", "NR-Aromatase", "NR-ER", "NR-ER-LBD", "NR-PPAR-gamma", "SR-ARE", "SR-ATAD5", "SR-HSE", "SR-MMP", "SR-p53"],
    }

@app.get("/healthz")
def healthz():
    return {"ok": True}

@app.post("/predict", response_model=Response)
def predict(request: Request):
    predictions = predict_func(request.smiles)
    return {"predictions": predictions, "model_info": {"name":"random_clf", "version":"1.0.0"}}