server: implement warmup event for llm and transcription

This commit is contained in:
Mathieu Virbel
2023-08-11 15:32:41 +02:00
parent a2518df3bd
commit 38a5ee0da2
8 changed files with 85 additions and 5 deletions

View File

@@ -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