-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess_backend.py
More file actions
86 lines (70 loc) · 2.32 KB
/
Copy pathpreprocess_backend.py
File metadata and controls
86 lines (70 loc) · 2.32 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Transcript preprocessing backend seam (cloud API vs on-device stub)."""
from __future__ import annotations
import logging
from abc import ABC, abstractmethod
from typing import Any, Optional
from runtime_resources import device_compute_route
logger = logging.getLogger(__name__)
class TranscriptPreprocessor(ABC):
@abstractmethod
def minimize(
self,
role: str,
query: str,
transcription: str,
*,
model: str,
token_limit: Optional[int] = None,
skip_merge_reminimize: bool = False,
) -> str:
...
class CloudApiTranscriptPreprocessor(TranscriptPreprocessor):
"""Existing OpenAI GPT Nano path via token_minimizer_chunked."""
def __init__(self, client: Any):
self.client = client
def minimize(
self,
role: str,
query: str,
transcription: str,
*,
model: str,
token_limit: Optional[int] = None,
skip_merge_reminimize: bool = False,
) -> str:
import stt_function_v3 as stt
return stt.token_minimizer_chunked(
role,
query,
transcription,
self.client,
model=model,
token_limit=token_limit,
skip_merge_reminimize=skip_merge_reminimize,
)
class OnDeviceTranscriptPreprocessor(TranscriptPreprocessor):
"""Reserved stub — not enabled in v4.2."""
def minimize(
self,
role: str,
query: str,
transcription: str,
*,
model: str,
token_limit: Optional[int] = None,
skip_merge_reminimize: bool = False,
) -> str:
with device_compute_route(label="on_device_preprocess"):
raise NotImplementedError(
"PREPROCESS_BACKEND=on_device is reserved but not implemented in v4.2. "
"Use PREPROCESS_BACKEND=cloud_api."
)
def create_transcript_preprocessor(backend: str, client: Any) -> TranscriptPreprocessor:
b = (backend or "cloud_api").strip().lower()
if b in ("cloud_api", "cloud", "openai"):
return CloudApiTranscriptPreprocessor(client)
if b in ("on_device", "on-device", "local"):
return OnDeviceTranscriptPreprocessor()
raise ValueError(f"Unknown PREPROCESS_BACKEND: {backend}")