# Use a lightweight base image with Python
FROM python:3.9-slim

# Install git and git-lfs for cloning large models
RUN apt-get update && apt-get install -y git git-lfs

# Initialize git-lfs
RUN git-lfs install

# Create a non-root user and set up environment variables
RUN useradd -m -u 1000 user

# Set environment variables
ENV HOME=/home/user \
    TRANSFORMERS_CACHE=/home/user/.cache \
    PATH=/home/user/.local/bin:$PATH

# Switch to the newly created user
USER user

# Set the working directory inside the container
WORKDIR $HOME/app

# Copy the requirements file
COPY --chown=user:user requirements.txt .

# Clone the Hugging Face model repository (replace with the desired model URL)
RUN git clone https://huggingface.co/matsant01/STEMerald-2b $HOME/app/model

# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the application code and set file ownership
COPY --chown=user:user . .

# Copy the FastAPI app
COPY app.py .

# Expose the port and run the app with Uvicorn 
EXPOSE 7860
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]