74 lines
2.2 KiB
Python
74 lines
2.2 KiB
Python
import os
|
|
import tempfile
|
|
|
|
from api.state import state, Status
|
|
from config import load as load_config
|
|
from transcription import engine as transcription_engine
|
|
from llm import OllamaClient
|
|
from output import save_transcript
|
|
from api.router import broadcast
|
|
|
|
|
|
async def run_pipeline():
|
|
cfg = load_config()
|
|
recorder = getattr(state, "_recorder", None)
|
|
if recorder is None:
|
|
return
|
|
|
|
output_dir = getattr(state, "_recording_output_dir", cfg["output"]["path"])
|
|
instructions = getattr(state, "_recording_instructions", "")
|
|
|
|
recorder.stop()
|
|
await state.set_status(Status.PROCESSING)
|
|
await broadcast({"event": "processing"})
|
|
|
|
wav_path = None
|
|
try:
|
|
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
|
|
wav_path = f.name
|
|
recorder.save_wav(wav_path)
|
|
|
|
raw_text = await transcription_engine.transcribe_file(
|
|
wav_path,
|
|
language=cfg["whisper"]["language"],
|
|
model_name=cfg["whisper"]["model"],
|
|
device=cfg["whisper"]["device"],
|
|
)
|
|
await broadcast({"event": "transcribed", "raw": raw_text})
|
|
|
|
client = OllamaClient(base_url=cfg["ollama"]["base_url"])
|
|
refined = await client.refine(
|
|
raw_text=raw_text,
|
|
instructions=instructions,
|
|
model=cfg["ollama"]["model"],
|
|
)
|
|
await broadcast({"event": "refined", "markdown": refined})
|
|
|
|
title = "Diktat"
|
|
for line in refined.splitlines():
|
|
if line.startswith("# "):
|
|
title = line[2:].strip()
|
|
break
|
|
|
|
path = save_transcript(
|
|
title=title,
|
|
content=refined,
|
|
output_dir=output_dir,
|
|
)
|
|
await broadcast({"event": "saved", "path": path, "title": title})
|
|
await state.set_status(Status.IDLE)
|
|
|
|
except Exception as e:
|
|
state.last_error = str(e)
|
|
await state.set_status(Status.ERROR)
|
|
await broadcast({"event": "error", "message": str(e)})
|
|
finally:
|
|
state.recording_user = None
|
|
state._recording_output_dir = None
|
|
state._recording_instructions = ""
|
|
if wav_path:
|
|
try:
|
|
os.unlink(wav_path)
|
|
except OSError:
|
|
pass
|