christopher
commited on
Commit
·
27c3646
1
Parent(s):
552befd
Added simple logging
Browse files
app.py
CHANGED
@@ -20,7 +20,6 @@ logging.basicConfig(
|
|
20 |
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
21 |
handlers=[logging.StreamHandler()]
|
22 |
)
|
23 |
-
logger = logging.getLogger(__name__)
|
24 |
|
25 |
# Initialize models
|
26 |
embedding_model = None
|
@@ -33,27 +32,27 @@ async def lifespan(app: FastAPI):
|
|
33 |
global embedding_model, summarization_model, nlp_model, db_service
|
34 |
|
35 |
# Model initialization
|
36 |
-
|
37 |
try:
|
38 |
embedding_model = EmbeddingModel()
|
39 |
summarization_model = SummarizationModel()
|
40 |
nlp_model = NLPModel()
|
41 |
db_service = DatabaseService()
|
42 |
-
|
43 |
except Exception as e:
|
44 |
-
|
45 |
raise
|
46 |
|
47 |
yield
|
48 |
|
49 |
# Cleanup
|
50 |
-
|
51 |
if db_service:
|
52 |
try:
|
53 |
await db_service.close()
|
54 |
-
|
55 |
except Exception as e:
|
56 |
-
|
57 |
|
58 |
app = FastAPI(
|
59 |
title="Kairos News API",
|
@@ -88,7 +87,7 @@ class JobStatus(BaseModel):
|
|
88 |
@app.get("/")
|
89 |
async def root(request: Request):
|
90 |
"""Root endpoint with API information"""
|
91 |
-
|
92 |
return {
|
93 |
"message": "Kairos News API is running",
|
94 |
"endpoints": {
|
@@ -111,7 +110,7 @@ async def get_logs(log_type: str = None):
|
|
111 |
@app.exception_handler(404)
|
112 |
async def not_found_exception_handler(request: Request, exc: HTTPException):
|
113 |
"""Custom 404 handler"""
|
114 |
-
|
115 |
return JSONResponse(
|
116 |
status_code=404,
|
117 |
content={
|
@@ -128,7 +127,7 @@ async def not_found_exception_handler(request: Request, exc: HTTPException):
|
|
128 |
@app.post("/index", response_model=JobStatus)
|
129 |
async def create_job(request: PostRequest, background_tasks: BackgroundTasks):
|
130 |
job_id = str(uuid.uuid4())
|
131 |
-
|
132 |
|
133 |
jobs_db[job_id] = {
|
134 |
"id": job_id,
|
@@ -149,17 +148,17 @@ async def create_job(request: PostRequest, background_tasks: BackgroundTasks):
|
|
149 |
db_service
|
150 |
)
|
151 |
|
152 |
-
|
153 |
return jobs_db[job_id]
|
154 |
|
155 |
@app.get("/loading", response_model=JobStatus)
|
156 |
async def get_job_status(id: str):
|
157 |
-
|
158 |
if id not in jobs_db:
|
159 |
-
|
160 |
raise HTTPException(status_code=404, detail="Job not found")
|
161 |
|
162 |
-
|
163 |
return jobs_db[id]
|
164 |
|
165 |
async def process_job(
|
@@ -171,7 +170,7 @@ async def process_job(
|
|
171 |
db_service: DatabaseService
|
172 |
):
|
173 |
try:
|
174 |
-
|
175 |
|
176 |
processor = QueryProcessor(
|
177 |
embedding_model=embedding_model,
|
@@ -180,7 +179,7 @@ async def process_job(
|
|
180 |
db_service=db_service
|
181 |
)
|
182 |
|
183 |
-
|
184 |
result = await processor.process(
|
185 |
query=request.query,
|
186 |
topic=request.topic,
|
@@ -193,13 +192,13 @@ async def process_job(
|
|
193 |
"completed_at": datetime.now(),
|
194 |
"result": result if result else {"message": "No results found"}
|
195 |
})
|
196 |
-
|
197 |
|
198 |
except Exception as e:
|
199 |
-
|
200 |
jobs_db[job_id].update({
|
201 |
"status": "failed",
|
202 |
"completed_at": datetime.now(),
|
203 |
"result": {"error": str(e)}
|
204 |
})
|
205 |
-
|
|
|
20 |
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
21 |
handlers=[logging.StreamHandler()]
|
22 |
)
|
|
|
23 |
|
24 |
# Initialize models
|
25 |
embedding_model = None
|
|
|
32 |
global embedding_model, summarization_model, nlp_model, db_service
|
33 |
|
34 |
# Model initialization
|
35 |
+
logging.info("Initializing models...")
|
36 |
try:
|
37 |
embedding_model = EmbeddingModel()
|
38 |
summarization_model = SummarizationModel()
|
39 |
nlp_model = NLPModel()
|
40 |
db_service = DatabaseService()
|
41 |
+
logging.info("All models initialized successfully")
|
42 |
except Exception as e:
|
43 |
+
logging.error(f"Model initialization failed: {str(e)}")
|
44 |
raise
|
45 |
|
46 |
yield
|
47 |
|
48 |
# Cleanup
|
49 |
+
logging.info("Shutting down application...")
|
50 |
if db_service:
|
51 |
try:
|
52 |
await db_service.close()
|
53 |
+
logging.info("Database connection closed successfully")
|
54 |
except Exception as e:
|
55 |
+
logging.error(f"Error closing database connection: {str(e)}")
|
56 |
|
57 |
app = FastAPI(
|
58 |
title="Kairos News API",
|
|
|
87 |
@app.get("/")
|
88 |
async def root(request: Request):
|
89 |
"""Root endpoint with API information"""
|
90 |
+
logging.info(f"Root access from {request.client.host}")
|
91 |
return {
|
92 |
"message": "Kairos News API is running",
|
93 |
"endpoints": {
|
|
|
110 |
@app.exception_handler(404)
|
111 |
async def not_found_exception_handler(request: Request, exc: HTTPException):
|
112 |
"""Custom 404 handler"""
|
113 |
+
logging.warning(f"404 Not Found: {request.url}")
|
114 |
return JSONResponse(
|
115 |
status_code=404,
|
116 |
content={
|
|
|
127 |
@app.post("/index", response_model=JobStatus)
|
128 |
async def create_job(request: PostRequest, background_tasks: BackgroundTasks):
|
129 |
job_id = str(uuid.uuid4())
|
130 |
+
logging.info(f"Creating new job {job_id} with request: {request.dict()}")
|
131 |
|
132 |
jobs_db[job_id] = {
|
133 |
"id": job_id,
|
|
|
148 |
db_service
|
149 |
)
|
150 |
|
151 |
+
logging.info(f"Job {job_id} created and processing started")
|
152 |
return jobs_db[job_id]
|
153 |
|
154 |
@app.get("/loading", response_model=JobStatus)
|
155 |
async def get_job_status(id: str):
|
156 |
+
logging.info(f"Checking status for job {id}")
|
157 |
if id not in jobs_db:
|
158 |
+
logging.warning(f"Job {id} not found")
|
159 |
raise HTTPException(status_code=404, detail="Job not found")
|
160 |
|
161 |
+
logging.info(f"Returning status for job {id}: {jobs_db[id]['status']}")
|
162 |
return jobs_db[id]
|
163 |
|
164 |
async def process_job(
|
|
|
170 |
db_service: DatabaseService
|
171 |
):
|
172 |
try:
|
173 |
+
logging.info(f"Starting processing for job {job_id}")
|
174 |
|
175 |
processor = QueryProcessor(
|
176 |
embedding_model=embedding_model,
|
|
|
179 |
db_service=db_service
|
180 |
)
|
181 |
|
182 |
+
logging.debug(f"Processing query: {request.query}")
|
183 |
result = await processor.process(
|
184 |
query=request.query,
|
185 |
topic=request.topic,
|
|
|
192 |
"completed_at": datetime.now(),
|
193 |
"result": result if result else {"message": "No results found"}
|
194 |
})
|
195 |
+
logging.info(f"Job {job_id} completed successfully")
|
196 |
|
197 |
except Exception as e:
|
198 |
+
logging.error(f"Error processing job {job_id}: {str(e)}", exc_info=True)
|
199 |
jobs_db[job_id].update({
|
200 |
"status": "failed",
|
201 |
"completed_at": datetime.now(),
|
202 |
"result": {"error": str(e)}
|
203 |
})
|
204 |
+
logging.info(f"Job {job_id} marked as failed")
|