Skip to content

Commit

Permalink
ready for docker dev deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
ilkersigirci committed Jan 1, 2025
1 parent 8b2f716 commit 53e71bd
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 25 deletions.
4 changes: 3 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ htmlcov/
.venv
venv

deployment
docs
notebooks
tests
deployment
*.sqlite
28 changes: 27 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ services:
networks:
- t2_proxy
- podflix-network
command: ["tail", "-f", "/dev/null"] # NOTE: For testing the container
# command: ["tail", "-f", "/dev/null"] # NOTE: For testing the container
restart: "no"
develop:
watch:
Expand All @@ -99,6 +99,19 @@ services:
# Rebuild the image on changes to the `pyproject.toml`
- action: rebuild
path: ./pyproject.toml
env_file:
- .env
environment:
- LIBRARY_BASE_PATH=/app
- CHAINLIT_APP_ROOT=/app/configs/chainlit
labels:
- "traefik.enable=true"
## HTTP Routers
- "traefik.http.routers.podflix-rtr.entrypoints=https"
- "traefik.http.routers.podflix-rtr.rule=Host(`podflix.$DOMAIN_NAME`)"
## HTTP Services
- "traefik.http.routers.podflix-rtr.service=podflix-svc"
- "traefik.http.services.podflix-svc.loadbalancer.server.port=5000"

podflix-prod:
image: podflix-prod:latest
Expand All @@ -112,6 +125,19 @@ services:
- podflix-network
command: ["tail", "-f", "/dev/null"] # NOTE: For testing the container
restart: "no"
env_file:
- .env
environment:
- LIBRARY_BASE_PATH=/app
- CHAINLIT_APP_ROOT=/app/configs/chainlit
labels:
- "traefik.enable=true"
## HTTP Routers
- "traefik.http.routers.podflix-rtr.entrypoints=https"
- "traefik.http.routers.podflix-rtr.rule=Host(`podflix.$DOMAIN_NAME`)"
## HTTP Services
- "traefik.http.routers.podflix-rtr.service=podflix-svc"
- "traefik.http.services.podflix-svc.loadbalancer.server.port=5000"

############ LANGFUSE #############
langfuse-db:
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ RUN \

RUN chmod +x /app/docker/entrypoint.sh
ENTRYPOINT ["/app/docker/entrypoint.sh"]

CMD ["uv", "run", "chainlit", "run", "src/podflix/gui/audio.py", "--host", "0.0.0.0", "--port", "5000", "--headless"]

#
# Stage: build
Expand Down
2 changes: 2 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/sh

uv run src/podflix/db/init_db.py

# Start the main application
exec "$@"
42 changes: 32 additions & 10 deletions src/podflix/db/init_db.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Initialize the database schema using SQL statements from init_db.sql file."""

import sqlite3
from pathlib import Path

import sqlalchemy as sa
Expand All @@ -26,24 +27,45 @@ def read_sql_file(file_path: str | Path) -> list[str]:
return [x.strip() for x in f.read().split(";") if x.strip()]


def table_exists(conn) -> bool:
"""Check if any tables exist in the database.
Args:
conn: SQLAlchemy connection object
Returns:
bool: True if any tables exist, False otherwise
"""
try:
# Query to check for existing tables in the public schema
query = """
SELECT COUNT(*)
FROM users
"""
result = conn.execute(sa.text(query)).scalar()
return result > 0
except Exception as e:
logger.error(f"Error checking for tables: {e}")
return False


def initialize_db():
"""Initialize the database using SQL statements from init_db.sql file.
Examples:
>>> initialize_db() # Creates/updates database schema
The function reads SQL statements from the init_db.sql file in the same
directory and executes them sequentially using a database connection.
The function only initializes the database if no tables exist.
"""
logger.info("Initializing the database...")
engine = sa.create_engine(DBInterfaceFactory.create().sync_connection())

raw_sql_statements = read_sql_file(Path(__file__).parent / "init_db.sql")
with engine.connect() as conn:
if table_exists(conn) is True:
logger.info("Database already initialized, skipping...")
return

with sa.create_engine(
DBInterfaceFactory.create().sync_connection()
).connect() as conn:
logger.info("Initializing the database...")
raw_sql_statements = read_sql_file(Path(__file__).parent / "init_db.sql")
for stmt in raw_sql_statements:
conn.execute(sa.text(stmt))
conn.commit()


if __name__ == "__main__":
Expand Down
40 changes: 28 additions & 12 deletions src/podflix/gui/audio.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import webbrowser
import webbrowser # noqa: F401
from dataclasses import dataclass # noqa: F401
from pathlib import Path
from uuid import uuid4
Expand Down Expand Up @@ -75,12 +75,17 @@ def auth_callback(username: str, password: str):
return None


@cl.action_callback("Detailed Traces")
async def on_action(action: cl.Action):
webbrowser.open(action.value, new=0)
# @cl.action_callback("Detailed Traces")
# async def on_action(action: cl.Action):
# # FIXME: Doesn't work inside the docker container
# webbrowser.open(action.value, new=0)

# Optionally remove the action button from the chatbot user interface
# await action.remove()
# # await cl.Message(
# # content=f"Here's your link: [Open Trace]({action.value})", author="System"
# # ).send()

# # Optionally remove the action button
# # await action.remove()


@cl.on_chat_start
Expand Down Expand Up @@ -201,15 +206,26 @@ async def on_message(msg: cl.Message):

logger.debug(f"Langfuse Run ID: {run_id}")

actions = [
cl.Action(
lf_traces_url = get_lf_traces_url(langchain_run_id=run_id)

# actions = [
# cl.Action(
# name="Detailed Traces",
# value=lf_traces_url,
# description="Detailed Logs in Langfuse",
# )
# ]

# assistant_message.actions.extend(actions)

elements = [
cl.Text(
name="Detailed Traces",
value=get_lf_traces_url(langchain_run_id=run_id),
description="Detailed Logs in Langfuse",
content=f"[Detailed Logs]({lf_traces_url})",
display="inline",
)
]

assistant_message.actions.extend(actions)
assistant_message.elements.extend(elements)

await assistant_message.update()
message_history.add_ai_message(assistant_message.content)

0 comments on commit 53e71bd

Please sign in to comment.