File size: 1,927 Bytes
e9c697f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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.