model download resolve
Browse files- Dockerfile +5 -4
- app/services/model_service.py +64 -15
Dockerfile
CHANGED
@@ -2,12 +2,12 @@ FROM python:3.9-slim
|
|
2 |
|
3 |
WORKDIR /app
|
4 |
|
5 |
-
# Create
|
6 |
ENV HF_HOME=/app/.cache
|
7 |
-
ENV TRANSFORMERS_CACHE=/app/.cache/huggingface
|
8 |
ENV TORCH_HOME=/app/.cache/torch
|
9 |
-
|
10 |
-
RUN
|
|
|
11 |
|
12 |
# Copy requirements first for better caching
|
13 |
COPY requirements.txt .
|
@@ -24,6 +24,7 @@ RUN chmod -R 777 uploads models
|
|
24 |
# Set environment variables
|
25 |
ENV PYTHONPATH=/app
|
26 |
ENV PORT=7860
|
|
|
27 |
|
28 |
# Expose the port
|
29 |
EXPOSE 7860
|
|
|
2 |
|
3 |
WORKDIR /app
|
4 |
|
5 |
+
# Create user-writable cache and temp directories
|
6 |
ENV HF_HOME=/app/.cache
|
|
|
7 |
ENV TORCH_HOME=/app/.cache/torch
|
8 |
+
ENV TMPDIR=/app/tmp
|
9 |
+
RUN mkdir -p /app/.cache/huggingface /app/.cache/torch /app/tmp
|
10 |
+
RUN chmod -R 777 /app/.cache /app/tmp
|
11 |
|
12 |
# Copy requirements first for better caching
|
13 |
COPY requirements.txt .
|
|
|
24 |
# Set environment variables
|
25 |
ENV PYTHONPATH=/app
|
26 |
ENV PORT=7860
|
27 |
+
ENV PYTHONUNBUFFERED=1
|
28 |
|
29 |
# Expose the port
|
30 |
EXPOSE 7860
|
app/services/model_service.py
CHANGED
@@ -43,25 +43,74 @@ class ModelService:
|
|
43 |
"""Download the model from Hugging Face Hub if not present locally"""
|
44 |
try:
|
45 |
# Create the directory if it doesn't exist
|
46 |
-
os.
|
|
|
47 |
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
#
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
local_dir=os.path.dirname(settings.MODEL_PATH),
|
55 |
-
local_dir_use_symlinks=False
|
56 |
-
)
|
57 |
|
58 |
-
#
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
logger.info(f"Model downloaded successfully to {settings.MODEL_PATH}")
|
64 |
-
return True
|
65 |
except Exception as e:
|
66 |
logger.error(f"Error downloading model from Hugging Face Hub: {e}")
|
67 |
return False
|
|
|
43 |
"""Download the model from Hugging Face Hub if not present locally"""
|
44 |
try:
|
45 |
# Create the directory if it doesn't exist
|
46 |
+
model_dir = os.path.dirname(settings.MODEL_PATH)
|
47 |
+
os.makedirs(model_dir, exist_ok=True)
|
48 |
|
49 |
+
# Log environment info for debugging
|
50 |
+
logger.info(f"HF_HOME: {os.environ.get('HF_HOME')}")
|
51 |
+
logger.info(f"TMPDIR: {os.environ.get('TMPDIR')}")
|
52 |
+
logger.info(f"Current working directory: {os.getcwd()}")
|
53 |
+
logger.info(f"Model directory: {model_dir}")
|
54 |
+
logger.info(f"Model path: {settings.MODEL_PATH}")
|
55 |
|
56 |
+
# Set up a temporary directory within our writable space
|
57 |
+
tmp_dir = os.environ.get("TMPDIR", "/app/tmp")
|
58 |
+
os.makedirs(tmp_dir, exist_ok=True)
|
59 |
+
logger.info(f"Using temporary directory: {tmp_dir}")
|
|
|
|
|
|
|
60 |
|
61 |
+
# Use a simpler approach to download the file
|
62 |
+
try:
|
63 |
+
# Try to download directly to the final location
|
64 |
+
logger.info(f"Downloading from {settings.HF_MODEL_REPO}/{settings.HF_MODEL_FILENAME}")
|
65 |
+
downloaded_path = hf_hub_download(
|
66 |
+
repo_id=settings.HF_MODEL_REPO,
|
67 |
+
filename=settings.HF_MODEL_FILENAME,
|
68 |
+
local_dir=model_dir,
|
69 |
+
local_dir_use_symlinks=False,
|
70 |
+
cache_dir=os.environ.get("HF_HOME"),
|
71 |
+
force_download=True
|
72 |
+
)
|
73 |
+
|
74 |
+
logger.info(f"Model downloaded to: {downloaded_path}")
|
75 |
+
|
76 |
+
# If needed, copy the file to the expected location
|
77 |
+
if downloaded_path != settings.MODEL_PATH:
|
78 |
+
import shutil
|
79 |
+
logger.info(f"Copying from {downloaded_path} to {settings.MODEL_PATH}")
|
80 |
+
shutil.copy2(downloaded_path, settings.MODEL_PATH)
|
81 |
+
|
82 |
+
logger.info(f"Model download successful")
|
83 |
+
return True
|
84 |
+
|
85 |
+
except Exception as download_error:
|
86 |
+
logger.error(f"Error during Hugging Face Hub download: {download_error}")
|
87 |
+
|
88 |
+
# Fallback method: direct download using requests
|
89 |
+
logger.info("Trying fallback method with direct HTTP download")
|
90 |
+
import requests
|
91 |
+
from huggingface_hub.utils import build_hf_headers
|
92 |
+
|
93 |
+
# Get Hugging Face token from settings
|
94 |
+
token = settings.HUGGINGFACE_TOKEN
|
95 |
+
|
96 |
+
# Build proper URL for the model file
|
97 |
+
url = f"https://huggingface.co/{settings.HF_MODEL_REPO}/resolve/main/{settings.HF_MODEL_FILENAME}"
|
98 |
+
logger.info(f"Downloading from URL: {url}")
|
99 |
+
|
100 |
+
# Download with proper headers
|
101 |
+
headers = build_hf_headers(token=token)
|
102 |
+
response = requests.get(url, headers=headers, stream=True)
|
103 |
+
response.raise_for_status()
|
104 |
+
|
105 |
+
# Write the file in chunks to avoid memory issues
|
106 |
+
logger.info(f"Writing downloaded content to {settings.MODEL_PATH}")
|
107 |
+
with open(settings.MODEL_PATH, 'wb') as f:
|
108 |
+
for chunk in response.iter_content(chunk_size=8192):
|
109 |
+
f.write(chunk)
|
110 |
+
|
111 |
+
logger.info(f"Model downloaded successfully using fallback method")
|
112 |
+
return True
|
113 |
|
|
|
|
|
114 |
except Exception as e:
|
115 |
logger.error(f"Error downloading model from Hugging Face Hub: {e}")
|
116 |
return False
|