Spaces:
Sleeping
Sleeping
Mod to support both gguf and hf model download
Browse files- Dockerfile +18 -1
- app/utils/download_model.py +32 -8
- entrypoint.sh +10 -6
Dockerfile
CHANGED
@@ -10,10 +10,18 @@ RUN apt-get update && apt-get install -y \
|
|
10 |
RUN useradd -m -u 1000 user
|
11 |
|
12 |
# Set environment variables for the non-root user
|
|
|
|
|
|
|
|
|
|
|
13 |
ENV HOME=/home/user \
|
14 |
PATH=/home/user/.local/bin:$PATH \
|
15 |
NAME=EduConnect \
|
16 |
-
EC_ADMIN_PWD='$2b$12$wGncNhE7OVmsb7TKFuNPKuJfKOIKdGtw302VMDJbAPrHrY73jqID.'
|
|
|
|
|
|
|
17 |
|
18 |
# Set the non-root user's home directory as the working directory
|
19 |
WORKDIR $HOME
|
@@ -44,5 +52,14 @@ EXPOSE 7860
|
|
44 |
# This directory is intended for persistent storage
|
45 |
VOLUME /home/user/data
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
# Run the FastAPI application using Uvicorn, binding to port 7860
|
48 |
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
10 |
RUN useradd -m -u 1000 user
|
11 |
|
12 |
# Set environment variables for the non-root user
|
13 |
+
# Name -> Name of the app container
|
14 |
+
# EC_ADMIN_PWD -> A secret
|
15 |
+
# HF_MODEL_NAME -> Name of the Hugging Face Hub model
|
16 |
+
# GGUF_MODEL_URL -> For special loading for GGUF
|
17 |
+
# MODEL_CLASS -> A switch to load 'gguf' or 'hf'
|
18 |
ENV HOME=/home/user \
|
19 |
PATH=/home/user/.local/bin:$PATH \
|
20 |
NAME=EduConnect \
|
21 |
+
EC_ADMIN_PWD='$2b$12$wGncNhE7OVmsb7TKFuNPKuJfKOIKdGtw302VMDJbAPrHrY73jqID.' \
|
22 |
+
HF_MODEL_NAME="BitBasher/llama-2-7b-mini-ibased-GGUF" \
|
23 |
+
GGUF_MODEL_URL='https://huggingface.co/BitBasher/llama-2-7b-mini-ibased-GGUF/raw/main/llama-2-7b-mini-ibased.Q5_K_M.gguf' \
|
24 |
+
MODEL_CLASS='gguf'
|
25 |
|
26 |
# Set the non-root user's home directory as the working directory
|
27 |
WORKDIR $HOME
|
|
|
52 |
# This directory is intended for persistent storage
|
53 |
VOLUME /home/user/data
|
54 |
|
55 |
+
# Copy the entrypoint script into the container and ensure it is executable
|
56 |
+
COPY --chown=user:user entrypoint.sh $HOME
|
57 |
+
|
58 |
+
# Change permission of entrypoint.sh and make sure it is executable
|
59 |
+
RUN chmod +x $HOME/entrypoint.sh
|
60 |
+
|
61 |
+
# Set the entrypoint script to be executed when the container starts
|
62 |
+
ENTRYPOINT ["./entrypoint.sh"]
|
63 |
+
|
64 |
# Run the FastAPI application using Uvicorn, binding to port 7860
|
65 |
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
app/utils/download_model.py
CHANGED
@@ -1,24 +1,48 @@
|
|
1 |
-
# /home/user/app/utils/download_model.py
|
2 |
import os
|
|
|
3 |
from transformers import AutoModel
|
4 |
|
5 |
-
def
|
6 |
-
# Use environment variables for the model name and directory
|
7 |
model_name = os.getenv("HF_MODEL_NAME")
|
8 |
model_dir = f"/home/user/data/models/{model_name}"
|
9 |
-
|
10 |
# Authenticate with Hugging Face using the token, if available
|
11 |
hf_token = os.getenv("HF_TOKEN")
|
12 |
if hf_token:
|
13 |
from huggingface_hub import HfFolder
|
14 |
-
HfFolder.save_token(hf_token)
|
15 |
-
|
16 |
-
# Download the model
|
17 |
print(f"Downloading model: {model_name}...")
|
18 |
model = AutoModel.from_pretrained(model_name)
|
19 |
model.save_pretrained(model_dir)
|
20 |
print(f"Model {model_name} downloaded and saved to {model_dir}")
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
if __name__ == "__main__":
|
23 |
download_model()
|
24 |
-
|
|
|
|
|
1 |
import os
|
2 |
+
import requests
|
3 |
from transformers import AutoModel
|
4 |
|
5 |
+
def download_hf_model():
|
|
|
6 |
model_name = os.getenv("HF_MODEL_NAME")
|
7 |
model_dir = f"/home/user/data/models/{model_name}"
|
8 |
+
|
9 |
# Authenticate with Hugging Face using the token, if available
|
10 |
hf_token = os.getenv("HF_TOKEN")
|
11 |
if hf_token:
|
12 |
from huggingface_hub import HfFolder
|
13 |
+
HfFolder.save_token(hf_token)
|
14 |
+
|
|
|
15 |
print(f"Downloading model: {model_name}...")
|
16 |
model = AutoModel.from_pretrained(model_name)
|
17 |
model.save_pretrained(model_dir)
|
18 |
print(f"Model {model_name} downloaded and saved to {model_dir}")
|
19 |
|
20 |
+
def download_gguf_model():
|
21 |
+
model_name = os.getenv("HF_MODEL_NAME")
|
22 |
+
model_dir = f"/home/user/data/models/{model_name}"
|
23 |
+
os.makedirs(model_dir, exist_ok=True)
|
24 |
+
|
25 |
+
model_url = os.getenv("GGUF_MODEL_URL") # Assuming URL is provided as an env variable
|
26 |
+
|
27 |
+
model_file_path = os.path.join(model_dir, os.path.basename(model_url))
|
28 |
+
|
29 |
+
print(f"Downloading model from {model_url}...")
|
30 |
+
response = requests.get(model_url, stream=True)
|
31 |
+
if response.status_code == 200:
|
32 |
+
with open(model_file_path, 'wb') as f:
|
33 |
+
f.write(response.content)
|
34 |
+
print(f"Model downloaded and saved to {model_file_path}")
|
35 |
+
else:
|
36 |
+
print(f"Failed to download the model. Status code: {response.status_code}")
|
37 |
+
|
38 |
+
def download_model():
|
39 |
+
model_class = os.getenv("MODEL_CLASS")
|
40 |
+
if model_class == 'gguf':
|
41 |
+
download_gguf_model()
|
42 |
+
elif model_class == 'hf':
|
43 |
+
download_hf_model()
|
44 |
+
else:
|
45 |
+
print(f"Unsupported model class: {model_class}")
|
46 |
+
|
47 |
if __name__ == "__main__":
|
48 |
download_model()
|
|
entrypoint.sh
CHANGED
@@ -3,8 +3,8 @@
|
|
3 |
# Authenticate with Hugging Face
|
4 |
export HF_HOME=/home/user/data/hf_cache
|
5 |
|
|
|
6 |
echo "Using Hugging Face API token for authentication"
|
7 |
-
export HF_TOKEN=${HF_TOKEN}
|
8 |
|
9 |
# Use the environment variable for the model name
|
10 |
MODEL_DIR="/home/user/data/models/${HF_MODEL_NAME}"
|
@@ -13,11 +13,15 @@ MODEL_URL=${HF_MODEL_URL} # Ensure consistent variable naming
|
|
13 |
# Download the model if it does not exist
|
14 |
if [ ! -d "$MODEL_DIR" ]; then
|
15 |
echo "Model not found. Downloading ${HF_MODEL_NAME} from ${MODEL_URL}..."
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
21 |
else
|
22 |
echo "Model ${HF_MODEL_NAME} already present."
|
23 |
fi
|
|
|
3 |
# Authenticate with Hugging Face
|
4 |
export HF_HOME=/home/user/data/hf_cache
|
5 |
|
6 |
+
# Assuming HF_TOKEN is already exported to the environment
|
7 |
echo "Using Hugging Face API token for authentication"
|
|
|
8 |
|
9 |
# Use the environment variable for the model name
|
10 |
MODEL_DIR="/home/user/data/models/${HF_MODEL_NAME}"
|
|
|
13 |
# Download the model if it does not exist
|
14 |
if [ ! -d "$MODEL_DIR" ]; then
|
15 |
echo "Model not found. Downloading ${HF_MODEL_NAME} from ${MODEL_URL}..."
|
16 |
+
# Navigate to the directory where download_model.py is located
|
17 |
+
cd /home/user/app/app/utils
|
18 |
+
|
19 |
+
# Execute the download_model script
|
20 |
+
echo "Downloading the model..."
|
21 |
+
python download_model.py || { echo "Model download failed"; exit 1; }
|
22 |
+
|
23 |
+
# Navigate back to the app directory
|
24 |
+
cd /home/user/app
|
25 |
else
|
26 |
echo "Model ${HF_MODEL_NAME} already present."
|
27 |
fi
|