Files
audio2text/app/database.py
zikai 00e2a95fb7 Initial commit: audio2text 双语字幕生成服务
- 音频/视频转双语(英/中)SRT 字幕,Docker 容器化,CPU 开发/GPU 生产同一份代码
- faster-whisper ASR(词级时间戳) + 断句时间戳重算 + NLLB 翻译(模型不共驻)
- 分片上传(断点续传) + SQLite 持久化 + 主页/历史/日志页面
- 历史页文件名搜索;缓存定时清理(默认保留7天,可配置)
- 双 Dockerfile(cpu/gpu) + setup/start/stop 脚本
2026-07-06 06:54:19 +00:00

69 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""SQLite 引擎、Session、Base、get_db 依赖。
自包含:无需外部 MySQL容器内单文件 SQLite 即可。对齐 server/database.py 的接口形态,
但用 SQLite本项目独立运行、无并发写入压力
"""
from __future__ import annotations
from pathlib import Path
from typing import Generator
from sqlalchemy import create_engine
from sqlalchemy.orm import DeclarativeBase, Session, sessionmaker
from .config import get_settings
_engine = None
_SessionLocal = None
def _db_path() -> Path:
"""SQLite 文件落在 work_dir 下,跟数据一起走 volume。"""
s = get_settings()
p = s.work_dir() / "audio2text.db"
p.parent.mkdir(parents=True, exist_ok=True)
return p
def get_engine():
global _engine, _SessionLocal
if _engine is None:
url = f"sqlite:///{_db_path()}"
_engine = create_engine(
url,
# 后台 reaper / 缓存清理线程与请求线程并发写同一库busy_timeout 让等待方
# 在拿锁时阻塞 5s 而非立即报 database is locked。
connect_args={"check_same_thread": False, "timeout": 30},
future=True,
)
_SessionLocal = sessionmaker(bind=_engine, autoflush=False, expire_on_commit=False)
return _engine
def get_session_local():
get_engine()
return _SessionLocal
class Base(DeclarativeBase):
pass
def init_db_schema() -> None:
"""建表(幂等)。"""
from .models.task import Task # noqa: F401
from .models.upload_session import UploadSession # noqa: F401
get_engine()
Base.metadata.create_all(get_engine())
def get_db() -> Generator[Session, None, None]:
"""FastAPI 依赖:每请求一个 Session结束自动关闭。"""
db = get_session_local()()
try:
yield db
finally:
db.close()