Upload 2 files
Browse files- Dockerfile +7 -9
- home.py +11 -5
Dockerfile
CHANGED
|
@@ -1,25 +1,23 @@
|
|
| 1 |
-
# Use an official Python runtime as a parent image
|
| 2 |
FROM python:3.9-slim
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
WORKDIR /app
|
| 6 |
-
|
| 7 |
-
# Create a non-root user and switch to it for HF Spaces security
|
| 8 |
RUN useradd -m -u 1000 user
|
| 9 |
USER user
|
| 10 |
ENV HOME=/home/user \
|
| 11 |
PATH=/home/user/.local/bin:$PATH
|
| 12 |
|
| 13 |
-
#
|
|
|
|
|
|
|
|
|
|
| 14 |
COPY --chown=user requirements.txt .
|
| 15 |
-
RUN pip install --no-cache-dir -r requirements.txt
|
| 16 |
|
| 17 |
-
# Copy the rest of the application
|
| 18 |
COPY --chown=user . .
|
| 19 |
|
| 20 |
# Set environment variables
|
| 21 |
ENV PYTHONUNBUFFERED=1
|
| 22 |
-
ENV FLASK_APP=home.py
|
| 23 |
|
| 24 |
# Expose the port Hugging Face Spaces expects
|
| 25 |
EXPOSE 7860
|
|
|
|
|
|
|
| 1 |
FROM python:3.9-slim
|
| 2 |
|
| 3 |
+
# Create a non-root user
|
|
|
|
|
|
|
|
|
|
| 4 |
RUN useradd -m -u 1000 user
|
| 5 |
USER user
|
| 6 |
ENV HOME=/home/user \
|
| 7 |
PATH=/home/user/.local/bin:$PATH
|
| 8 |
|
| 9 |
+
# Set the working directory to the user's home
|
| 10 |
+
WORKDIR $HOME/app
|
| 11 |
+
|
| 12 |
+
# Copy requirements first for better caching
|
| 13 |
COPY --chown=user requirements.txt .
|
| 14 |
+
RUN pip install --no-cache-dir --user -r requirements.txt
|
| 15 |
|
| 16 |
+
# Copy the rest of the application
|
| 17 |
COPY --chown=user . .
|
| 18 |
|
| 19 |
# Set environment variables
|
| 20 |
ENV PYTHONUNBUFFERED=1
|
|
|
|
| 21 |
|
| 22 |
# Expose the port Hugging Face Spaces expects
|
| 23 |
EXPOSE 7860
|
home.py
CHANGED
|
@@ -49,16 +49,22 @@ def download_from_hub():
|
|
| 49 |
print("⚠️ HF_TOKEN not found. Persistence disabled.")
|
| 50 |
return
|
| 51 |
|
|
|
|
| 52 |
print(f"📥 Downloading files from {REPO_ID}...")
|
| 53 |
for filename in [DATA_FILE] + MODEL_FILES:
|
| 54 |
try:
|
|
|
|
|
|
|
| 55 |
path = hf_hub_download(repo_id=REPO_ID, filename=filename, repo_type="dataset", token=HF_TOKEN)
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
shutil.
|
| 59 |
-
print(f"✅ Downloaded {filename}")
|
| 60 |
except Exception as e:
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
def upload_to_hub(filename):
|
| 64 |
"""Upload a file to HF Hub"""
|
|
|
|
| 49 |
print("⚠️ HF_TOKEN not found. Persistence disabled.")
|
| 50 |
return
|
| 51 |
|
| 52 |
+
import shutil
|
| 53 |
print(f"📥 Downloading files from {REPO_ID}...")
|
| 54 |
for filename in [DATA_FILE] + MODEL_FILES:
|
| 55 |
try:
|
| 56 |
+
# Check if file exists in hub first to avoid noisy 404 logs if possible,
|
| 57 |
+
# but hf_hub_download handles it. We just catch the exception.
|
| 58 |
path = hf_hub_download(repo_id=REPO_ID, filename=filename, repo_type="dataset", token=HF_TOKEN)
|
| 59 |
+
|
| 60 |
+
# Use copyfile as it doesn't try to copy metadata (which can cause Permission Denied)
|
| 61 |
+
shutil.copyfile(path, filename)
|
| 62 |
+
print(f"✅ Downloaded and synchronized {filename}")
|
| 63 |
except Exception as e:
|
| 64 |
+
if "404" in str(e):
|
| 65 |
+
print(f"ℹ️ {filename} not found on Hub (first run?).")
|
| 66 |
+
else:
|
| 67 |
+
print(f"⚠️ Could not sync {filename}: {e}")
|
| 68 |
|
| 69 |
def upload_to_hub(filename):
|
| 70 |
"""Upload a file to HF Hub"""
|