Cleaned up conditional logging for optional video_file

This commit is contained in:
McCloudS
2024-11-17 18:25:44 -07:00
committed by GitHub
parent 5df2187197
commit c6c684cdc8

View File

@@ -328,17 +328,18 @@ def batch(
@app.post("//asr") @app.post("//asr")
@app.post("/asr") @app.post("/asr")
async def asr( async def asr(
task: Union[str, None] = Query(default="transcribe", enum=["transcribe", "translate"]), task: Union[str, None] = Query(default="transcribe", enum=["transcribe", "translate"]),
language: Union[str, None] = Query(default=None), language: Union[str, None] = Query(default=None),
video_file: Union[str, None] = Query(default="Name not supplied"), video_file: Union[str, None] = Query(default=None),
initial_prompt: Union[str, None] = Query(default=None), #not used by Bazarr initial_prompt: Union[str, None] = Query(default=None), # Not used by Bazarr
audio_file: UploadFile = File(...), audio_file: UploadFile = File(...),
encode: bool = Query(default=True, description="Encode audio first through ffmpeg"), #not used by Bazarr/always False encode: bool = Query(default=True, description="Encode audio first through ffmpeg"), # Not used by Bazarr/always False
output: Union[str, None] = Query(default="srt", enum=["txt", "vtt", "srt", "tsv", "json"]), output: Union[str, None] = Query(default="srt", enum=["txt", "vtt", "srt", "tsv", "json"]),
word_timestamps: bool = Query(default=False, description="Word level timestamps") #not used by Bazarr word_timestamps: bool = Query(default=False, description="Word-level timestamps"), # Not used by Bazarr
): ):
try: try:
logging.info(f"Transcribing file '{video_file}' from Bazarr/ASR webhook") logging.info(f"Transcribing file '{video_file}' from Bazarr/ASR webhook" if video_file else "Transcribing file from Bazarr/ASR webhook")
result = None result = None
random_name = ''.join(random.choices("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", k=6)) random_name = ''.join(random.choices("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", k=6))
@@ -349,7 +350,7 @@ async def asr(
start_time = time.time() start_time = time.time()
start_model() start_model()
task_id = { 'path': f"Bazarr-asr-{random_name}" } task_id = {'path': f"Bazarr-asr-{random_name}"}
task_queue.put(task_id) task_queue.put(task_id)
args = {} args = {}
@@ -370,25 +371,34 @@ async def asr(
result = model.transcribe_stable(task=task, language=language, **args) result = model.transcribe_stable(task=task, language=language, **args)
appendLine(result) appendLine(result)
elapsed_time = time.time() - start_time elapsed_time = time.time() - start_time
minutes, seconds = divmod(int(elapsed_time), 60) minutes, seconds = divmod(int(elapsed_time), 60)
logging.info(f"Transcription of '{video_file}' from Bazarr complete, it took {minutes} minutes and {seconds} seconds to complete.") logging.info(
f"Transcription of '{video_file}' from Bazarr complete, it took {minutes} minutes and {seconds} seconds to complete." if video_file
else f"Transcription complete, it took {minutes} minutes and {seconds} seconds to complete.")
except Exception as e: except Exception as e:
logging.info(f"Error processing or transcribing Bazarr file: {video_file} -- Exception: {e}") logging.error(
f"Error processing or transcribing Bazarr file: {video_file} -- Exception: {e}" if video_file
else f"Error processing or transcribing Bazarr file Exception: {e}"
)
finally: finally:
await audio_file.close() await audio_file.close()
task_queue.task_done() task_queue.task_done()
delete_model() delete_model()
if result: if result:
return StreamingResponse( return StreamingResponse(
iter(result.to_srt_vtt(filepath = None, word_level=word_level_highlight)), iter(result.to_srt_vtt(filepath=None, word_level=word_level_highlight)),
media_type="text/plain", media_type="text/plain",
headers={ headers={
'Source': 'Transcribed using stable-ts from Subgen!', 'Source': 'Transcribed using stable-ts from Subgen!',
}) }
)
else: else:
return return
@app.post("//detect-language") @app.post("//detect-language")
@app.post("/detect-language") @app.post("/detect-language")
async def detect_language( async def detect_language(