devusman commited on
Commit
870c988
·
1 Parent(s): 42693f7

updated this

Browse files
Files changed (3) hide show
  1. Dockerfile +12 -4
  2. app.py +12 -21
  3. requirements.txt +5 -1
Dockerfile CHANGED
@@ -1,16 +1,24 @@
1
- # Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
- # you will also find guides on how best to write your Dockerfile
3
-
4
  FROM python:3.9
5
 
 
6
  RUN useradd -m -u 1000 user
7
  USER user
 
 
8
  ENV PATH="/home/user/.local/bin:$PATH"
9
 
 
10
  WORKDIR /app
11
 
 
12
  COPY --chown=user ./requirements.txt requirements.txt
 
13
  RUN pip install --no-cache-dir --upgrade -r requirements.txt
14
 
 
15
  COPY --chown=user . /app
16
- CMD ["gunicorn","-b","0.0.0.0:7860", "app:app"]
 
 
 
 
 
 
 
 
1
  FROM python:3.9
2
 
3
+ # Create a non-root user for better security
4
  RUN useradd -m -u 1000 user
5
  USER user
6
+
7
+ # Add the user's local bin to the PATH. This is where pip installs executables.
8
  ENV PATH="/home/user/.local/bin:$PATH"
9
 
10
+ # Set the working directory inside the container
11
  WORKDIR /app
12
 
13
+ # Copy only the requirements.txt first to leverage Docker layer caching
14
  COPY --chown=user ./requirements.txt requirements.txt
15
+ # Install the dependencies specified in requirements.txt
16
  RUN pip install --no-cache-dir --upgrade -r requirements.txt
17
 
18
+ # Copy the rest of your application code into the container
19
  COPY --chown=user . /app
20
+
21
+ # The CMD instruction tells Docker how to run your application.
22
+ # This command starts the Gunicorn server, binding it to port 7860 on all network interfaces,
23
+ # and points it to the 'app' object inside your 'app.py' file.
24
+ CMD ["gunicorn", "-b", "0.0.0.0:7860", "app:app"]
app.py CHANGED
@@ -1,23 +1,19 @@
1
- # app.py
2
-
3
  import os
4
  from flask import Flask, request, jsonify
5
  from flask_cors import CORS
6
  import spacy
7
 
8
  # --- CORRECTED MODEL LOADING SECTION ---
9
- # This path points to the 'it_core_news_pruned' directory
10
- # located in the same folder as this 'app.py' script.
 
11
  try:
12
- # Get the absolute path to the directory containing this script
13
- base_dir = os.path.dirname(os.path.abspath(__file__))
14
- # Join it with the name of our model directory
15
- model_path = os.path.join(base_dir, 'it_core_news_pruned')
16
- # Load the model from the specified path
17
- nlp = spacy.load(model_path)
18
- except Exception as e:
19
- # This provides a more detailed error if loading fails
20
- raise RuntimeError(f"Error loading spaCy model from {model_path}: {e}")
21
  # --- END SECTION ---
22
 
23
  # Initialize the Flask app
@@ -137,11 +133,6 @@ def analyze_sentence():
137
  print(f"An error occurred: {e}")
138
  return jsonify({"error": "An internal error occurred. See server logs for details."}), 500
139
 
140
- # --- FIX: ADD THIS BLOCK TO BIND TO THE CORRECT HOST AND PORT ---
141
- if __name__ == "__main__":
142
- # The host '0.0.0.0' makes the server publicly available
143
- # The port is dynamically read from the 'PORT' environment variable,
144
- # with a default of 10000 for local testing.
145
- port = int(os.environ.get('PORT', 10000))
146
- app.run(host='0.0.0.0', port=port)
147
- # --- END FIX ---
 
 
 
1
  import os
2
  from flask import Flask, request, jsonify
3
  from flask_cors import CORS
4
  import spacy
5
 
6
  # --- CORRECTED MODEL LOADING SECTION ---
7
+ # This approach loads the model by its package name. It is more robust because
8
+ # the model is now managed as a dependency in requirements.txt,
9
+ # removing the need to manually place a model folder next to the script.
10
  try:
11
+ nlp = spacy.load("it_core_news_sm")
12
+ except OSError:
13
+ raise RuntimeError(
14
+ "Could not find the 'it_core_news_sm' model. "
15
+ "Please ensure it is listed and installed from your requirements.txt file."
16
+ )
 
 
 
17
  # --- END SECTION ---
18
 
19
  # Initialize the Flask app
 
133
  print(f"An error occurred: {e}")
134
  return jsonify({"error": "An internal error occurred. See server logs for details."}), 500
135
 
136
+ # The __main__ block has been removed because it is not used by Gunicorn.
137
+ # The Dockerfile's CMD instruction is the single source of truth for running the app,
138
+ # which prevents confusion about which host and port are being used.
 
 
 
 
 
requirements.txt CHANGED
@@ -1,3 +1,7 @@
1
  flask
 
 
2
  spacy
3
- Flask-Cors
 
 
 
1
  flask
2
+ Flask-Cors
3
+ gunicorn
4
  spacy
5
+ # The following line installs the small Italian model for spaCy
6
+ # directly via pip, making your project self-contained.
7
+ it_core_news_sm @ https://github.com/explosion/spacy-models/releases/download/it_core_news_sm-3.7.0/it_core_news_sm-3.7.0.tar.gz