mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-22 05:09:05 +00:00
server: implement warmup event for llm and transcription
This commit is contained in:
@@ -16,6 +16,7 @@ from reflector.processors.audio_transcript_auto import AudioTranscriptAutoProces
|
||||
from reflector.processors.types import AudioFile, Transcript, Word
|
||||
from reflector.settings import settings
|
||||
from reflector.utils.retry import retry
|
||||
from time import monotonic
|
||||
import httpx
|
||||
|
||||
|
||||
@@ -23,24 +24,37 @@ class AudioTranscriptModalProcessor(AudioTranscriptProcessor):
|
||||
def __init__(self, modal_api_key: str):
|
||||
super().__init__()
|
||||
self.transcript_url = settings.TRANSCRIPT_URL + "/transcribe"
|
||||
self.warmup_url = settings.TRANSCRIPT_URL + "/warmup"
|
||||
self.timeout = settings.TRANSCRIPT_TIMEOUT
|
||||
self.headers = {
|
||||
"Authorization": f"Bearer {modal_api_key}",
|
||||
}
|
||||
|
||||
async def _warmup(self):
|
||||
try:
|
||||
async with httpx.AsyncClient() as client:
|
||||
start = monotonic()
|
||||
self.logger.debug("Transcribe modal: warming up...")
|
||||
response = await client.post(
|
||||
self.warmup_url,
|
||||
headers=self.headers,
|
||||
timeout=self.timeout,
|
||||
)
|
||||
response.raise_for_status()
|
||||
duration = monotonic() - start
|
||||
self.logger.debug(f"Transcribe modal: warmup took {duration:.2f}s")
|
||||
except Exception:
|
||||
self.logger.exception("Transcribe modal: warmup failed")
|
||||
|
||||
async def _transcript(self, data: AudioFile):
|
||||
async with httpx.AsyncClient() as client:
|
||||
print(f"Try to transcribe audio {data.path.name}")
|
||||
files = {
|
||||
"file": (data.path.name, data.path.open("rb")),
|
||||
}
|
||||
form = {
|
||||
"timestamp": float(round(data.timestamp, 2)),
|
||||
}
|
||||
response = await retry(client.post)(
|
||||
self.transcript_url,
|
||||
files=files,
|
||||
data=form,
|
||||
timeout=self.timeout,
|
||||
headers=self.headers,
|
||||
)
|
||||
@@ -51,10 +65,15 @@ class AudioTranscriptModalProcessor(AudioTranscriptProcessor):
|
||||
transcript = Transcript(
|
||||
text=result["text"],
|
||||
words=[
|
||||
Word(text=word["text"], start=word["start"], end=word["end"])
|
||||
Word(
|
||||
text=word["text"],
|
||||
start=word["start"],
|
||||
end=word["end"],
|
||||
)
|
||||
for word in result["words"]
|
||||
],
|
||||
)
|
||||
transcript.add_offset(data.timestamp)
|
||||
|
||||
return transcript
|
||||
|
||||
|
||||
Reference in New Issue
Block a user