Solareva Taisia commited on
Commit
b44d065
·
1 Parent(s): e5bb1eb

fix(spaces): use uvicorn directly in Dockerfile for HuggingFace Spaces

Browse files
Files changed (2) hide show
  1. Dockerfile +3 -3
  2. app.py +11 -6
Dockerfile CHANGED
@@ -41,7 +41,7 @@ RUN mkdir -p monitoring/predictions
41
  # Expose default port (Spaces uses 7860, but PORT env var is set dynamically)
42
  EXPOSE 7860
43
 
44
- # Spaces will run: python app.py
45
- # The app.py file handles PORT env var automatically
46
- CMD ["python", "app.py"]
47
 
 
41
  # Expose default port (Spaces uses 7860, but PORT env var is set dynamically)
42
  EXPOSE 7860
43
 
44
+ # Run uvicorn directly, referencing app from app.py
45
+ # HuggingFace Spaces sets PORT env var automatically (use shell form to expand env var)
46
+ CMD ["sh", "-c", "uvicorn app:app --host 0.0.0.0 --port ${PORT:-7860}"]
47
 
app.py CHANGED
@@ -1,5 +1,5 @@
1
  """
2
- HuggingFace Spaces entry point for Russian News Classification API.
3
 
4
  This file is required by HuggingFace Spaces. It imports and exposes the FastAPI app
5
  from api/main.py. Spaces will automatically run this with uvicorn.
@@ -15,13 +15,18 @@ sys.path.insert(0, str(app_dir))
15
  # Import the FastAPI app from api.main
16
  from api.main import app
17
 
18
- # HuggingFace Spaces automatically handles PORT, but we can set it here as fallback
19
- # Spaces expects the app to be accessible via the 'app' variable
20
  __all__ = ["app"]
21
 
22
  if __name__ == "__main__":
23
- # For local testing
24
  import uvicorn
25
- port = int(os.environ.get("PORT", 8000))
26
- uvicorn.run(app, host="0.0.0.0", port=port)
 
 
 
 
 
 
27
 
 
1
  """
2
+ HuggingFace Spaces entry point for News Classification API.
3
 
4
  This file is required by HuggingFace Spaces. It imports and exposes the FastAPI app
5
  from api/main.py. Spaces will automatically run this with uvicorn.
 
15
  # Import the FastAPI app from api.main
16
  from api.main import app
17
 
18
+ # HuggingFace Spaces expects the app to be accessible via the 'app' variable
19
+ # For Docker Spaces, we need to run uvicorn directly when executed
20
  __all__ = ["app"]
21
 
22
  if __name__ == "__main__":
 
23
  import uvicorn
24
+ # HuggingFace Spaces sets PORT env var (typically 7860)
25
+ port = int(os.environ.get("PORT", 7860))
26
+ uvicorn.run(
27
+ "app:app", # Use string reference so it works with app variable
28
+ host="0.0.0.0",
29
+ port=port,
30
+ log_level="info"
31
+ )
32