U
    l{f                     @   s6  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlmZmZmZ d dlmZmZ d dlmZ e jejdZeeZejjeddd	Zeej e d
Z!e"e! e#e eej e Z$edddddZ%ej&de%dZ'e&dZ(e$j)ddgdedddZ*dS )    N)AudioSegment)Wav2Vec2CTCTokenizerWav2Vec2ForCTCWav2Vec2Processor)	APIRouter
UploadFile)	AppConfigzapp.logi      )maxBytesbackupCountz4%(asctime)s - %(name)s - %(levelname)s - %(message)szI/var/www/html/Darija-Ai-API/weights/wav2vec2-large-xlsr-darija/vocab.jsonz[UNK]z[PAD]|)Z	unk_tokenZ	pad_tokenZword_delimiter_tokenz>/var/www/html/Darija-Ai-API/weights/wav2vec2-large-xlsr-darija)	tokenizerz /v1/audio/convert-speech-to-textZAudio)tags)filec              
      s  z<d}t j|dd t  d}t j||}td| |  I dH }t	t
|}|j|dd tj|d	}tj }t }tj|d
d\}	}
t|	dddj}t|j}tj|dd}t|}t | }td|  tdddd<}dddddg}tj||d}| |||||d d W 5 Q R X d|iW S  t!k
r } z$t"dt#|  dd i W Y S d}~X Y nX dS )!z:
    Convert speech to text using the Wav2Vec2 model.
    Zaudio_filesT)exist_okz.wavzFILE UUID: NZwav)format)filenamei>  )srpt)Zreturn_tensorspadding)ZaxiszTranscription: ztranscription_results.csva )modenewline	File PathFile DurationUpload DatetimeProcessing DurationTranscription)
fieldnamesr   )r   r   r   r   r   transcriptionz!Error in convert_speech_to_text: errorz2An error occurred while converting speech to text.)$osmakedirsuuiduuid4pathjoinprintreadr   	from_fileioBytesIOZexportlibrosaZget_durationdatetimenowtimeload	processorinput_valuesmodellogitstorchZargmaxr   Zbatch_decodeloggerinfoopencsv
DictWriterwriterow	Exception	exceptionstr)r   Z	audio_dirZunique_filenameZ
audio_pathZ
audio_dataZaudio_segmentdurationZupload_datetimeZ
start_timeZinput_audior   r4   r6   tokensr!   Zprocessing_durationZcsv_filer    writere rE   6/var/www/html/Darija-Ai-API/app/routes/api/v1/audio.pyconvert_speech_to_text7   sB    




rG   )+r#   r,   r;   r%   r1   r7   r.   loggingr/   Zpydubr   Ztransformersr   r   r   fastapir   r   configr   r'   r(   LOG_DIRZlog_file_path	getLogger__name__r8   handlersRotatingFileHandlerZfile_handlersetLevelDEBUG	FormatterZlog_formattersetFormatter
addHandlerrouterr   Zfrom_pretrainedr3   r5   postrG   rE   rE   rE   rF   <module>   s@   



