samdak93 commited on
Commit
9240df3
·
1 Parent(s): 22a3c8a

chnage dockerfile

Browse files
Files changed (2) hide show
  1. Dockerfile +13 -9
  2. app.py +7 -4
Dockerfile CHANGED
@@ -10,25 +10,29 @@ RUN apt-get update && apt-get install -y \
10
  python3-dev \
11
  && rm -rf /var/lib/apt/lists/*
12
 
13
- # Make sure you set the environment variables
14
- ENV TRANSFORMERS_CACHE=/app/cache
15
- ENV HF_HOME=/app/cache
16
-
17
- # Create the writable directory for cache
18
- RUN mkdir -p /app/cache
19
 
 
 
 
20
 
21
  # Copy app code
22
  COPY app.py /app/app.py
23
  COPY requirements.txt /app/requirements.txt
24
 
25
-
26
-
27
  # Install Python dependencies
28
  RUN pip install --upgrade pip && pip install -r requirements.txt
29
 
 
 
 
 
 
 
 
30
  # Expose the port
31
  EXPOSE 7860
32
 
33
  # Start the Flask app
34
- CMD ["python", "app.py"]
 
10
  python3-dev \
11
  && rm -rf /var/lib/apt/lists/*
12
 
13
+ # Create non-root user to run the app
14
+ RUN useradd -m appuser
 
 
 
 
15
 
16
+ # Create the cache directory and set proper permissions
17
+ RUN mkdir -p /app/cache && \
18
+ chown -R appuser:appuser /app
19
 
20
  # Copy app code
21
  COPY app.py /app/app.py
22
  COPY requirements.txt /app/requirements.txt
23
 
 
 
24
  # Install Python dependencies
25
  RUN pip install --upgrade pip && pip install -r requirements.txt
26
 
27
+ # Set environment variables for the model caching
28
+ ENV TRANSFORMERS_CACHE=/app/cache
29
+ ENV HF_HOME=/app/cache
30
+
31
+ # Switch to non-root user
32
+ USER appuser
33
+
34
  # Expose the port
35
  EXPOSE 7860
36
 
37
  # Start the Flask app
38
+ CMD ["python", "app.py"]
app.py CHANGED
@@ -1,11 +1,11 @@
1
  import os
2
- os.environ['TRANSFORMERS_CACHE'] = '/app/cache'
3
- os.environ['HF_HOME'] = '/app/cache'
 
4
 
5
  from flask import Flask, request, jsonify
6
  from flask_cors import CORS
7
  from transformers import pipeline
8
- import os
9
  import re
10
  import logging
11
  from datetime import datetime
@@ -140,6 +140,9 @@ def api_parse_recipe():
140
  return jsonify({"error": str(e)}), 500
141
 
142
  if __name__ == '__main__':
 
 
 
143
  load_model()
144
  port = int(os.environ.get('PORT', 7860))
145
- app.run(host='0.0.0.0', port=port)
 
1
  import os
2
+ # Set cache directories to use /tmp which is typically writable
3
+ os.environ['TRANSFORMERS_CACHE'] = '/tmp/huggingface'
4
+ os.environ['HF_HOME'] = '/tmp/huggingface'
5
 
6
  from flask import Flask, request, jsonify
7
  from flask_cors import CORS
8
  from transformers import pipeline
 
9
  import re
10
  import logging
11
  from datetime import datetime
 
140
  return jsonify({"error": str(e)}), 500
141
 
142
  if __name__ == '__main__':
143
+ # Create cache directory if it doesn't exist
144
+ os.makedirs(os.environ['TRANSFORMERS_CACHE'], exist_ok=True)
145
+
146
  load_model()
147
  port = int(os.environ.get('PORT', 7860))
148
+ app.run(host='0.0.0.0', port=port)