60 lines
1.7 KiB
Python
60 lines
1.7 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
|
|
|
|
db_path = app.config.get("ALT_TEXT_DB")
|
|
if not db_path:
|
|
db_path = str(Path(app.instance_path) / "alt_text_cache.db")
|
|
app.config["ALT_TEXT_DB"] = db_path
|
|
|
|
Path(app.instance_path).mkdir(parents=True, exist_ok=True)
|
|
|
|
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
|