Add structured logging for Docker; support LOG_LEVEL env and --log-level
This commit is contained in:
@@ -19,10 +19,13 @@ Environment Variables:
|
||||
import os
|
||||
import io
|
||||
import json
|
||||
import logging
|
||||
from typing import Dict, List, Any, Optional
|
||||
|
||||
import httpx
|
||||
|
||||
logger = logging.getLogger("scraibe.localai_client")
|
||||
|
||||
|
||||
class LocalAIError(Exception):
|
||||
"""Raised when the LocalAI API returns an error or unexpected response."""
|
||||
@@ -67,6 +70,12 @@ class LocalAIClient:
|
||||
"Provide the LocalAI server URL via environment or constructor."
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"Initializing LocalAIClient: url=%s model=%s",
|
||||
self.api_url,
|
||||
self.model,
|
||||
)
|
||||
|
||||
self._client = httpx.Client(
|
||||
base_url=self.api_url,
|
||||
timeout=self.timeout,
|
||||
@@ -130,7 +139,8 @@ class LocalAIClient:
|
||||
if verbose:
|
||||
print("Starting diarization and transcription via LocalAI.")
|
||||
|
||||
# Defaults: use verbose_json + include_text to get both diarization and transcription.
|
||||
logger.info("diarize_and_transcribe requested for: %s", audio_path)
|
||||
|
||||
if response_format is None:
|
||||
response_format = "verbose_json"
|
||||
if include_text is None:
|
||||
@@ -158,6 +168,8 @@ class LocalAIClient:
|
||||
if min_duration_off is not None:
|
||||
data["min_duration_off"] = str(min_duration_off)
|
||||
|
||||
logger.debug("LocalAI request params: %s", data)
|
||||
|
||||
# Open file
|
||||
if not os.path.exists(audio_path):
|
||||
raise LocalAIError(f"Audio file not found: {audio_path}")
|
||||
@@ -172,6 +184,7 @@ class LocalAIClient:
|
||||
headers["Authorization"] = f"Bearer {self.api_key}"
|
||||
|
||||
# POST /v1/audio/diarization
|
||||
logger.info("Sending request to LocalAI: /v1/audio/diarization")
|
||||
resp = self._client.post(
|
||||
"/v1/audio/diarization",
|
||||
data=data,
|
||||
@@ -179,8 +192,11 @@ class LocalAIClient:
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
logger.info("LocalAI response status: %d", resp.status_code)
|
||||
|
||||
if resp.status_code >= 400:
|
||||
body = resp.text
|
||||
logger.error("LocalAI error response: %s", body)
|
||||
raise LocalAIError(
|
||||
f"LocalAI request failed with status {resp.status_code}: {body}"
|
||||
)
|
||||
@@ -188,6 +204,7 @@ class LocalAIClient:
|
||||
try:
|
||||
result = resp.json()
|
||||
except json.JSONDecodeError:
|
||||
logger.error("Failed to parse LocalAI response as JSON.")
|
||||
raise LocalAIError(
|
||||
"Failed to parse LocalAI response as JSON."
|
||||
)
|
||||
@@ -209,7 +226,7 @@ class LocalAIClient:
|
||||
segments = result.get("segments", [])
|
||||
|
||||
if not segments:
|
||||
# If no segments, return empty but valid structure
|
||||
logger.warning("LocalAI returned no segments.")
|
||||
return {
|
||||
"segments": [],
|
||||
"speakers": [],
|
||||
@@ -230,6 +247,11 @@ class LocalAIClient:
|
||||
out_speakers.append(speaker)
|
||||
out_transcripts.append(text)
|
||||
|
||||
logger.info(
|
||||
"Parsed %d segments from LocalAI.",
|
||||
len(out_segments),
|
||||
)
|
||||
|
||||
return {
|
||||
"segments": out_segments,
|
||||
"speakers": out_speakers,
|
||||
|
||||
Reference in New Issue
Block a user