File size: 2,327 Bytes
542c765
 
 
6c11af5
 
 
 
542c765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c11af5
542c765
 
6c11af5
 
542c765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# main.py — MERGED VERSION
# Source backend routers (analyze, chat, doctor_upload) + scaffold routers (nutrition, exercise)

import os
from dotenv import load_dotenv
load_dotenv()  # Load .env file before anything else

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager

from app.routers import analyze, chat, doctor_upload, nutrition, exercise


@asynccontextmanager
async def lifespan(app: FastAPI):
    # Load ML models on startup (if available)
    print("Starting ReportRaahat backend...")
    try:
        from app.ml.model import load_model
        load_model()
        print("Model loading complete.")
    except Exception as e:
        print(f"Startup info — models not fully loaded: {e}")
        print("Mock endpoints will work for testing.")
    yield
    print("Shutting down ReportRaahat backend.")


app = FastAPI(
    title="ReportRaahat API",
    description="AI-powered medical report simplifier for rural India",
    version="2.0.0",
    lifespan=lifespan
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        "http://localhost:3000",
        "http://localhost:7860",
        "https://reportraahat.vercel.app",
        "https://*.vercel.app",
        "https://*.hf.space",
        "https://huggingface.co",
    ],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ML teammate's routes
app.include_router(analyze.router, tags=["Report Analysis"])
app.include_router(chat.router, tags=["Doctor Chat"])
app.include_router(doctor_upload.router, tags=["Human Dialogue"])

# Member 4's routes
app.include_router(nutrition.router, prefix="/nutrition", tags=["Nutrition"])
app.include_router(exercise.router, prefix="/exercise", tags=["Exercise"])


@app.get("/")
async def root():
    return {
        "name": "ReportRaahat API",
        "version": "2.0.0",
        "status": "running",
        "endpoints": {
            "analyze": "POST /analyze",
            "upload_and_chat": "POST /upload_and_chat (RECOMMENDED - starts dialogue immediately)",
            "chat": "POST /chat",
            "nutrition": "POST /nutrition",
            "exercise": "POST /exercise",
            "docs": "/docs"
        }
    }


@app.get("/health")
async def health():
    return {"status": "healthy"}