Fix NLTK data permissions and Docker user setup
Browse files- Dockerfile +18 -10
- app/main.py +35 -32
Dockerfile
CHANGED
@@ -5,7 +5,8 @@ ENV DEBIAN_FRONTEND=noninteractive \
|
|
5 |
PYTHONUNBUFFERED=1 \
|
6 |
PYTHONDONTWRITEBYTECODE=1 \
|
7 |
PORT=7860 \
|
8 |
-
BASE_DIR=/app
|
|
|
9 |
|
10 |
# Install Python and other dependencies
|
11 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
@@ -20,25 +21,32 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
20 |
curl \
|
21 |
&& rm -rf /var/lib/apt/lists/*
|
22 |
|
|
|
|
|
|
|
23 |
# Set working directory
|
24 |
WORKDIR /app
|
25 |
|
26 |
-
# Create necessary directories
|
27 |
-
RUN mkdir -p /app/data/pdfs /app/data/texts /app/data/index
|
|
|
28 |
|
29 |
# Copy requirements first for better caching
|
30 |
-
COPY requirements.txt .
|
|
|
|
|
|
|
31 |
|
32 |
# Install Python packages
|
33 |
-
RUN pip3 install --no-cache-dir -r requirements.txt
|
34 |
-
RUN pip3 install --no-cache-dir gradio
|
35 |
|
36 |
# Copy your application code and data
|
37 |
-
COPY app/ /app/
|
38 |
-
COPY data/ /app/data/
|
39 |
|
40 |
-
#
|
41 |
-
COPY spaces_app.py /app/
|
42 |
|
43 |
# Expose the port Hugging Face Spaces expects
|
44 |
EXPOSE 7860
|
|
|
5 |
PYTHONUNBUFFERED=1 \
|
6 |
PYTHONDONTWRITEBYTECODE=1 \
|
7 |
PORT=7860 \
|
8 |
+
BASE_DIR=/app \
|
9 |
+
NLTK_DATA=/app/nltk_data
|
10 |
|
11 |
# Install Python and other dependencies
|
12 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
|
21 |
curl \
|
22 |
&& rm -rf /var/lib/apt/lists/*
|
23 |
|
24 |
+
# Create a non-root user
|
25 |
+
RUN useradd -m -u 1000 user
|
26 |
+
|
27 |
# Set working directory
|
28 |
WORKDIR /app
|
29 |
|
30 |
+
# Create necessary directories and set permissions
|
31 |
+
RUN mkdir -p /app/data/pdfs /app/data/texts /app/data/index /app/nltk_data && \
|
32 |
+
chown -R user:user /app
|
33 |
|
34 |
# Copy requirements first for better caching
|
35 |
+
COPY --chown=user:user requirements.txt .
|
36 |
+
|
37 |
+
# Switch to non-root user
|
38 |
+
USER user
|
39 |
|
40 |
# Install Python packages
|
41 |
+
RUN pip3 install --no-cache-dir --user -r requirements.txt
|
42 |
+
RUN pip3 install --no-cache-dir --user gradio
|
43 |
|
44 |
# Copy your application code and data
|
45 |
+
COPY --chown=user:user app/ /app/
|
46 |
+
COPY --chown=user:user data/ /app/data/
|
47 |
|
48 |
+
# Copy your Gradio interface
|
49 |
+
COPY --chown=user:user spaces_app.py /app/
|
50 |
|
51 |
# Expose the port Hugging Face Spaces expects
|
52 |
EXPOSE 7860
|
app/main.py
CHANGED
@@ -414,29 +414,34 @@ logger = logging.getLogger(__name__)
|
|
414 |
# Initialize global variables in app state
|
415 |
@app.on_event("startup")
|
416 |
async def startup_event():
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
# Download NLTK data
|
421 |
-
logger.info("Downloading NLTK data...")
|
422 |
-
try:
|
423 |
-
await asyncio.to_thread(nltk.download, 'punkt', quiet=True)
|
424 |
-
await asyncio.to_thread(nltk.download, 'punkt_tab', quiet=True)
|
425 |
-
except Exception as e:
|
426 |
-
logger.error(f"Error downloading NLTK data: {str(e)}")
|
427 |
-
raise
|
428 |
-
|
429 |
-
# Initialize the model
|
430 |
-
logger.info("Initializing model...")
|
431 |
-
|
432 |
-
# Check if CUDA is available
|
433 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
434 |
-
logger.info(f"Using device: {device}")
|
435 |
|
436 |
-
|
437 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
438 |
|
439 |
-
|
|
|
|
|
|
|
440 |
"text-generation",
|
441 |
model=MODEL_ID,
|
442 |
trust_remote_code=True,
|
@@ -444,16 +449,14 @@ async def startup_event():
|
|
444 |
device_map="auto",
|
445 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
446 |
)
|
447 |
-
|
448 |
-
|
449 |
-
# Load or create index and related components
|
450 |
-
logger.info("Loading FAISS index and documents...")
|
451 |
-
app.state.index, app.state.documents, app.state.embedding_model = await load_or_create_index()
|
452 |
-
logger.info("FAISS index and documents loaded successfully")
|
453 |
|
|
|
|
|
|
|
454 |
except Exception as e:
|
455 |
-
logger.error(f"
|
456 |
-
raise
|
457 |
|
458 |
@app.post("/generate")
|
459 |
async def generate_content(request: ContentRequest):
|
@@ -485,9 +488,9 @@ async def generate_content(request: ContentRequest):
|
|
485 |
|
486 |
response = generate_response_with_rag(
|
487 |
request.topic, # Use topic as the prompt
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
settings
|
492 |
)
|
493 |
|
|
|
414 |
# Initialize global variables in app state
|
415 |
@app.on_event("startup")
|
416 |
async def startup_event():
|
417 |
+
# Check if CUDA is available
|
418 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
419 |
+
logger.info(f"Using device: {device}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
420 |
|
421 |
+
if device == "cpu":
|
422 |
+
logger.warning("GPU not detected. Model will run slower on CPU.")
|
423 |
+
|
424 |
+
"""Initialize the application on startup."""
|
425 |
+
logger = logging.getLogger(__name__)
|
426 |
+
logger.info("Starting application initialization...")
|
427 |
+
|
428 |
+
# Set NLTK data path
|
429 |
+
nltk_data_dir = os.environ.get('NLTK_DATA', os.path.join(os.path.expanduser('~'), 'nltk_data'))
|
430 |
+
os.makedirs(nltk_data_dir, exist_ok=True)
|
431 |
+
|
432 |
+
# Download NLTK data
|
433 |
+
logger.info("Downloading NLTK data...")
|
434 |
+
try:
|
435 |
+
await asyncio.to_thread(nltk.download, 'punkt', download_dir=nltk_data_dir, quiet=True)
|
436 |
+
await asyncio.to_thread(nltk.download, 'punkt_tab', download_dir=nltk_data_dir, quiet=True)
|
437 |
+
except Exception as e:
|
438 |
+
logger.error(f"Error downloading NLTK data: {str(e)}")
|
439 |
+
raise Exception(f"Failed to initialize application: {str(e)}")
|
440 |
|
441 |
+
# Initialize the model and index
|
442 |
+
try:
|
443 |
+
global model, embedding_model, faiss_index, documents
|
444 |
+
model = pipeline(
|
445 |
"text-generation",
|
446 |
model=MODEL_ID,
|
447 |
trust_remote_code=True,
|
|
|
449 |
device_map="auto",
|
450 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
451 |
)
|
452 |
+
embedding_model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
|
|
|
|
|
|
|
|
|
|
|
453 |
|
454 |
+
# Load or create the FAISS index
|
455 |
+
faiss_index, documents, embedding_model = await load_or_create_index()
|
456 |
+
logger.info("Application initialization completed successfully")
|
457 |
except Exception as e:
|
458 |
+
logger.error(f"Error initializing application: {str(e)}")
|
459 |
+
raise Exception(f"Failed to initialize application: {str(e)}")
|
460 |
|
461 |
@app.post("/generate")
|
462 |
async def generate_content(request: ContentRequest):
|
|
|
488 |
|
489 |
response = generate_response_with_rag(
|
490 |
request.topic, # Use topic as the prompt
|
491 |
+
faiss_index,
|
492 |
+
embedding_model,
|
493 |
+
documents,
|
494 |
settings
|
495 |
)
|
496 |
|