Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4c98845
gitignore update
samuelveigarangel Sep 15, 2025
7fcb03e
Update modeladmin to snippetview
samuelveigarangel Sep 15, 2025
050297e
Add label em inlinePanel
samuelveigarangel Sep 15, 2025
b0f1a31
Atualiza o download do modelo para command
samuelveigarangel Sep 15, 2025
a32aadf
Fix __init__
samuelveigarangel Sep 15, 2025
a4161d8
Move LLAMA_MODEL_DIR e MODEL_LLAMA
samuelveigarangel Sep 15, 2025
5de0daf
black
samuelveigarangel Sep 15, 2025
71c0a4b
remove token HF_TOKEN
samuelveigarangel Sep 25, 2025
f5caf9f
Update README.md
samuelveigarangel Sep 18, 2025
648760b
remove dep repetida no mesmo arquivo
pitangainnovare Oct 3, 2025
6865310
remove dep llama e hugging face
pitangainnovare Oct 3, 2025
51dd32e
cria requirement adicional específico para llama e huggingface
pitangainnovare Oct 3, 2025
2b00c39
Altera Dockerfile padrão (local e production) para não terem llama
pitangainnovare Oct 3, 2025
67d1f70
Cria variável settings para indicar se Llama está ou não ativado
pitangainnovare Oct 3, 2025
78a42aa
Cria um yml para usar fazer deploy local de imagem com llama
pitangainnovare Oct 3, 2025
ca0819e
Cria dockerfile com llama (prod e local)
pitangainnovare Oct 3, 2025
3302460
Cria um comando em make para construir imagem com llama (build_llama)
pitangainnovare Oct 3, 2025
7e3a713
Ajusta import em módulo que cria GenericLlama
pitangainnovare Oct 3, 2025
524d97e
Cria exceções para melhor lidar com os erros de instanciação do Llama
pitangainnovare Oct 3, 2025
b51c734
Impede criação do GenericLlama
pitangainnovare Oct 3, 2025
beb0036
Caso o singleton não esteja instanciado, faz import do llama
pitangainnovare Oct 3, 2025
1509008
Verifica se o caminho do modelo existe
pitangainnovare Oct 3, 2025
c9e2681
Cria uma instância do LLama
pitangainnovare Oct 3, 2025
2a57bed
E povoa o cache (singleton pattern)
pitangainnovare Oct 3, 2025
975045b
Ajuste return para melhor legibilidade
pitangainnovare Oct 3, 2025
77d3aa3
Ajuste o marker para melhor lidar com as alterações no GenericLlama
pitangainnovare Oct 3, 2025
cb2021a
Remove instalação duplicada de deps
pitangainnovare Oct 3, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .envs/.local/.django
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ CELERY_FLOWER_PASSWORD=QgScyefPrYhHgO6onW61u0nazc5xdBuP4sM7jMRrBBFuA2RjsFhZLp7xb

# Timeout fetch_data
# ------------------------------------------------------------------------------
FETCH_DATA_TIMEOUT=2
FETCH_DATA_TIMEOUT=2


HF_TOKEN=
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,6 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# End of https://www.toptal.com/developers/gitignore/api/django
# End of https://www.toptal.com/developers/gitignore/api/django

