s4um1l commited on
Commit
75e2ca4
·
1 Parent(s): 209e402

fixing https issue on hf

Browse files
Files changed (2) hide show
  1. Dockerfile +4 -4
  2. backend/main.py +27 -1
Dockerfile CHANGED
@@ -40,10 +40,10 @@ RUN apt-get update && apt-get install -y \
40
 
41
  # Set up user environment
42
  ENV HOME=/home/user \
43
- PATH=/home/user/app/.venv/bin:$PATH
44
-
45
- ENV UVICORN_WS_PROTOCOL=websockets
46
-
47
 
48
  # Verify dependencies are available in the final image
49
  RUN python -c "import numpy; print(f'NumPy version: {numpy.__version__}')" && \
 
40
 
41
  # Set up user environment
42
  ENV HOME=/home/user \
43
+ PATH=/home/user/app/.venv/bin:$PATH \
44
+ UVICORN_WS_PROTOCOL=websockets \
45
+ FORWARDED_ALLOW_IPS="*" \
46
+ HTTPTOOLS_LOG_DEBUG=1
47
 
48
  # Verify dependencies are available in the final image
49
  RUN python -c "import numpy; print(f'NumPy version: {numpy.__version__}')" && \
backend/main.py CHANGED
@@ -36,6 +36,22 @@ load_dotenv()
36
 
37
  app = FastAPI()
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  # Configure CORS - allow all origins explicitly for development
40
  app.add_middleware(
41
  CORSMiddleware,
@@ -302,4 +318,14 @@ if frontend_path.exists():
302
  return FileResponse(str(frontend_path / "index.html"))
303
 
304
  if __name__ == "__main__":
305
- uvicorn.run("main:app", host="0.0.0.0", port=8000)
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  app = FastAPI()
38
 
39
+ # For deployment on Hugging Face Spaces, add a middleware to handle HTTPS
40
+ from starlette.middleware.base import BaseHTTPMiddleware
41
+
42
+ class HTTPSRedirectMiddleware(BaseHTTPMiddleware):
43
+ async def dispatch(self, request, call_next):
44
+ # Check for X-Forwarded-Proto header and ensure it's HTTPS
45
+ if request.headers.get("X-Forwarded-Proto") == "http":
46
+ logger.info("Redirecting to HTTPS")
47
+ url = request.url.replace(scheme="https")
48
+ return RedirectResponse(url=str(url), status_code=307)
49
+ return await call_next(request)
50
+
51
+ # Add the HTTPS middleware
52
+ from starlette.responses import RedirectResponse
53
+ app.add_middleware(HTTPSRedirectMiddleware)
54
+
55
  # Configure CORS - allow all origins explicitly for development
56
  app.add_middleware(
57
  CORSMiddleware,
 
318
  return FileResponse(str(frontend_path / "index.html"))
319
 
320
  if __name__ == "__main__":
321
+ # Get the port from environment variable or use default
322
+ port = int(os.environ.get("PORT", 8000))
323
+
324
+ # For Hugging Face Spaces deployment
325
+ uvicorn.run(
326
+ "main:app",
327
+ host="0.0.0.0",
328
+ port=port,
329
+ proxy_headers=True, # This tells uvicorn to trust the X-Forwarded-* headers
330
+ forwarded_allow_ips="*" # Allow forwarded requests from any IP
331
+ )