File size: 6,187 Bytes
a3584e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python3
"""
FastAPI backend for Audio Transcription Pipeline.
Serves on port 8081 with endpoints for transcription, ASCII viz, and health check.
"""

import json
import logging
import os
import shutil
import tempfile
import time
from typing import Optional

import librosa
import uvicorn
from fastapi import FastAPI, File, Form, HTTPException, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, PlainTextResponse

logging.basicConfig(
    level=logging.INFO,
    format="%(levelname)s:%(name)s:%(message)s",
)
logger = logging.getLogger(__name__)

# Quiet down libraries
logging.getLogger("faster_whisper").setLevel(logging.WARNING)
logging.getLogger("transformers").setLevel(logging.WARNING)
logging.getLogger("librosa").setLevel(logging.WARNING)
logging.getLogger("pipeline").setLevel(logging.WARNING)
logging.getLogger("httpx").setLevel(logging.WARNING)

app = FastAPI(
    title="Audio Transcription Pipeline",
    description="Transcribe, classify, diarize, summarize, and visualize audio",
    version="1.0.0",
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Lazy-loaded pipeline
_pipeline = None


def _get_pipeline():
    global _pipeline
    if _pipeline is None:
        logger.info("Initializing AudioPipeline...")
        from pipeline.orchestrator import AudioPipeline

        _pipeline = AudioPipeline(
            enable_summarizer=True,
            enable_ascii=False,  # ASCII on-demand
        )
        logger.info("AudioPipeline initialized.")
    return _pipeline


def _get_ascii_viz():
    """Get AsciiSpectrogram instance (no caching - lightweight)."""
    from pipeline.ascii_spectrogram import AsciiSpectrogram
    return AsciiSpectrogram()


def _clean_for_json(obj):
    """Recursively clean for JSON serialization."""
    if isinstance(obj, dict):
        return {k: _clean_for_json(v) for k, v in obj.items() if not k.startswith("_")}
    elif isinstance(obj, list):
        return [_clean_for_json(item) for item in obj]
    elif isinstance(obj, float):
        if obj != obj:  # NaN
            return None
        return obj
    return obj


@app.get("/health")
async def health_check():
    """Health check endpoint."""
    return {
        "status": "ok",
        "service": "audio-transcription-pipeline",
        "version": "1.0.0",
    }


@app.post("/transcribe")
async def transcribe(
    file: UploadFile = File(...),
    language: Optional[str] = Form(None),
    vad_filter: bool = Form(True),
    include_ascii: bool = Form(False),
    include_summary: bool = Form(True),
):
    """Transcribe an uploaded audio file through the full pipeline."""
    start_time = time.time()

    # Save uploaded file to temp
    suffix = os.path.splitext(file.filename or "audio.wav")[1] or ".wav"
    with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
        shutil.copyfileobj(file.file, tmp)
        tmp_path = tmp.name

    try:
        pipe = _get_pipeline()
        result = pipe.process_file(
            tmp_path,
            language=language,
            vad_filter=vad_filter,
        )

        # Add ASCII if requested
        if include_ascii:
            try:
                audio, sr = librosa.load(tmp_path, sr=16000, mono=True)
                viz = _get_ascii_viz()
                frames = []
                for frame_text, timestamp in viz.generate_frames(audio, sr, fps=10):
                    frames.append({"timestamp": round(timestamp, 2), "frame": frame_text})
                result["ascii_frames"] = frames
            except Exception as e:
                logger.warning(f"ASCII viz failed: {e}")
                result["ascii_frames"] = []

        # Add processing time
        elapsed = time.time() - start_time
        result.setdefault("metadata", {})["api_processing_time_seconds"] = round(elapsed, 2)

        clean = _clean_for_json(result)
        return JSONResponse(content=clean)

    except Exception as e:
        logger.exception(f"Transcription failed: {e}")
        raise HTTPException(status_code=500, detail=str(e))

    finally:
        # Clean up temp file
        try:
            os.unlink(tmp_path)
        except OSError:
            pass


@app.get("/ascii-viz")
async def ascii_viz(
    audio_path: str = Form(...),
    columns: int = 80,
    rows: int = 20,
    fps: int = 10,
    mode: str = "spectrogram",
):
    """Generate ASCII spectrogram frames from an audio file path."""
    if not os.path.exists(audio_path):
        raise HTTPException(status_code=404, detail=f"Audio file not found: {audio_path}")

    try:
        audio, sr = librosa.load(audio_path, sr=16000, mono=True)
        viz = _get_ascii_viz()
        frames = []
        for frame_text, timestamp in viz.generate_frames(audio, sr, fps=fps, mode=mode):
            frames.append({"timestamp": round(timestamp, 2), "frame": frame_text})
        return {"frames": frames, "count": len(frames), "duration": round(len(audio) / sr, 2)}
    except Exception as e:
        logger.exception(f"ASCII viz failed: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.get("/ascii-viz-file")
async def ascii_viz_file(
    audio_path: str,
    columns: int = 80,
    rows: int = 20,
    fps: int = 10,
    mode: str = "spectrogram",
):
    """Generate ASCII spectrogram and return as plain text."""
    if not os.path.exists(audio_path):
        raise HTTPException(status_code=404, detail=f"Audio file not found: {audio_path}")

    try:
        audio, sr = librosa.load(audio_path, sr=16000, mono=True)
        viz = _get_ascii_viz()
        lines = []
        for frame_text, timestamp in viz.generate_frames(audio, sr, fps=fps, mode=mode):
            lines.append(f"--- Frame @ {timestamp:.1f}s ---")
            lines.append(frame_text)
        return PlainTextResponse("\n".join(lines))
    except Exception as e:
        logger.exception(f"ASCII viz failed: {e}")
        raise HTTPException(status_code=500, detail=str(e))


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8081, log_level="info")