File size: 3,832 Bytes
41943e0
88f8604
 
41943e0
 
88f8604
41943e0
88f8604
41943e0
 
88f8604
 
 
41943e0
88f8604
41943e0
88f8604
 
 
 
 
41943e0
 
88f8604
41943e0
 
88f8604
41943e0
 
 
 
 
88f8604
 
 
 
 
41943e0
 
 
88f8604
 
 
 
41943e0
88f8604
 
41943e0
88f8604
 
41943e0
88f8604
41943e0
88f8604
 
41943e0
88f8604
 
 
41943e0
88f8604
 
41943e0
88f8604
 
 
 
 
41943e0
 
88f8604
 
 
 
 
 
 
 
 
 
41943e0
88f8604
 
 
 
 
41943e0
 
88f8604
 
 
 
41943e0
88f8604
 
 
 
 
 
 
 
 
 
 
41943e0
88f8604
 
 
 
 
 
 
41943e0
88f8604
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41943e0
88f8604
 
 
41943e0
 
 
88f8604
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""
Antigravity Notebook - Main FastAPI Application
Entry point for the backend API server.
"""

from fastapi import FastAPI, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
import uvicorn

from backend.config import settings
from backend.database import init_db, engine
from backend.routers import notebooks, sources, chat

# Create FastAPI app
app = FastAPI(
    title="Antigravity Notebook",
    description="NotebookLM clone using CLaRa-7B latent compression for infinite context reasoning",
    version="1.0.0",
    docs_url="/docs",
    redoc_url="/redoc"
)

# CORS middleware (for Streamlit frontend)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # In production, specify your frontend URL
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Include routers
app.include_router(notebooks.router)
app.include_router(sources.router)
app.include_router(chat.router)


@app.on_event("startup")
async def startup_event():
    """Initialize database and services on startup"""
    print("\n" + "="*60)
    print("πŸš€ Starting Antigravity Notebook")
    print("="*60)

    # Initialize database
    print("\nπŸ“Š Initializing database...")
    try:
        init_db()
        print("βœ… Database ready!")
    except Exception as e:
        print(f"❌ Database initialization failed: {e}")

    # Pre-load CLaRa model (optional - can be lazy loaded)
    print("\nπŸ€– Initializing CLaRa model...")
    try:
        from backend.models.clara import get_clara_model
        clara = get_clara_model()
        print("βœ… CLaRa model loaded!")
    except Exception as e:
        print(f"⚠️  CLaRa model failed to load: {e}")
        print("   Model will be loaded on first request")

    print("\n" + "="*60)
    print(f"βœ… Antigravity Notebook is ready!")
    print(f"πŸ“ API: http://{settings.API_HOST}:{settings.API_PORT}")
    print(f"πŸ“š Docs: http://{settings.API_HOST}:{settings.API_PORT}/docs")
    print("="*60 + "\n")


@app.on_event("shutdown")
async def shutdown_event():
    """Cleanup on shutdown"""
    print("\nπŸ‘‹ Shutting down Antigravity Notebook...")
    engine.dispose()


@app.get("/", tags=["root"])
def root():
    """Root endpoint - health check"""
    return {
        "service": "Antigravity Notebook",
        "version": "1.0.0",
        "status": "running",
        "description": "NotebookLM clone with CLaRa-7B latent compression",
        "docs": "/docs"
    }


@app.get("/health", tags=["root"])
def health_check():
    """Health check endpoint"""
    try:
        # Check database connection
        from backend.database import SessionLocal
        db = SessionLocal()
        db.execute("SELECT 1")
        db.close()

        return {
            "status": "healthy",
            "database": "connected",
            "api": "running"
        }
    except Exception as e:
        return JSONResponse(
            status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
            content={
                "status": "unhealthy",
                "error": str(e)
            }
        )


@app.get("/stats", tags=["root"])
def get_stats():
    """Get storage statistics"""
    from backend.services.storage import get_storage_service

    storage = get_storage_service()
    stats = storage.get_storage_stats()

    return {
        "storage": stats,
        "config": {
            "max_context_tokens": settings.MAX_CONTEXT_TOKENS,
            "compression_ratio": settings.COMPRESSION_RATIO,
            "model": settings.MODEL_NAME
        }
    }


def main():
    """Run the application"""
    uvicorn.run(
        "backend.main:app",
        host=settings.API_HOST,
        port=settings.API_PORT,
        reload=True,
        log_level="info"
    )


if __name__ == "__main__":
    main()