
    liQ                     p   d Z ddlZddlZddlZddlZddlmZmZmZm	Z	 ddl
mZ ddlmZmZmZmZmZmZ ddlmZ ddd	d
dede	e   dededeg ef   dede	ej0                     dedeeef   fdZdedede	e   deegdf   deeeeef   gdf   deeegdf   dededeg ef   dedej0                  ddfdZy)u3   异步转写任务与可复用转写执行逻辑。    N)AnyCallableDictOptional)AudioSegment   )SessionChunkQueuefilter_time_stampsmerge_adjacent_same_speakerpreload_persisted_speakersprocess_chunk_pipelinesplit_file_to_segments)SpeakerRegistryFz/transcribe)detailed_log_enabledloggerlog_tagtmp_pathmanual_speaker_idschunk_secondsspeaker_devicebuild_speaker_registryr   r   r   returnc                    |xs t        j                  t              } |       }t        ||       t	        j
                         }	t               }
	 t        | |      }|D ]  }|
j                  |        d}d}g }g }t        |      }d}|
j                         s(|
j                         }|nt        t        |            dz  }|r|j                  d|||||z          t        |||||      }||j                  d      }|j                  d      xs d	j!                         }|r|j#                  |       |j                  d
      xs g }	 t%        |      }|r|j)                  |       t        |j                  d|      xs |      }|r|j                  d|||||z   |       ||z  }|dz  }|
j                         s(t+        |      }|dj-                  |      j!                         t/        t	        j
                         |	z
  d      ||d	 |j1                          S # t&        $ r Y w xY w# t&        $ r Y S w xY w# 	 |j1                          w # t&        $ r Y w w xY wxY w)zDRun the queue-based transcribe flow and return the response payload.g        Nr   g     @@z6[detailed][%s] start chunk index=%s range=[%.3f, %.3f])segment
offset_secr   speaker_registryr   languagetext time_stampsduration_seczA[detailed][%s] result chunk index=%s range=[%.3f, %.3f] result=%sr       )r   r   time_secchunksr    )logging	getLogger__name__r   timeperf_counterr	   r   putlenemptytry_getfloatinfor   getstripappendr
   	Exceptionextendr   joinroundclear)r   r   r   r   r   r   r   r   r   
started_atqueuesegmentssegr   r   
text_partsenriched_tschunk_countchunk_indexestimated_duration_secchunk_resulttxtchunk_tschunk_duration_secs                           /app/gzzm/async_transcribe.py run_transcribe_pipeline_for_filerG      s    2w((2F-//1CD""$J->-@EK)(MBCIIcN  

(m++---/C{%*3s8_v%="#L!77 2%+!1-L '++J7##F+1r88:C!!#&#''6<"H-h7 ""8,!&|'7'7H^'_'ycy!z#W!33  ,,J1Kc ++-f 2+> HHZ(..0d//1J>B!&
	""$;  <  			""$ 		so   C=I  H" A'I  AI  H1"	H.+I  -H..I  1	H=<H= I"II"	II"II"task_idmark_runningmark_succeededmark_failedc                   K   	  ||        t        j                  t        ||||||	|
d	       d{   } || |       	 t        j                  |       y7 $# t        $ rQ}|
j	                  d|        	  || t        |             n!# t        $ r |
j	                  d|        Y nw xY wY d}~nd}~ww xY w# t        $ r Y yw xY w# 	 t        j                  |       w # t        $ r Y w w xY wxY ww)z3Background async runner for one uploaded file task.z/transcribe/async)r   r   r   r   r   r   r   r   Nz#Async transcribe failed for task=%sz+Failed to persist failed status for task=%s)asyncio	to_threadrG   r4   	exceptionstrosremove)rH   r   r   rI   rJ   rK   r   r   r   r   r   resultexcs                rF   run_async_transcribe_taskrU   w   s     W((,1')#9!5'

 

 	w'	IIh)

  U>H	US* 	UJGT	U	U  			IIh 		s   C.-A AA  B5 C.A 	B2!B-4BB-B%"B-$B%%B-(C -B22C 5	C>C. CC.C+CC+	C(%C+'C((C++C.)__doc__rM   r&   rQ   r)   typingr   r   r   r   pydubr   servicesr	   r
   r   r   r   r   utils.speaker_idr   rP   intboolLoggerrG   rU        rF   <module>r`      s{   9   	  0 0   . "''+ ^^ !^ 	^
 ^ %R%89^ ^ W^^$^ ^ 
#s(^^B'' ' !	'
 C5$;'' c4S>2D89' 3*d*+' ' ' %R%89' ' NN' 
'r_   