Spaces:
Runtime error
Runtime error
Silicon Valley - Admin
commited on
Commit
路
c46d8cf
1
Parent(s):
13c3439
Optimize Dockerfile for Hugging Face Spaces deployment and update server configuration
Browse files- Refactored Dockerfile to improve caching and streamline the build process.
- Added environment variables for configuration, including PORT and logging settings.
- Updated server.py to utilize environment variables for configuration and improved logging.
- Enhanced CORS settings to allow requests from Hugging Face Spaces domains.
- Added werkzeug to requirements.txt for compatibility with the updated server implementation.
- Dockerfile +12 -22
- requirements.txt +1 -0
- server.py +21 -14
Dockerfile
CHANGED
@@ -1,39 +1,29 @@
|
|
1 |
-
# Dockerfile
|
2 |
FROM python:3.11-slim
|
3 |
|
4 |
-
|
5 |
-
RUN adduser --disabled-password --gecos '' appuser
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
# Instalar dependencias del sistema si son necesarias
|
10 |
-
RUN apt-get update && apt-get install -y --no-install-recommends \
|
11 |
build-essential \
|
12 |
&& rm -rf /var/lib/apt/lists/*
|
13 |
|
14 |
-
# Copiar
|
15 |
COPY requirements.txt .
|
16 |
-
COPY server.py .
|
17 |
-
COPY hypercorn.toml .
|
18 |
|
19 |
# Instalar dependencias de Python
|
20 |
RUN pip install --no-cache-dir -r requirements.txt
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
mkdir -p /home/appuser/.cache/huggingface && \
|
25 |
-
chown -R appuser:appuser /home/appuser/.cache
|
26 |
|
27 |
-
# Variables de entorno
|
28 |
ENV PYTHONUNBUFFERED=1
|
29 |
-
ENV
|
30 |
-
ENV
|
31 |
-
|
32 |
-
# Cambiar al usuario no root
|
33 |
-
USER appuser
|
34 |
|
35 |
# Exponer el puerto que Hugging Face Spaces espera
|
36 |
EXPOSE 7860
|
37 |
|
38 |
-
# Comando para ejecutar la aplicaci贸n
|
39 |
-
CMD ["hypercorn", "--
|
|
|
1 |
+
# Dockerfile optimizado para Hugging Face Spaces
|
2 |
FROM python:3.11-slim
|
3 |
|
4 |
+
WORKDIR /code
|
|
|
5 |
|
6 |
+
# Instalar dependencias del sistema
|
7 |
+
RUN apt-get update && apt-get install -y \
|
|
|
|
|
8 |
build-essential \
|
9 |
&& rm -rf /var/lib/apt/lists/*
|
10 |
|
11 |
+
# Copiar solo requirements.txt primero para aprovechar la cach茅 de Docker
|
12 |
COPY requirements.txt .
|
|
|
|
|
13 |
|
14 |
# Instalar dependencias de Python
|
15 |
RUN pip install --no-cache-dir -r requirements.txt
|
16 |
|
17 |
+
# Copiar el resto de los archivos
|
18 |
+
COPY . .
|
|
|
|
|
19 |
|
20 |
+
# Variables de entorno para Hugging Face Spaces
|
21 |
ENV PYTHONUNBUFFERED=1
|
22 |
+
ENV PYTHONPATH=/code
|
23 |
+
ENV PORT=7860
|
|
|
|
|
|
|
24 |
|
25 |
# Exponer el puerto que Hugging Face Spaces espera
|
26 |
EXPOSE 7860
|
27 |
|
28 |
+
# Comando para ejecutar la aplicaci贸n
|
29 |
+
CMD ["hypercorn", "server:app", "--bind", "0.0.0.0:7860", "--workers", "1"]
|
requirements.txt
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
quart==0.18.4
|
|
|
2 |
quart-schema==0.15.0
|
3 |
quart-cors==0.7.0
|
4 |
hypercorn==0.14.3
|
|
|
1 |
quart==0.18.4
|
2 |
+
werkzeug==2.3.7
|
3 |
quart-schema==0.15.0
|
4 |
quart-cors==0.7.0
|
5 |
hypercorn==0.14.3
|
server.py
CHANGED
@@ -9,28 +9,33 @@ from quart_cors import cors
|
|
9 |
import importlib.metadata
|
10 |
import secrets
|
11 |
import logging
|
12 |
-
import
|
13 |
|
14 |
from broker import SessionBroker, SessionDoesNotExist, ClientRequest, ClientResponse, ClientError
|
15 |
|
16 |
-
# Configuraci贸n
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
MAX_MESSAGE_SIZE: int = 16 * 1024 * 1024
|
21 |
-
RATE_LIMIT: int = 100
|
22 |
-
SESSION_TIMEOUT: int = 3600
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
# Crear aplicaci贸n con CORS habilitado
|
25 |
app = Quart(__name__)
|
26 |
app = cors(app,
|
27 |
-
allow_origin="
|
28 |
allow_methods=["GET", "POST", "OPTIONS"],
|
29 |
allow_headers=["Content-Type"],
|
30 |
max_age=3600
|
31 |
)
|
32 |
QuartSchema(app)
|
33 |
-
app.logger.setLevel(LOG_LEVEL)
|
34 |
|
35 |
broker = SessionBroker()
|
36 |
|
@@ -108,9 +113,11 @@ async def command(data: Command) -> Tuple[CommandResponse | ErrorResponse, int]:
|
|
108 |
|
109 |
# Ejecutar aplicaci贸n
|
110 |
def run():
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
114 |
|
115 |
# Agregar un endpoint de health check
|
116 |
@app.route("/health")
|
|
|
9 |
import importlib.metadata
|
10 |
import secrets
|
11 |
import logging
|
12 |
+
import os
|
13 |
|
14 |
from broker import SessionBroker, SessionDoesNotExist, ClientRequest, ClientResponse, ClientError
|
15 |
|
16 |
+
# Configuraci贸n para Hugging Face Spaces
|
17 |
+
PORT = int(os.getenv('PORT', 7860))
|
18 |
+
TIMEOUT: int = int(os.getenv('TIMEOUT', 60))
|
19 |
+
LOG_LEVEL: int = getattr(logging, os.getenv('LOG_LEVEL', 'INFO'))
|
20 |
+
MAX_MESSAGE_SIZE: int = int(os.getenv('MAX_MESSAGE_SIZE', 16 * 1024 * 1024))
|
21 |
+
RATE_LIMIT: int = int(os.getenv('RATE_LIMIT', 100))
|
22 |
+
SESSION_TIMEOUT: int = int(os.getenv('SESSION_TIMEOUT', 3600))
|
23 |
+
|
24 |
+
# Configurar logging
|
25 |
+
logging.basicConfig(
|
26 |
+
level=LOG_LEVEL,
|
27 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
28 |
+
)
|
29 |
|
30 |
+
# Crear aplicaci贸n con CORS habilitado
|
31 |
app = Quart(__name__)
|
32 |
app = cors(app,
|
33 |
+
allow_origin=["https://*.hf.space", "https://*.huggingface.co"],
|
34 |
allow_methods=["GET", "POST", "OPTIONS"],
|
35 |
allow_headers=["Content-Type"],
|
36 |
max_age=3600
|
37 |
)
|
38 |
QuartSchema(app)
|
|
|
39 |
|
40 |
broker = SessionBroker()
|
41 |
|
|
|
113 |
|
114 |
# Ejecutar aplicaci贸n
|
115 |
def run():
|
116 |
+
app.run(
|
117 |
+
host='0.0.0.0',
|
118 |
+
port=PORT,
|
119 |
+
debug=False
|
120 |
+
)
|
121 |
|
122 |
# Agregar un endpoint de health check
|
123 |
@app.route("/health")
|