station-announcer/station_announcer/__init__.py

61 lines
1.8 KiB
Python

"""Package initialization for the alt text generator app."""
from pathlib import Path
from flask import Flask
from .cache import AltTextCache
from .config import load_settings
from .immich import ImmichClient
from .openai_client import AltTextGenerator
from .mastodon import MastodonClient
def create_app() -> Flask:
"""Create and configure the Flask application."""
package_path = Path(__file__).resolve().parent
project_root = package_path.parent
app = Flask(
__name__,
template_folder=str(project_root / "templates"),
static_folder=str(project_root / "static"),
)
settings = load_settings()
app.config.update(settings)
secret_key = app.config.get("SECRET_KEY") or "dev-secret-key"
app.config["SECRET_KEY"] = secret_key
instance_dir = Path(app.instance_path)
instance_dir.mkdir(parents=True, exist_ok=True)
db_path = app.config.get("STATION_DB")
if not db_path:
db_path = str(instance_dir / "station_announcer.db")
app.config["STATION_DB"] = db_path
app.immich_client = ImmichClient(
base_url=app.config["IMMICH_API_URL"],
api_key=app.config["IMMICH_API_KEY"],
)
app.alt_text_cache = AltTextCache(db_path)
app.alt_text_generator = AltTextGenerator(
api_key=app.config["OPENAI_API_KEY"],
model=app.config.get("OPENAI_MODEL", "gpt-4o-mini"),
)
mastodon_token = app.config.get("MASTODON_ACCESS_TOKEN")
if mastodon_token:
app.mastodon_client = MastodonClient(
base_url=app.config.get("MASTODON_BASE_URL", "http://localhost:3000"),
access_token=mastodon_token,
)
else:
app.mastodon_client = None
from . import routes # pragma: no cover
app.register_blueprint(routes.bp)
return app