Spaces:
Sleeping
Sleeping
updated this
Browse files- Dockerfile +12 -4
- app.py +12 -21
- 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 10 |
-
#
|
|
|
|
| 11 |
try:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 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 |
-
#
|
| 141 |
-
|
| 142 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
| 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
|