-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.py
More file actions
149 lines (126 loc) · 5 KB
/
main.py
File metadata and controls
149 lines (126 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import shutil
from pathlib import Path
import redis
from fastapi import FastAPI, Depends, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from sqlalchemy import text
from sqlalchemy.orm import Session
from config import settings as _settings
from database import init_db, seed_default_actions, recover_stale_jobs, cleanup_orphaned_storage, get_db, engine
from models import Meeting
from api import meetings, speakers, segments, export, websocket, live_websocket, actions, model_settings, encryption, search, speaker_profiles, vocabulary, insights, protocol, analytics
app = FastAPI(title="Transcriber")
_default_origins = ["http://localhost:5174", "http://localhost:5175", "http://127.0.0.1:5174", "http://127.0.0.1:5175"]
_cors_origins = [x.strip() for x in _settings.cors_origins.split(",") if x.strip()] if _settings.cors_origins else _default_origins
app.add_middleware(
CORSMiddleware,
allow_origins=_cors_origins,
allow_credentials=True,
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allow_headers=["*"],
)
app.include_router(meetings.router)
app.include_router(speakers.router)
app.include_router(segments.router)
app.include_router(export.router)
app.include_router(websocket.router)
app.include_router(live_websocket.router)
app.include_router(actions.router)
app.include_router(model_settings.router)
app.include_router(encryption.router)
app.include_router(search.router)
app.include_router(speaker_profiles.router)
app.include_router(vocabulary.router)
app.include_router(insights.router)
app.include_router(protocol.router)
app.include_router(analytics.router)
@app.on_event("startup")
def startup():
init_db()
seed_default_actions()
recover_stale_jobs()
cleanup_orphaned_storage()
import logging
_log = logging.getLogger(__name__)
if not _settings.hf_auth_token or _settings.hf_auth_token == "hf_your_token_here":
_log.warning("HF_AUTH_TOKEN not set — speaker diarization will fail. "
"Set it in .env (get one at https://huggingface.co/settings/tokens)")
@app.get("/api/meetings/{meeting_id}/audio")
def stream_audio(meeting_id: str, db: Session = Depends(get_db)):
meeting = db.query(Meeting).filter(Meeting.id == meeting_id).first()
if not meeting or not meeting.audio_filepath:
raise HTTPException(404, "Audio not found")
path = Path(meeting.audio_filepath)
if not path.exists():
raise HTTPException(404, "Audio file not found")
return FileResponse(
path,
media_type="audio/wav",
headers={"Accept-Ranges": "bytes"},
)
@app.get("/api/health")
def health():
checks = {}
# Database
try:
with engine.connect() as conn:
conn.execute(text("SELECT 1"))
checks["database"] = "ok"
except Exception as e:
checks["database"] = f"error: {e}"
# Redis
try:
r = redis.Redis.from_url(_settings.redis_url)
r.ping()
r.close()
checks["redis"] = "ok"
except Exception as e:
checks["redis"] = f"error: {e}"
# Whisper CLI
whisper_path = Path(_settings.whisper_cli_path)
checks["whisper_cli"] = "ok" if whisper_path.exists() else f"missing: {whisper_path}"
# Disk space
storage_path = Path(_settings.storage_path)
storage_path.mkdir(parents=True, exist_ok=True)
disk = shutil.disk_usage(storage_path)
free_gb = disk.free / (1024 ** 3)
checks["disk_free_gb"] = round(free_gb, 1)
if free_gb < 1:
checks["disk"] = "warning: less than 1 GB free"
all_ok = all(
v == "ok" for k, v in checks.items()
if k not in ("disk_free_gb",)
)
return {"status": "ok" if all_ok else "degraded", **checks}
@app.get("/api/settings")
def get_settings():
from preferences import get_public_preferences
prefs = get_public_preferences()
return {
"llm_provider": _settings.llm_provider,
"openrouter_model": _settings.openrouter_model,
"ollama_model": _settings.ollama_model,
"ollama_base_url": _settings.ollama_base_url,
"preferences": prefs,
}
@app.put("/api/settings/preferences")
def update_preferences(body: dict):
from preferences import save_preferences, load_preferences, get_public_preferences
current = load_preferences()
# Only update known fields
if "default_vocabulary" in body:
current["default_vocabulary"] = (body["default_vocabulary"] or "").strip()[:2000]
if "speaker_profiles_enabled" in body:
current["speaker_profiles_enabled"] = bool(body["speaker_profiles_enabled"])
if "hf_auth_token" in body:
val = (body["hf_auth_token"] or "").strip()
# Don't overwrite with the masked value
if val and "*" not in val:
current["hf_auth_token"] = val
if "openrouter_api_key" in body:
val = (body["openrouter_api_key"] or "").strip()
if val and "*" not in val:
current["openrouter_api_key"] = val
save_preferences(current)
return get_public_preferences()