karim323 commited on
Commit
b82bbd8
·
1 Parent(s): 936e189

Fix HF Spaces deployment: use port 7860, async model loading, immediate health checks

Browse files
Files changed (3) hide show
  1. Dockerfile +3 -3
  2. lib/routes.py +26 -9
  3. main.py +10 -4
Dockerfile CHANGED
@@ -19,9 +19,9 @@ RUN pip install --no-cache-dir --upgrade pip && \
19
  # Copy application code
20
  COPY . .
21
 
22
- # Expose port (Railway will override with $PORT)
23
- EXPOSE 8000
24
 
25
  # Run the application
26
- CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
27
 
 
19
  # Copy application code
20
  COPY . .
21
 
22
+ # Expose port (Hugging Face Spaces uses 7860)
23
+ EXPOSE 7860
24
 
25
  # Run the application
26
+ CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
27
 
lib/routes.py CHANGED
@@ -59,16 +59,33 @@ async def root(request: Request):
59
  @limiter.limit("30/minute")
60
  async def health_check(request: Request):
61
  """Detailed health check endpoint with model status"""
62
- from main import sentiment_model, ner_model, paraphrase_model, summarization_model
63
- return {
64
- "status": "healthy",
65
- "models": {
66
- "sentiment": sentiment_model.is_loaded() if sentiment_model else False,
67
- "ner": ner_model.is_loaded() if ner_model else False,
68
- "paraphrase": paraphrase_model.is_loaded() if paraphrase_model else False,
69
- "summarization": summarization_model.is_loaded() if summarization_model else False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  }
71
- }
72
 
73
 
74
  # Sentiment analysis endpoints
 
59
  @limiter.limit("30/minute")
60
  async def health_check(request: Request):
61
  """Detailed health check endpoint with model status"""
62
+ # Always return healthy - models load in background
63
+ # This allows HF Spaces health checks to pass immediately
64
+ try:
65
+ from main import sentiment_model, ner_model, paraphrase_model, summarization_model
66
+ return {
67
+ "status": "healthy",
68
+ "message": "API is running. Models may still be loading.",
69
+ "models": {
70
+ "sentiment": sentiment_model.is_loaded() if sentiment_model else False,
71
+ "ner": ner_model.is_loaded() if ner_model else False,
72
+ "paraphrase": paraphrase_model.is_loaded() if paraphrase_model else False,
73
+ "summarization": summarization_model.is_loaded() if summarization_model else False
74
+ }
75
+ }
76
+ except Exception:
77
+ # Even if models aren't initialized yet, return healthy
78
+ # This ensures HF Spaces health checks pass
79
+ return {
80
+ "status": "healthy",
81
+ "message": "API is starting. Models loading in background.",
82
+ "models": {
83
+ "sentiment": False,
84
+ "ner": False,
85
+ "paraphrase": False,
86
+ "summarization": False
87
+ }
88
  }
 
89
 
90
 
91
  # Sentiment analysis endpoints
main.py CHANGED
@@ -30,8 +30,12 @@ logging.basicConfig(level=logging.INFO)
30
  logger = logging.getLogger(__name__)
31
 
32
  # Get configuration from environment variables
33
- ALLOWED_ORIGINS = os.getenv("ALLOWED_ORIGINS", "http://localhost:8000").split(",")
34
- ENVIRONMENT = os.getenv("ENVIRONMENT", "development")
 
 
 
 
35
 
36
  logger.info(f"Starting application in {ENVIRONMENT} mode")
37
  logger.info(f"Allowed CORS origins: {ALLOWED_ORIGINS}")
@@ -110,10 +114,12 @@ def load_models():
110
  logger.info("Core models loaded successfully!")
111
 
112
 
113
- # Load models on startup
114
  @app.on_event("startup")
115
  async def startup_event():
116
- load_models()
 
 
117
 
118
 
119
  # Include router
 
30
  logger = logging.getLogger(__name__)
31
 
32
  # Get configuration from environment variables
33
+ # For Hugging Face Spaces, allow all origins by default
34
+ default_origins = "*" if os.getenv("HF_SPACE_ID") else "http://localhost:8000"
35
+ ALLOWED_ORIGINS = os.getenv("ALLOWED_ORIGINS", default_origins)
36
+ if ALLOWED_ORIGINS != "*":
37
+ ALLOWED_ORIGINS = ALLOWED_ORIGINS.split(",")
38
+ ENVIRONMENT = os.getenv("ENVIRONMENT", "production" if os.getenv("HF_SPACE_ID") else "development")
39
 
40
  logger.info(f"Starting application in {ENVIRONMENT} mode")
41
  logger.info(f"Allowed CORS origins: {ALLOWED_ORIGINS}")
 
114
  logger.info("Core models loaded successfully!")
115
 
116
 
117
+ # Load models on startup (non-blocking for HF Spaces health checks)
118
  @app.on_event("startup")
119
  async def startup_event():
120
+ # Load models in background to allow health checks to respond quickly
121
+ import asyncio
122
+ asyncio.create_task(asyncio.to_thread(load_models))
123
 
124
 
125
  # Include router