venni16 commited on
Commit
3519394
·
verified ·
1 Parent(s): 974b300

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -5
app.py CHANGED
@@ -10,6 +10,8 @@ from datetime import datetime
10
  import traceback
11
  import logging
12
  from typing import List, Optional
 
 
13
 
14
  # Setup logging
15
  logging.basicConfig(level=logging.INFO)
@@ -31,6 +33,7 @@ app.add_middleware(
31
  allow_credentials=True,
32
  allow_methods=["*"],
33
  allow_headers=["*"],
 
34
  )
35
 
36
  # Import processing functions
@@ -44,19 +47,46 @@ except ImportError as e:
44
  logger.error(f"Import error: {e}")
45
  DEPENDENCIES_LOADED = False
46
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  @app.get("/")
48
  async def root():
49
  return {"message": "Material Summarizer API", "status": "running"}
50
 
51
  @app.get("/health")
52
  async def health_check():
 
53
  status = "healthy" if DEPENDENCIES_LOADED else "missing_dependencies"
54
- return {
55
- "status": status,
56
- "service": "material-summarizer",
57
- "dependencies_loaded": DEPENDENCIES_LOADED
58
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
 
60
  @app.post("/summarize-document")
61
  async def summarize_document(
62
  file: UploadFile = File(...),
 
10
  import traceback
11
  import logging
12
  from typing import List, Optional
13
+ import time
14
+ from fastapi.responses import JSONResponse
15
 
16
  # Setup logging
17
  logging.basicConfig(level=logging.INFO)
 
33
  allow_credentials=True,
34
  allow_methods=["*"],
35
  allow_headers=["*"],
36
+ max_age=600,
37
  )
38
 
39
  # Import processing functions
 
47
  logger.error(f"Import error: {e}")
48
  DEPENDENCIES_LOADED = False
49
 
50
+ @app.on_event("startup")
51
+ async def startup_event():
52
+ """Handle startup events"""
53
+ logger.info("Application startup initiated")
54
+ # Load model on startup to avoid cold start delays
55
+ try:
56
+ from summarizer import get_summarizer
57
+ get_summarizer() # Pre-load the model
58
+ logger.info("Models pre-loaded successfully")
59
+ except Exception as e:
60
+ logger.warning(f"Model pre-loading failed: {e}")
61
+
62
  @app.get("/")
63
  async def root():
64
  return {"message": "Material Summarizer API", "status": "running"}
65
 
66
  @app.get("/health")
67
  async def health_check():
68
+ """Health check endpoint specifically for Hugging Face Spaces"""
69
  status = "healthy" if DEPENDENCIES_LOADED else "missing_dependencies"
70
+ return JSONResponse(
71
+ content={
72
+ "status": status,
73
+ "service": "material-summarizer",
74
+ "dependencies_loaded": DEPENDENCIES_LOADED,
75
+ "timestamp": time.time()
76
+ },
77
+ status_code=200 if DEPENDENCIES_LOADED else 503
78
+ )
79
+
80
+
81
+ @app.get("/ping")
82
+ async def ping():
83
+ """Simple ping endpoint for load balancers"""
84
+ return JSONResponse(
85
+ content={"status": "ok", "timestamp": time.time()},
86
+ status_code=200
87
+ )
88
 
89
+
90
  @app.post("/summarize-document")
91
  async def summarize_document(
92
  file: UploadFile = File(...),