Skip to content

Commit

Permalink
issue #221: secrets.env & config.env
Browse files Browse the repository at this point in the history
  • Loading branch information
k-allagbe committed Dec 10, 2024
1 parent 2fa132b commit 30f3957
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 79 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
secrets.env
26 changes: 0 additions & 26 deletions .env.template

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ celerybeat.pid

# Environments
.env
secrets.env
.venv
env/
venv/
Expand Down
24 changes: 0 additions & 24 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,6 @@ COPY . .
RUN pip install --no-cache-dir -r requirements.txt
RUN opentelemetry-bootstrap --action=install

ARG ARG_AZURE_API_ENDPOINT
ARG ARG_AZURE_API_KEY
ARG ARG_AZURE_OPENAI_DEPLOYMENT
ARG ARG_AZURE_OPENAI_ENDPOINT
ARG ARG_AZURE_OPENAI_KEY
ARG ARG_FERTISCAN_STORAGE_URL
ARG ARG_FERTISCAN_DB_URL
ARG ARG_FERTISCAN_SCHEMA
ARG ARG_ALLOWED_ORIGINS
ARG ARG_PROMPT_PATH
ARG ARG_UPLOAD_PATH

ENV AZURE_API_ENDPOINT=${ARG_AZURE_API_ENDPOINT:-your_azure_form_recognizer_endpoint}
ENV AZURE_API_KEY=${ARG_AZURE_API_KEY:-your_azure_form_recognizer_key}
ENV AZURE_OPENAI_DEPLOYMENT=${ARG_AZURE_OPENAI_DEPLOYMENT:-your_azure_openai_deployment}
ENV AZURE_OPENAI_ENDPOINT=${ARG_AZURE_OPENAI_ENDPOINT:-your_azure_openai_endpoint}
ENV AZURE_OPENAI_KEY=${ARG_AZURE_OPENAI_KEY:-your_azure_openai_key}
ENV FERTISCAN_STORAGE_URL=${ARG_FERTISCAN_STORAGE_URL:-your_fertiscan_storage_url}
ENV FERTISCAN_DB_URL=${ARG_FERTISCAN_DB_URL:-your_fertiscan_db_url}
ENV FERTISCAN_SCHEMA=${ARG_FERTISCAN_SCHEMA:-your_fertiscan_schema}
ENV ALLOWED_ORIGINS=${ARG_ALLOWED_ORIGINS:-["http://url.to_frontend/"]}
ENV PROMPT_PATH=${ARG_PROMPT_PATH:-path/to/file}
ENV UPLOAD_PATH=${ARG_UPLOAD_PATH:-path/to/file}

EXPOSE 5000

