File size: 2,029 Bytes
3822931
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22055af
 
 
 
 
 
3822931
 
 
d7bb799
3822931
d7bb799
3822931
 
 
 
 
de6c365
3822931
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, File, UploadFile, Form
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import shutil
import tempfile
import os
import json

# Orquestador Médico
from ADNI_Fusion_Engine import PerdixAdniEngine

app = FastAPI(title="Perdix ADNI Medical Node", version="1.0.0")

# Permitir a NextJS conectarse a este motor en Hugging Face
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"], # En producción cambiar por la URL de vercel
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

print("Inicializando Motor ADNI Neuronal en Hugging Face...")
engine = PerdixAdniEngine()

@app.get("/")
def read_root():
    return {"status": "online", "message": "Perdix ADNI Engine is running. Send POST to /diagnose."}

@app.post("/diagnose")
async def diagnose(

    age: float = Form(65.0), 

    mmse_score: float = Form(25.0), 

    edu_years: float = Form(12.0),

    mri_scan: UploadFile = File(...)

):
    try:
        # Guardar la imagen subida en disco temporalmente
        ext = os.path.splitext(mri_scan.filename)[1]
        if not ext:
            ext = ".png"
            
        with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as tmp:
            shutil.copyfileobj(mri_scan.file, tmp)
            tmp_path = tmp.name
        
        # Ejecutamos la Inferencia con PyTorch
        result = engine.diagnose_patient(
            image_path=tmp_path, 
            age=age, 
            mmse_score=mmse_score, 
            edu_years=edu_years
        )
        
        # Inyectar ID Clínico
        import uuid
        result["diagnosis_id"] = "ADNI-" + str(uuid.uuid4())[:8]
        
        # Limpieza de Disco 
        os.unlink(tmp_path)
        
        return result
        
    except Exception as e:
        return {"status": "error", "message": str(e)}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)