File size: 4,147 Bytes
12d60da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
app.py
------
Entry point aplikasi BrainScan AI.

Tanggung jawab file ini HANYA:
  1. Membuat instance FastAPI
  2. Mengatur CORS
  3. Membuat folder output yang diperlukan
  4. Menyertakan (include) seluruh route dari api.py
  5. Mount static files (frontend) & folder figures (heatmap)

Semua logika endpoint (load model, /api/analyze/, dll) ada di api.py β€”
file ini sengaja dibuat "tipis", cuma perakitan (wiring) doang.

Jalankan dari root proyek (folder yang berisi folder src/):
    uvicorn src.app:app --reload
    # atau
    python -m src.app
"""

import os

from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware

from src.config import OUTPUT_DIR
from src.api import router as api_router

# ─────────────────────────────────────────────────────────────
# 1. Inisialisasi Aplikasi FastAPI
# ─────────────────────────────────────────────────────────────
app = FastAPI(
    title="BrainScan AI Framework API",
    description="API untuk analisis otomatis CT-Scan & MRI menggunakan arsitektur Hybrid CNN-Transformer",
    version="1.0",
)

# ─────────────────────────────────────────────────────────────
# 2. CORS β€” biar frontend (domain/origin manapun) bisa akses API ini
# ─────────────────────────────────────────────────────────────
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ─────────────────────────────────────────────────────────────
# 3. Folder output yang diperlukan saat runtime
# ─────────────────────────────────────────────────────────────
os.makedirs(os.path.join(OUTPUT_DIR, "figures"), exist_ok=True)
os.makedirs("temp_uploads", exist_ok=True)

# ─────────────────────────────────────────────────────────────
# 4. Sambungkan semua endpoint dari api.py
#    (mengimpor src.api otomatis menjalankan proses load model
#    di dalamnya, lihat komentar di bagian atas api.py)
# ─────────────────────────────────────────────────────────────
app.include_router(api_router)

# ─────────────────────────────────────────────────────────────
# 5. Mount static files
#    - /outputs/figures -> hasil heatmap Grad-CAM (fallback akses langsung)
#    - /                 -> frontend (index.html, app.js, index.css)
#    Urutan penting: mount("/") harus PALING TERAKHIR supaya tidak
#    "menutupi" route API yang sudah didaftarkan lewat include_router.
# ─────────────────────────────────────────────────────────────
app.mount(
    "/outputs/figures",
    StaticFiles(directory=os.path.join(OUTPUT_DIR, "figures")),
    name="figures",
)
app.mount("/", StaticFiles(directory="src/static", html=True), name="static")


if __name__ == "__main__":
    import uvicorn
    # Jalankan dari ROOT proyek (bukan dari dalam folder src/):
    #   uvicorn src.app:app --reload
    uvicorn.run("src.app:app", host="127.0.0.1", port=8000, reload=True)