Spaces:
Sleeping
Sleeping
update streamlit app
Browse files- .streamlit/config.toml +30 -0
- Dockerfile +40 -3
- src/streamlit_app.py +28 -15
.streamlit/config.toml
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[global]
|
| 2 |
+
developmentMode = false
|
| 3 |
+
|
| 4 |
+
[server]
|
| 5 |
+
headless = true
|
| 6 |
+
enableCORS = false
|
| 7 |
+
enableXsrfProtection = false
|
| 8 |
+
port = 7860
|
| 9 |
+
maxUploadSize = 200
|
| 10 |
+
|
| 11 |
+
[browser]
|
| 12 |
+
gatherUsageStats = false
|
| 13 |
+
|
| 14 |
+
[theme]
|
| 15 |
+
primaryColor = "#3b82f6"
|
| 16 |
+
backgroundColor = "#ffffff"
|
| 17 |
+
secondaryBackgroundColor = "#f8fafc"
|
| 18 |
+
textColor = "#1f2937"
|
| 19 |
+
|
| 20 |
+
[runner]
|
| 21 |
+
magicEnabled = false
|
| 22 |
+
installTracer = false
|
| 23 |
+
fixMatplotlib = true
|
| 24 |
+
|
| 25 |
+
[client]
|
| 26 |
+
caching = true
|
| 27 |
+
showErrorDetails = false
|
| 28 |
+
|
| 29 |
+
[logger]
|
| 30 |
+
level = "error"
|
Dockerfile
CHANGED
|
@@ -2,20 +2,57 @@ FROM python:3.9-slim
|
|
| 2 |
|
| 3 |
WORKDIR /app
|
| 4 |
|
|
|
|
| 5 |
RUN apt-get update && apt-get install -y \
|
| 6 |
build-essential \
|
| 7 |
curl \
|
| 8 |
software-properties-common \
|
| 9 |
git \
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
&& rm -rf /var/lib/apt/lists/*
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
COPY requirements.txt ./
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
|
|
|
| 17 |
EXPOSE 8501
|
| 18 |
|
|
|
|
| 19 |
HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
+
# Install system dependencies including geospatial libraries
|
| 6 |
RUN apt-get update && apt-get install -y \
|
| 7 |
build-essential \
|
| 8 |
curl \
|
| 9 |
software-properties-common \
|
| 10 |
git \
|
| 11 |
+
gcc \
|
| 12 |
+
g++ \
|
| 13 |
+
libgdal-dev \
|
| 14 |
+
gdal-bin \
|
| 15 |
+
libgeos-dev \
|
| 16 |
+
libproj-dev \
|
| 17 |
+
libspatialindex-dev \
|
| 18 |
&& rm -rf /var/lib/apt/lists/*
|
| 19 |
|
| 20 |
+
# Set GDAL environment variables
|
| 21 |
+
ENV GDAL_CONFIG=/usr/bin/gdal-config
|
| 22 |
+
ENV CPLUS_INCLUDE_PATH=/usr/include/gdal
|
| 23 |
+
ENV C_INCLUDE_PATH=/usr/include/gdal
|
| 24 |
+
|
| 25 |
+
# Create writable directories for Streamlit and other components
|
| 26 |
+
RUN mkdir -p /tmp/.streamlit /tmp/.matplotlib /tmp/.config /tmp/.cache /tmp/.local && \
|
| 27 |
+
chmod -R 777 /tmp
|
| 28 |
+
|
| 29 |
+
# Set environment variables BEFORE copying requirements
|
| 30 |
+
ENV HOME=/tmp
|
| 31 |
+
ENV MPLCONFIGDIR=/tmp/.matplotlib
|
| 32 |
+
ENV STREAMLIT_CONFIG_DIR=/tmp/.streamlit
|
| 33 |
+
ENV XDG_CONFIG_HOME=/tmp/.config
|
| 34 |
+
ENV XDG_CACHE_HOME=/tmp/.cache
|
| 35 |
+
|
| 36 |
+
# Copy requirements and install Python dependencies
|
| 37 |
COPY requirements.txt ./
|
| 38 |
+
RUN pip3 install --no-cache-dir --upgrade pip && \
|
| 39 |
+
pip3 install --no-cache-dir -r requirements.txt
|
| 40 |
|
| 41 |
+
# Copy application files
|
| 42 |
+
COPY src/ ./src/
|
| 43 |
+
COPY .streamlit/ /tmp/.streamlit/
|
| 44 |
|
| 45 |
+
# Expose port (HF Spaces uses 7860 by default, but keeping 8501 as per template)
|
| 46 |
EXPOSE 8501
|
| 47 |
|
| 48 |
+
# Health check with updated URL
|
| 49 |
HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
|
| 50 |
|
| 51 |
+
# Updated entrypoint with XSRF protection disabled for HF Spaces
|
| 52 |
+
ENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", \
|
| 53 |
+
"--server.port=8501", \
|
| 54 |
+
"--server.address=0.0.0.0", \
|
| 55 |
+
"--server.headless=true", \
|
| 56 |
+
"--server.enableXsrfProtection=false", \
|
| 57 |
+
"--server.enableCORS=false", \
|
| 58 |
+
"--server.fileWatcherType=none"]
|
src/streamlit_app.py
CHANGED
|
@@ -23,22 +23,35 @@ warnings.filterwarnings("ignore")
|
|
| 23 |
|
| 24 |
# === FIXED ENVIRONMENT SETUP FOR HUGGING FACE SPACES ===
|
| 25 |
# Set up writable directories for Hugging Face Spaces
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
try:
|
| 39 |
-
os.makedirs(
|
| 40 |
-
except
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Set matplotlib backend to Agg (non-interactive) for server environment
|
| 44 |
try:
|
|
|
|
| 23 |
|
| 24 |
# === FIXED ENVIRONMENT SETUP FOR HUGGING FACE SPACES ===
|
| 25 |
# Set up writable directories for Hugging Face Spaces
|
| 26 |
+
# Set up writable directories for Hugging Face Spaces
|
| 27 |
+
temp_dir = tempfile.gettempdir()
|
| 28 |
+
|
| 29 |
+
# Environment variables that must be set BEFORE importing streamlit
|
| 30 |
+
os.environ["STREAMLIT_CONFIG_DIR"] = os.path.join(temp_dir, ".streamlit")
|
| 31 |
+
os.environ["HOME"] = temp_dir
|
| 32 |
+
os.environ["MPLCONFIGDIR"] = os.path.join(temp_dir, ".matplotlib")
|
| 33 |
+
os.environ["XDG_CONFIG_HOME"] = os.path.join(temp_dir, ".config")
|
| 34 |
+
os.environ["XDG_CACHE_HOME"] = os.path.join(temp_dir, ".cache")
|
| 35 |
+
|
| 36 |
+
# Create all necessary directories
|
| 37 |
+
directories = [
|
| 38 |
+
os.environ["STREAMLIT_CONFIG_DIR"],
|
| 39 |
+
os.environ["MPLCONFIGDIR"],
|
| 40 |
+
os.environ["XDG_CONFIG_HOME"],
|
| 41 |
+
os.environ["XDG_CACHE_HOME"],
|
| 42 |
+
os.path.join(temp_dir, ".local"),
|
| 43 |
+
os.path.join(temp_dir, ".cache", "matplotlib"),
|
| 44 |
+
os.path.join(temp_dir, ".config", "matplotlib"),
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
for dir_path in directories:
|
| 48 |
try:
|
| 49 |
+
os.makedirs(dir_path, mode=0o777, exist_ok=True)
|
| 50 |
+
except (OSError, PermissionError):
|
| 51 |
+
pass # Ignore errors
|
| 52 |
+
|
| 53 |
+
# Suppress warnings
|
| 54 |
+
warnings.filterwarnings("ignore")
|
| 55 |
|
| 56 |
# Set matplotlib backend to Agg (non-interactive) for server environment
|
| 57 |
try:
|