RaPTOR / api.py
Mimi Lavin
initial deploy
e9c697f
Raw
History Blame Contribute Delete
1.93 kB
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse, HTMLResponse
from pydantic import BaseModel, Field
from typing import Dict, Any
from mimis_inference import predict_philicity
from routes_ui import router as ui_router
app = FastAPI(title="Philicity API", version="1.0")
app.include_router(ui_router)
# Allow your browser page to call the API
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # later you can restrict this
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Serve the webpage + any static assets
app.mount("/static", StaticFiles(directory="static"), name="static")
# BUG FIX 1: there were two @app.get("/") definitions — the second silently
# overwrote the first. Kept only one, with the correct HTMLResponse class.
@app.get("/", response_class=HTMLResponse)
@app.head("/", response_class=HTMLResponse)
def home():
return FileResponse("static/index.html")
@app.get("/health")
def health():
return {"status": "ok"}
class PhilicityRequest(BaseModel):
original_smiles: str = Field(..., min_length=1)
radical_smiles: str = Field(..., min_length=1)
@app.post("/predict")
def predict(req: PhilicityRequest):
try:
y = predict_philicity(req.original_smiles, req.radical_smiles)
return {"philicity": y}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
# BUG FIX 2: the stub /predict_all that just echoed input back has been removed.
# The real /predict_all implementation lives in routes_ui.py and is registered
# via app.include_router(ui_router) above — so it is still fully available.
# BUG FIX 3: PredictAllRequest was defined here but only used in routes_ui.py.
# It has been removed from this file; it belongs in routes_ui.py where it is used.