llama3/llama-3.2/*
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ build_date: ## Show build date
build: ## Build app using $(compose)
@docker compose -f $(compose) build

build_llama: ## Build app using $(compose) with llama enabled
@docker compose -f llama.$(compose) build

build_no_cache: ## Build app using $(compose)
@docker compose -f $(compose) build --no-cache

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,7 @@ Signup at: [https://sentry.io/signup/?code=cookiecutter](https://sentry.io/signu
## Deployment

See full [Docker deployment guide](http://cookiecutter-django.readthedocs.io/en/latest/deployment-with-docker.html).

## Download Model

https://github.com/scieloorg/markapi/wiki/Guia-r%C3%A1pido:-baixar-e-configurar-o-modelo-do-MarkAPI-para-marca%C3%A7%C3%A3o-de-refer%C3%AAncias-em-PDF
27 changes: 2 additions & 25 deletions compose/local/django/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ RUN apt-get update && \
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 50 && \
apt-get clean && rm -rf /var/lib/apt/lists/*

# Instalar ninja-build y cmake
RUN apt-get install -y ninja-build cmake

# Configurar variables de entorno para compilar con BLAS y SIMD condicionalmente
ARG ENABLE_OPTIMIZATIONS=true
ARG ENABLE_OPTIMIZATIONS=true
ENV CFLAGS="${ENABLE_OPTIMIZATIONS:+-mfma -mavx2}" \
CXXFLAGS="${ENABLE_OPTIMIZATIONS:+-mfma -mavx2}" \
CMAKE_ARGS="${ENABLE_OPTIMIZATIONS:+-DGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS}"

# Actualizar pip, setuptools y wheel antes de instalar dependencias
RUN python -m pip install --upgrade pip setuptools wheel

Expand All @@ -63,12 +53,6 @@ ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV BUILD_ENV ${BUILD_ENVIRONMENT}

# Disable AVX support for llama-cpp-python if needed
ARG DISABLE_AVX=false

# Set the version of llama-cpp-python
ARG LLAMA_VERSION=0.3.14

WORKDIR ${APP_HOME}

RUN sed -i 's/main/main contrib non-free/' /etc/apt/sources.list
Expand All @@ -89,17 +73,10 @@ RUN apt-get update && apt-get install --no-install-recommends -y \
# copy python dependency wheels from python-build-stage
COPY --from=python-build-stage /usr/src/app/wheels /wheels/

# Use wheels to install python dependencies (excluding llama-cpp-python)
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ $(find /wheels/ -name "*.whl" ! -name "llama_cpp_python*") \
# Use wheels to install python dependencies
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ $(find /wheels/ -name "*.whl") \
&& rm -rf /wheels/

# Install llama-cpp-python with specific CMAKE flags for Kubernetes nodes with or without AVX support
RUN if [ "${DISABLE_AVX}" = "true" ]; then \
CMAKE_ARGS='-DLLAMA_AVX=OFF -DLLAMA_AVX2=OFF -DLLAMA_FMA=OFF -DLLAMA_F16C=OFF -DLLAMA_OPENMP=ON' pip install llama-cpp-python==${LLAMA_VERSION} --force-reinstall --no-cache-dir; \
else \
pip install llama-cpp-python==${LLAMA_VERSION} --force-reinstall --no-cache-dir; \
fi

COPY ./compose/production/django/entrypoint /entrypoint
RUN sed -i 's/\r$//g' /entrypoint
RUN chmod +x /entrypoint
Expand Down
110 changes: 110 additions & 0 deletions compose/local/django/Dockerfile.llama
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
ARG PYTHON_VERSION=3.11-bullseye

# define an alias for the specfic python version used in this file.
FROM python:${PYTHON_VERSION} AS python

# Python build stage
FROM python AS python-build-stage

ARG BUILD_ENVIRONMENT=local

# Install apt packages
RUN apt-get update && apt-get install --no-install-recommends -y \
# dependencies for building Python packages
build-essential \
git \
# psycopg2 dependencies
libpq-dev \
# other dependencies
software-properties-common \
libopenblas-dev \
libomp-dev

# Instalar gcc-10 y g++-10 en Debian Bullseye
RUN apt-get update && \
apt-get install -y gcc-10 g++-10 ninja-build cmake && \
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 50 && \
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 50 && \
apt-get clean && rm -rf /var/lib/apt/lists/*

# Configurar variables de entorno para compilar con BLAS y SIMD condicionalmente
Copy link

Copilot AI Oct 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment should be in English to maintain consistency with the rest of the codebase.

Suggested change
# Configurar variables de entorno para compilar con BLAS y SIMD condicionalmente
# Configure environment variables to compile with BLAS and SIMD conditionally

Copilot uses AI. Check for mistakes.
ARG ENABLE_OPTIMIZATIONS=true
ENV CFLAGS="${ENABLE_OPTIMIZATIONS:+-mfma -mavx2}" \
CXXFLAGS="${ENABLE_OPTIMIZATIONS:+-mfma -mavx2}" \
CMAKE_ARGS="${ENABLE_OPTIMIZATIONS:+-DGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS}"
Copy link

Copilot AI Oct 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CMAKE_ARGS environment variable name on line 37 conflicts with the variable used in the generic_llama.py file for a different purpose. Consider using a more specific name like LLAMA_CMAKE_ARGS to avoid confusion.

Suggested change
CMAKE_ARGS="${ENABLE_OPTIMIZATIONS:+-DGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS}"
LLAMA_CMAKE_ARGS="${ENABLE_OPTIMIZATIONS:+-DGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS}"

Copilot uses AI. Check for mistakes.

# Actualizar pip, setuptools y wheel antes de instalar dependencias
Copy link

Copilot AI Oct 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment should be in English to maintain consistency with the rest of the codebase.

Suggested change
# Actualizar pip, setuptools y wheel antes de instalar dependencias
# Update pip, setuptools, and wheel before installing dependencies

Copilot uses AI. Check for mistakes.
RUN python -m pip install --upgrade pip setuptools wheel

# Requirements are installed here to ensure they will be cached.
COPY ./requirements .

# Create application directory to hold DOCX layouts
COPY ./docx_layouts .

# Update pip
RUN python -m pip install --upgrade pip

# Create Python Dependency and Sub-Dependency Wheels.
RUN pip wheel --wheel-dir /usr/src/app/wheels \
-r ${BUILD_ENVIRONMENT}.txt \
-r extra-llama.txt

# Python 'run' stage
FROM python AS python-run-stage

ARG BUILD_ENVIRONMENT=local
ARG APP_HOME=/app

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV BUILD_ENV ${BUILD_ENVIRONMENT}

WORKDIR ${APP_HOME}

RUN sed -i 's/main/main contrib non-free/' /etc/apt/sources.list

# Install required system dependencies
RUN apt-get update && apt-get install --no-install-recommends -y \
# psycopg2 dependencies
libpq-dev \
# Translations dependencies
gettext \
# libreoffice for document conversions
default-jre libreoffice libreoffice-java-common ttf-mscorefonts-installer fonts-liberation fonts-liberation2 fonts-crosextra-carlito fonts-crosextra-caladea fonts-dejavu fonts-noto \
# cleaning up unused files
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& rm -rf /var/lib/apt/lists/*

# All absolute dir copies ignore workdir instruction. All relative dir copies are wrt to the workdir instruction
# copy python dependency wheels from python-build-stage
COPY --from=python-build-stage /usr/src/app/wheels /wheels/

# Use wheels to install python dependencies
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ $(find /wheels/ -name "*.whl") \
&& rm -rf /wheels/

COPY ./compose/production/django/entrypoint /entrypoint
RUN sed -i 's/\r$//g' /entrypoint
RUN chmod +x /entrypoint

COPY ./compose/local/django/start /start
RUN sed -i 's/\r$//g' /start
RUN chmod +x /start

COPY ./compose/local/django/celery/worker/start /start-celeryworker
RUN sed -i 's/\r$//g' /start-celeryworker
RUN chmod +x /start-celeryworker

COPY ./compose/local/django/celery/beat/start /start-celerybeat
RUN sed -i 's/\r$//g' /start-celerybeat
RUN chmod +x /start-celerybeat

COPY ./compose/local/django/celery/flower/start /start-flower
RUN sed -i 's/\r$//g' /start-flower
RUN chmod +x /start-flower

# copy application code to WORKDIR
COPY . ${APP_HOME}

ENTRYPOINT ["/entrypoint"]
17 changes: 2 additions & 15 deletions compose/production/django/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV BUILD_ENV ${BUILD_ENVIRONMENT}

# Install llama-cpp-python with specific CMAKE flags for Kubernetes nodes without AVX support
ARG DISABLE_AVX=true

# Set the version of llama-cpp-python
ARG LLAMA_VERSION=0.3.14

WORKDIR ${APP_HOME}

RUN addgroup --system django \
Expand All @@ -68,15 +62,8 @@ RUN apt-get update && apt-get install --no-install-recommends -y \
# copy python dependency wheels from python-build-stage
COPY --from=python-build-stage /usr/src/app/wheels /wheels/

# use wheels to install python dependencies (excluding llama-cpp-python)
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ $(find /wheels/ -name "*.whl" ! -name "llama_cpp_python*") && rm -rf /wheels/

# Install llama-cpp-python with specific CMAKE flags for Kubernetes nodes without AVX support
RUN if [ "${DISABLE_AVX}" = "true" ]; then \
CMAKE_ARGS='-DLLAMA_AVX=OFF -DLLAMA_AVX2=OFF -DLLAMA_FMA=OFF -DLLAMA_F16C=OFF -DLLAMA_OPENMP=ON' pip install llama-cpp-python==${LLAMA_VERSION} --force-reinstall --no-cache-dir; \
else \
pip install llama-cpp-python==${LLAMA_VERSION} --force-reinstall --no-cache-dir; \
fi
# use wheels to install python dependencies
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ $(find /wheels/ -name "*.whl") && rm -rf /wheels/

COPY --chown=django:django ./compose/production/django/entrypoint /entrypoint
RUN sed -i 's/\r$//g' /entrypoint
Expand Down
109 changes: 109 additions & 0 deletions compose/production/django/Dockerfile.llama
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
ARG PYTHON_VERSION=3.11-bullseye

# define an alias for the specfic python version used in this file.
FROM python:${PYTHON_VERSION} as python

# Python build stage
FROM python as python-build-stage

ARG BUILD_ENVIRONMENT=production

# Install apt packages
RUN apt-get update && apt-get install --no-install-recommends -y \
# dependencies for building Python packages
git \
build-essential \
# psycopg2 dependencies
libpq-dev

# Requirements are installed here to ensure they will be cached.
COPY ./requirements .

# Create application directory to hold DOCX layouts
COPY ./docx_layouts .

# Update pip
RUN python -m pip install --upgrade pip

# Create Python Dependency and Sub-Dependency Wheels.
RUN pip wheel --wheel-dir /usr/src/app/wheels \
-r ${BUILD_ENVIRONMENT}.txt

# Python 'run' stage
FROM python as python-run-stage

ARG BUILD_ENVIRONMENT=production
ARG APP_HOME=/app

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV BUILD_ENV ${BUILD_ENVIRONMENT}

# Install llama-cpp-python with specific CMAKE flags for Kubernetes nodes without AVX support
ARG DISABLE_AVX=true

# Set the version of llama-cpp-python
ARG LLAMA_VERSION=0.3.14

WORKDIR ${APP_HOME}

RUN addgroup --system django \
&& adduser --system --ingroup django django

RUN sed -i 's/main/main contrib non-free/' /etc/apt/sources.list

# Install required system dependencies
RUN apt-get update && apt-get install --no-install-recommends -y \
# psycopg2 dependencies
libpq-dev \
# libreoffice for document conversions
default-jre libreoffice libreoffice-java-common ttf-mscorefonts-installer fonts-liberation fonts-liberation2 fonts-crosextra-carlito fonts-crosextra-caladea fonts-dejavu fonts-noto \
# Translations dependencies
gettext \
# cleaning up unused files
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& rm -rf /var/lib/apt/lists/*

# All absolute dir copies ignore workdir instruction. All relative dir copies are wrt to the workdir instruction
# copy python dependency wheels from python-build-stage
COPY --from=python-build-stage /usr/src/app/wheels /wheels/

# use wheels to install python dependencies (excluding llama-cpp-python)
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ $(find /wheels/ -name "*.whl" ! -name "llama_cpp_python*") && rm -rf /wheels/

# Install llama-cpp-python with specific CMAKE flags for Kubernetes nodes without AVX support
RUN if [ "${DISABLE_AVX}" = "true" ]; then \
CMAKE_ARGS='-DLLAMA_AVX=OFF -DLLAMA_AVX2=OFF -DLLAMA_FMA=OFF -DLLAMA_F16C=OFF -DLLAMA_OPENMP=ON' pip install llama-cpp-python==${LLAMA_VERSION} --force-reinstall --no-cache-dir; \
else \
pip install llama-cpp-python==${LLAMA_VERSION} --force-reinstall --no-cache-dir; \
fi

COPY --chown=django:django ./compose/production/django/entrypoint /entrypoint
RUN sed -i 's/\r$//g' /entrypoint
RUN chmod +x /entrypoint

COPY --chown=django:django ./compose/production/django/start /start
RUN sed -i 's/\r$//g' /start
RUN chmod +x /start

COPY --chown=django:django ./compose/production/django/celery/worker/start /start-celeryworker
RUN sed -i 's/\r$//g' /start-celeryworker
RUN chmod +x /start-celeryworker

COPY --chown=django:django ./compose/production/django/celery/beat/start /start-celerybeat
RUN sed -i 's/\r$//g' /start-celerybeat
RUN chmod +x /start-celerybeat

COPY ./compose/production/django/celery/flower/start /start-flower
RUN sed -i 's/\r$//g' /start-flower
RUN chmod +x /start-flower

# copy application code to WORKDIR
COPY --chown=django:django . ${APP_HOME}

# make django owner of the WORKDIR directory as well.
RUN chown django:django ${APP_HOME}

USER django

ENTRYPOINT ["/entrypoint"]
9 changes: 6 additions & 3 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
ROOT_DIR = Path(__file__).resolve(strict=True).parent.parent.parent
# core/
APPS_DIR = ROOT_DIR / "core"
LLAMA_MODEL_DIR = ROOT_DIR / "llama3/llama-3.2"
MODEL_LLAMA = "llama-3.2-3b-instruct-q4_k_m.gguf"

env = environ.Env()
READ_DOT_ENV_FILE = env.bool("DJANGO_READ_DOT_ENV_FILE", default=False)
Expand Down Expand Up @@ -294,4 +292,9 @@
"ACCESS_TOKEN_LIFETIME": timedelta(minutes=60),
"REFRESH_TOKEN_LIFETIME": timedelta(days=1),
# "AUTH_TOKEN_CLASSES": ("rest_framework_simplejwt.tokens.AccessToken",),
}
}

# LLAMA
LLAMA_ENABLED = env.bool("LLAMA_ENABLED", default=False)
LLAMA_MODEL_DIR = ROOT_DIR / "llama3/llama-3.2"
MODEL_LLAMA = "llama-3.2-3b-instruct-q4_k_m.gguf"
Loading