RUN chown -R 1000:1000 /app
Expand Down
24 changes: 6 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,20 @@ generation using an [LLM](https://en.wikipedia.org/wiki/Large_language_model).
1. Build the Docker image:

```sh
docker build -t fertiscan-backend \
--build-arg ARG_AZURE_API_ENDPOINT=your_azure_form_recognizer_endpoint \
--build-arg ARG_AZURE_API_KEY=your_azure_form_recognizer_key \
--build-arg ARG_AZURE_OPENAI_DEPLOYMENT=your_azure_openai_deployment \
--build-arg ARG_AZURE_OPENAI_ENDPOINT=your_azure_openai_endpoint \
--build-arg ARG_AZURE_OPENAI_KEY=your_azure_openai_key \
--build-arg ARG_FERTISCAN_STORAGE_URL=your_fertiscan_storage_url \
--build-arg ARG_FERTISCAN_DB_URL=your_fertiscan_db_url \
--build-arg ARG_FERTISCAN_SCHEMA=your_fertiscan_schema \
--build-arg ARG_ALLOWED_ORIGINS=["http://url.to_frontend/"] \
--build-arg OTEL_EXPORTER_OTLP_ENDPOINT=your_phoenix_endpoint \
--build-arg ARG_PROMPT_PATH=path/to/file \
--build-arg ARG_UPLOAD_PATH=path/to/file \
.
docker build -t fertiscan-backend .
```

2. Run the Docker container:

```sh
docker run -p 5000:5000 fertiscan-backend
docker run -p 5000:5000 --env-file secrets.env fertiscan-backend
```

#### Docker Compose

1. Create a `.env` file from [.env.template](./.env.template). Include the
following environment variables:
1. Create a `secrets.env` file from
[secrets.env.template](./secrets.env.template). Include the following
environment variables:

```ini
FERTISCAN_DB_URL=postgresql://postgres:postgres@postgres:5432/fertiscan
Expand Down Expand Up @@ -111,7 +99,7 @@ following details:

### Environment Variables

Create a `.env` file from [.env.template](./.env.template).
Create a `secrets.env` file from [secrets.env.template](./secrets.env.template).

```ini
AZURE_API_ENDPOINT=your_azure_form_recognizer_endpoint
Expand Down
27 changes: 18 additions & 9 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi.logger import logger
from fastapi.middleware.cors import CORSMiddleware
from pipeline import GPT, OCR
from psycopg_pool import ConnectionPool
from pydantic import Field, PostgresDsn
from pydantic_settings import BaseSettings

from opentelemetry import trace
from opentelemetry._logs import set_logger_provider
from opentelemetry.exporter.otlp.proto.grpc._log_exporter import OTLPLogExporter
Expand All @@ -17,10 +13,14 @@
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from pipeline import GPT, OCR
from psycopg_pool import ConnectionPool
from pydantic import Field, PostgresDsn
from pydantic_settings import BaseSettings

from fastapi.logger import logger
load_dotenv("secrets.env")
load_dotenv("config.env")

load_dotenv()

class Settings(BaseSettings):
api_endpoint: str = Field(alias="azure_api_endpoint")
Expand All @@ -38,6 +38,7 @@ class Settings(BaseSettings):
allowed_origins: list[str]
otel_exporter_otlp_endpoint: str = Field(alias="otel_exporter_otlp_endpoint")


@asynccontextmanager
async def lifespan(app: FastAPI):
settings = app.state.settings
Expand All @@ -52,13 +53,21 @@ async def lifespan(app: FastAPI):
tracer_provider = TracerProvider(resource=resource)
trace.set_tracer_provider(tracer_provider)
tracer_provider.add_span_processor(
BatchSpanProcessor(OTLPSpanExporter(endpoint=settings.otel_exporter_otlp_endpoint, insecure=True))
BatchSpanProcessor(
OTLPSpanExporter(
endpoint=settings.otel_exporter_otlp_endpoint, insecure=True
)
)
)
# Logging setup
logger_provider = LoggerProvider(resource=resource)
set_logger_provider(logger_provider)
logger_provider.add_log_record_processor(
BatchLogRecordProcessor(OTLPLogExporter(endpoint=settings.otel_exporter_otlp_endpoint, insecure=True))
BatchLogRecordProcessor(
OTLPLogExporter(
endpoint=settings.otel_exporter_otlp_endpoint, insecure=True
)
)
)
handler = LoggingHandler(logger_provider=logger_provider)
logger.addHandler(handler)
Expand Down
24 changes: 24 additions & 0 deletions config.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Document Intelligence
AZURE_API_ENDPOINT=https://fertiscan-computer-vision-service.cognitiveservices.azure.com/

# OpenAI
AZURE_OPENAI_ENDPOINT=https://fertiscan-openai.openai.azure.com/
AZURE_OPENAI_DEPLOYMENT=gpt-4o

# For CORS purposes
ALLOWED_ORIGINS=["*"]

# API base path for deployment
API_BASE_PATH=/swagger
SWAGGER_PATH=/docs

# DB
FERTISCAN_SCHEMA=fertiscan_0.0.15

# Phoenix API configuration
PHOENIX_ENDPOINT=
OTEL_EXPORTER_OTLP_ENDPOINT=

# Other
UPLOAD_PATH=./uploads
# LOG_FILENAME=fertiscan.log
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ services:
container_name: backend
restart: always
env_file:
- .env
- secrets.env
environment:
- OTEL_EXPORTER_OTLP_ENDPOINT=http://alloy:4317
- OTEL_EXPORTER_OTLP_INSECURE=true
Expand Down Expand Up @@ -76,7 +76,7 @@ services:
- INSTANCE_ID=${BB_INSTANCE_ID}
- DATABASE_ID=${BB_DATABASE_ID}
env_file:
- .env
- secrets.env
ports:
- "5432:5432"
healthcheck:
Expand Down
17 changes: 17 additions & 0 deletions secrets.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Document Intelligence
AZURE_API_KEY=

# OpenAI
AZURE_OPENAI_KEY=

# API base path for deployment
API_BASE_PATH='/swagger'
SWAGGER_PATH='/docs'

# DB
FERTISCAN_DB_URL=

# Blob Storage
FERTISCAN_STORAGE_URL=


0 comments on commit 30f3957

Please sign in to comment.