uprava na errors a try catch
This commit is contained in:
+49
-12
@@ -6,7 +6,10 @@ co nejlepšího přepisu.
|
|||||||
|
|
||||||
Audio přichází jako proměnná (upload `file` ve form-data). Všechny ostatní parametry
|
Audio přichází jako proměnná (upload `file` ve form-data). Všechny ostatní parametry
|
||||||
jsou v POST těle (form fields). API klíče jsou v X- hlavičkách (viz credentials.py).
|
jsou v POST těle (form fields). API klíče jsou v X- hlavičkách (viz credentials.py).
|
||||||
Výstup je vždy JSON: {"gpt": ..., "deepgram": ..., "merge": ...}.
|
Výstup je vždy JSON: {"gpt": ..., "deepgram": ..., "merge": ..., "errors": [...]}.
|
||||||
|
|
||||||
|
Selhání jednoho z přepisů (Deepgram / OpenAI) nebo sloučení neshodí celý request —
|
||||||
|
jeho část zůstane prázdná a chyba se přidá do pole `errors`.
|
||||||
"""
|
"""
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
@@ -41,6 +44,11 @@ class TranscribeResult(BaseModel):
|
|||||||
...,
|
...,
|
||||||
description="Sloučený, co nejpřesnější a úplný přepis se štítky mluvčích.",
|
description="Sloučený, co nejpřesnější a úplný přepis se štítky mluvčích.",
|
||||||
)
|
)
|
||||||
|
errors: list[str] = Field(
|
||||||
|
default_factory=list,
|
||||||
|
description="Chyby dílčích kroků (Deepgram / OpenAI přepis / sloučení). "
|
||||||
|
"Prázdné, pokud vše proběhlo v pořádku. Selhavší část je pak prázdná.",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def _read_upload(file: UploadFile) -> tuple[bytes, str, str]:
|
async def _read_upload(file: UploadFile) -> tuple[bytes, str, str]:
|
||||||
@@ -85,8 +93,10 @@ async def transcribe(
|
|||||||
|
|
||||||
audio, filename, content_type = await _read_upload(file)
|
audio, filename, content_type = await _read_upload(file)
|
||||||
|
|
||||||
# Oba přepisy paralelně.
|
errors: list[str] = []
|
||||||
text_deepgram, text_gpt = await asyncio.gather(
|
|
||||||
|
# Oba přepisy paralelně; selhání jednoho neshodí druhý ani celý request.
|
||||||
|
result_deepgram, result_gpt = await asyncio.gather(
|
||||||
deepgram_client.transcribe(
|
deepgram_client.transcribe(
|
||||||
api_key=deepgram_key,
|
api_key=deepgram_key,
|
||||||
audio=audio,
|
audio=audio,
|
||||||
@@ -104,15 +114,42 @@ async def transcribe(
|
|||||||
model=whisper_model,
|
model=whisper_model,
|
||||||
language=language,
|
language=language,
|
||||||
),
|
),
|
||||||
|
return_exceptions=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
prompt = combine_prompt.strip() if combine_prompt and combine_prompt.strip() else DEFAULT_COMBINE_PROMPT
|
if isinstance(result_deepgram, BaseException):
|
||||||
merged = await openai_client.merge_transcripts(
|
log.exception("Deepgram přepis selhal", exc_info=result_deepgram)
|
||||||
api_key=openai_key,
|
errors.append(f"Deepgram přepis selhal: {result_deepgram}")
|
||||||
model=chat_model,
|
text_deepgram = ""
|
||||||
system_prompt=prompt,
|
else:
|
||||||
text_deepgram=text_deepgram,
|
text_deepgram = result_deepgram
|
||||||
text_whisper=text_gpt,
|
|
||||||
)
|
|
||||||
|
|
||||||
return TranscribeResult(gpt=text_gpt, deepgram=text_deepgram, merge=merged)
|
if isinstance(result_gpt, BaseException):
|
||||||
|
log.exception("OpenAI (whisper) přepis selhal", exc_info=result_gpt)
|
||||||
|
errors.append(f"OpenAI (whisper) přepis selhal: {result_gpt}")
|
||||||
|
text_gpt = ""
|
||||||
|
else:
|
||||||
|
text_gpt = result_gpt
|
||||||
|
|
||||||
|
# Sloučení má smysl jen když je aspoň jeden přepis k dispozici.
|
||||||
|
merged = ""
|
||||||
|
if text_deepgram or text_gpt:
|
||||||
|
prompt = combine_prompt.strip() if combine_prompt and combine_prompt.strip() else DEFAULT_COMBINE_PROMPT
|
||||||
|
try:
|
||||||
|
merged = await openai_client.merge_transcripts(
|
||||||
|
api_key=openai_key,
|
||||||
|
model=chat_model,
|
||||||
|
system_prompt=prompt,
|
||||||
|
text_deepgram=text_deepgram,
|
||||||
|
text_whisper=text_gpt,
|
||||||
|
)
|
||||||
|
except Exception as exc:
|
||||||
|
log.exception("Sloučení přepisů selhalo")
|
||||||
|
errors.append(f"Sloučení přepisů selhalo: {exc}")
|
||||||
|
|
||||||
|
return TranscribeResult(
|
||||||
|
gpt=text_gpt,
|
||||||
|
deepgram=text_deepgram,
|
||||||
|
merge=merged,
|
||||||
|
errors=errors,
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user