File size: 6,826 Bytes
28ba0c9
d510a4f
28ba0c9
cd26350
 
6078097
89a139c
c5f836c
6078097
 
56bcac1
89a139c
85e93a2
89a139c
 
6078097
c5f836c
56bcac1
cd26350
115794d
cd26350
28ba0c9
ef25ff3
6f8a5dc
 
 
cd26350
6078097
cd26350
 
28ba0c9
cd26350
 
 
 
 
6078097
 
46061e2
 
 
 
 
6078097
46061e2
6078097
46061e2
 
 
 
6078097
46061e2
 
 
 
 
 
 
 
 
 
 
 
 
6078097
46061e2
 
 
 
 
 
 
 
 
 
 
 
6078097
 
 
 
 
 
7e9f264
115794d
 
 
 
 
 
6078097
cd26350
115794d
 
89a139c
 
115794d
 
 
 
 
9a32423
6d70778
115794d
 
 
9a32423
115794d
 
 
 
 
6078097
115794d
 
 
46061e2
115794d
 
 
46061e2
115794d
395c9ad
 
 
 
 
 
 
 
 
c5f836c
115794d
 
6078097
cd26350
115794d
 
cd26350
6f8a5dc
 
 
 
6078097
 
 
115794d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6078097
 
 
 
 
56bcac1
6078097
 
115794d
 
 
6078097
 
 
 
 
 
 
 
 
cd26350
 
 
28ba0c9
 
 
 
 
 
6885e0c
70a17d8
28ba0c9
 
 
6885e0c
28ba0c9
6885e0c
28ba0c9
 
6885e0c
28ba0c9
 
 
31e0e3b
28ba0c9
 
cb96d83
56bcac1
28ba0c9
89a139c
 
cd26350
28ba0c9
cd26350
56bcac1
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""Necthrall - AI-Powered Scientific Research Assistant.

FastAPI + NiceGUI integrated application for research paper analysis.
"""

import asyncio
import os
import sys
from contextlib import asynccontextmanager

from config.config import get_settings

# Disable tokenizers parallelism to avoid warnings
os.environ["TOKENIZERS_PARALLELISM"] = "false"

from datetime import datetime

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from loguru import logger
from nicegui import ui

# Global concurrency control
MAX_CONCURRENT_SEARCHES = 2
search_semaphore = asyncio.Semaphore(MAX_CONCURRENT_SEARCHES)

# Configure loguru
logger.remove()
logger.add(
    sys.stderr,
    format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan> - <level>{message}</level>",
    level="INFO",
)


# Move search_worker definition BEFORE app instantiation so it can be used in lifespan
async def search_worker(app_instance: FastAPI):
    """Background worker to process search queries sequentially."""
    logger.info("Search worker started")
    while True:
        try:
            # Get a "work item" out of the queue.
            # Using the passed instance instead of global 'app' for better safety
            query, deep_mode, progress_callback, future = (
                await app_instance.state.search_queue.get()
            )

            try:
                # Process the query
                result = await app_instance.state.query_service.process_query(
                    query,
                    deep_mode=deep_mode,
                    progress_callback=progress_callback,
                )
                # Set the result
                if not future.done():
                    future.set_result(result)
            except Exception as e:
                logger.exception("Error in search worker")
                if not future.done():
                    future.set_exception(e)
            finally:
                # Notify the queue that the "work item" has been processed.
                app_instance.state.search_queue.task_done()

                # Rate limiting: sleep for 1 second
                await asyncio.sleep(1.0)

        except asyncio.CancelledError:
            logger.info("Search worker cancelled")
            break
        except Exception as e:
            logger.error(f"Unexpected error in search worker loop: {e}")
            await asyncio.sleep(1.0)


@asynccontextmanager
async def lifespan(app: FastAPI):
    """
    Lifespan context manager that handles startup and shutdown logic.
    Replaces @app.on_event("startup").
    """

    # Initialize state attributes to avoid AttributeErrors if startup fails
    app.state.search_queue = asyncio.Queue()
    app.state.query_service = None
    app.state.embedding_model = None
    app.state.workers = []

    # --- STARTUP LOGIC ---
    try:
        from config.config import get_settings
        from config.embedding_config import init_embedding
        from services.query_service import QueryService

        # 1. Load Settings
        settings = get_settings()
        settings.validate_keys()

        # 2. Init Embedding Model (puts it on app.state.embedding_model)
        embedding_model = None
        try:
            embedding_model = await init_embedding()
            app.state.embedding_model = embedding_model
            if embedding_model:
                logger.info("โœ… Embedding model loaded successfully")
            else:
                logger.warning("โš ๏ธ Embedding model failed to load")
        except Exception as e:
            logger.warning(f"โš ๏ธ Embedding model initialization failed: {e}")
            # app.state.embedding_model is already None

        # 3. Create Singleton QueryService
        # This will internally create Discovery, Ingestion, and RAG services
        query_service = QueryService(settings=settings, embedding_model=embedding_model)

        # 4. Store in State
        app.state.query_service = query_service
        logger.info("๐Ÿš€ Query service initialized")

        # 5. Init Workers
        # Start N workers for better concurrency
        CONCURRENT_WORKERS: int = 1
        app.state.workers = [
            asyncio.create_task(search_worker(app)) for _ in range(CONCURRENT_WORKERS)
        ]

        logger.info(
            f"๐Ÿš€ {CONCURRENT_WORKERS} Search workers initialized (Parallel Lanes)"
        )
        logger.info("โœ… Application startup successful")
        logger.info(f"Query optimization model: {settings.QUERY_OPTIMIZATION_MODEL}")
        logger.info(f"Synthesis model: {settings.SYNTHESIS_MODEL}")

    except Exception as e:
        logger.critical(f"Failed to initialize application: {e}")
        # app.state.query_service is already None

    # Expose concurrency controls to UI
    app.state.search_semaphore = search_semaphore
    app.state.max_concurrent_searches = MAX_CONCURRENT_SEARCHES

    yield  # Application runs here

    # --- SHUTDOWN LOGIC ---
    logger.info("Shutting down Necthrall...")

    # Cancel workers
    if hasattr(app.state, "workers"):
        logger.info("๐Ÿ›‘ Shutting down: Cancelling workers...")
        for worker in app.state.workers:
            worker.cancel()
        await asyncio.gather(*app.state.workers, return_exceptions=True)

    # Close services
    if hasattr(app.state, "query_service") and app.state.query_service:
        await app.state.query_service.close()

    logger.info("Shutdown complete")


app = FastAPI(
    title="Necthrall API",
    description="AI-powered scientific research assistant",
    version="3.0.0",
    lifespan=lifespan,
)

# Mount static files
app.mount("/static", StaticFiles(directory="static"), name="static")

# Configure CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.get("/health")
async def health_check():
    """Health check endpoint."""
    return {
        "status": "healthy",
        "timestamp": datetime.now().isoformat(),
        "version": "3.0.0",
    }


# =============================================================================
# NiceGUI Frontend
# =============================================================================

from ui.pages import init_ui

# Initialize UI with FastAPI app reference
init_ui(app)

# Mount NiceGUI with FastAPI
ui.run_with(
    app,
    title="Nechtrall - Science, Distilled.",
    favicon="logo/favicon.png",
    dark=False,
    reconnect_timeout=30.0,
    storage_secret=get_settings().NICEGUI_STORAGE_SECRET,
)


if __name__ == "__main__":
    import uvicorn

    uvicorn.run("main:app", host="0.0.0.0", port=7860, log_level="info", reload=False)