Spaces:
Sleeping
Sleeping
Upload routes/agent.py with huggingface_hub
Browse files- routes/agent.py +70 -0
routes/agent.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Header, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from firebase_auth import verify_token
|
| 4 |
+
from agent.toxicologist_agent import ToxicologistAgent
|
| 5 |
+
from agent.structural_alerts import identify_structural_alerts
|
| 6 |
+
from config import TASK_NAMES
|
| 7 |
+
|
| 8 |
+
router = APIRouter(prefix="/api")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class AgentRequest(BaseModel):
|
| 12 |
+
smiles: str
|
| 13 |
+
predictions: list
|
| 14 |
+
shap_attributions: list[float]
|
| 15 |
+
high_impact_atoms: list[int]
|
| 16 |
+
target_assay: str = "NR-AR"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class AgentResponse(BaseModel):
|
| 20 |
+
target_assay: str
|
| 21 |
+
risk_level: str
|
| 22 |
+
toxicophore_assessment: str
|
| 23 |
+
biochemical_mechanism: str
|
| 24 |
+
structural_alerts_found: list
|
| 25 |
+
bioisosteric_replacements: list
|
| 26 |
+
confidence: str
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
_agent = None
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def get_agent():
|
| 33 |
+
global _agent
|
| 34 |
+
if _agent is None:
|
| 35 |
+
_agent = ToxicologistAgent()
|
| 36 |
+
return _agent
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
@router.post("/agent")
|
| 40 |
+
async def agent_analysis(request: AgentRequest, authorization: str = Header(...)):
|
| 41 |
+
await verify_token(authorization)
|
| 42 |
+
|
| 43 |
+
agent = get_agent()
|
| 44 |
+
|
| 45 |
+
alerts = identify_structural_alerts(
|
| 46 |
+
request.smiles,
|
| 47 |
+
high_attribution_indices=request.high_impact_atoms,
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
report = agent.generate_report(
|
| 52 |
+
smiles=request.smiles,
|
| 53 |
+
target_assay=request.target_assay,
|
| 54 |
+
predictions=request.predictions,
|
| 55 |
+
shap_attributions=request.shap_attributions,
|
| 56 |
+
high_attr_indices=request.high_impact_atoms,
|
| 57 |
+
alerts_found=alerts,
|
| 58 |
+
)
|
| 59 |
+
except Exception as e:
|
| 60 |
+
raise HTTPException(status_code=500, detail=f"Agent analysis failed: {str(e)}")
|
| 61 |
+
|
| 62 |
+
return AgentResponse(
|
| 63 |
+
target_assay=report.target_assay,
|
| 64 |
+
risk_level=report.risk_level,
|
| 65 |
+
toxicophore_assessment=report.toxicophore_assessment,
|
| 66 |
+
biochemical_mechanism=report.biochemical_mechanism,
|
| 67 |
+
structural_alerts_found=alerts,
|
| 68 |
+
bioisosteric_replacements=report.bioisosteric_replacements,
|
| 69 |
+
confidence=report.confidence,
|
| 70 |
+
)
|