"""Shared UI helper utilities used by both Streamlit frontends. This module intentionally contains pure helpers so unit tests can run without a live Streamlit server or network dependencies. """ from typing import Dict, Any def detect_audio_media_type(data: bytes) -> str: """Lightweight magic-byte detection for common audio formats.""" if not data: return "application/octet-stream" # WAV: RIFF....WAVE if data[:4] == b"RIFF" and b"WAVE" in data[:12]: return "audio/wav" # OGG: OggS if data[:4] == b"OggS": return "audio/ogg" # MP3: ID3 tag or 0xFF 0xFB frame header if data[:3] == b"ID3" or data[:2] == bytes([0xFF, 0xFB]): return "audio/mpeg" return "application/octet-stream" def ext_for_media_type(media_type: str) -> str: mapping = { "audio/wav": ".wav", "audio/ogg": ".ogg", "audio/mpeg": ".mp3", "audio/mp4": ".m4a", } return mapping.get(media_type, "") def infer_download_filename(request_id: str, media_type: str) -> str: """Return a sensible download filename for an audio artifact.""" ext = ext_for_media_type(media_type) or "" return f"{request_id}{ext}" def format_result_for_display(result: Dict[str, Any]) -> Dict[str, Any]: """Normalize a pipeline result dict for the UI renderer. This function is purposely conservative: it doesn't change content, only ensures expected keys exist so the UI can render reliably. """ out = {} out["summary"] = result.get("summary") or "" out["translated_summary"] = result.get("translated_summary") or "" out["transcript"] = result.get("transcript") or "" out["word_count"] = result.get("word_count") or 0 out["reading_time_seconds"] = result.get("reading_time_seconds") or 0 out["target_language"] = result.get("target_language") or "" out["request_id"] = result.get("request_id") out["audio_b64"] = result.get("audio_b64") return out __all__ = [ "detect_audio_media_type", "ext_for_media_type", "infer_download_filename", "format_result_for_display", ]