U
    g;e                     @   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_test.logi      )maxBytesbackupCountz4%(asctime)s - %(name)s - %(levelname)s - %(message)szR/var/www/html/Darija-Ai-API/weights/wav2vec2-large-xlsr-darija-train_v1/vocab.jsonz[UNK]z[PAD]|)	unk_token	pad_tokenword_delimiter_tokenzG/var/www/html/Darija-Ai-API/weights/wav2vec2-large-xlsr-darija-train_v1)	tokenizerz /v2/audio/convert-speech-to-textzAudio D-voice)tags)filec              
      s~  z2d}t j|dd t  d}t j||}|  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
rx } 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.
    audio_filesT)exist_okz.wavNwav)format)filenamei>  )srpt)return_tensorspadding)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readr   	from_fileioBytesIOexportlibrosaget_durationdatetimenowtimeload	processorinput_valuesmodellogitstorchargmaxr   batch_decodeloggerinfoopencsv
DictWriterwriterow	Exception	exceptionstr)r   	audio_dirunique_filename
audio_path
audio_dataaudio_segmentdurationupload_datetime
start_timeinput_audior   r<   r>   tokensr(   processing_durationcsv_filer'   writere rY   6/var/www/html/Darija-Ai-API/app/routes/api/v2/audio.pyconvert_speech_to_text7   s@    



r[   )+r*   r2   rE   r,   r9   r?   r5   loggingr7   pydubr   transformersr   r   r   fastapir   r   configr   r.   r/   LOG_DIRlog_file_path	getLogger__name__rB   handlersRotatingFileHandlerfile_handlersetLevelDEBUG	Formatterlog_formattersetFormatter
addHandlerrouterr   from_pretrainedr;   r=   postr[   rY   rY   rY   rZ   <module>   s@   



