o
    gh	                     @   sx   d dl mZ d dlmZmZ d dlm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ZG dd deZdS )    )BaseTool)	BaseModelField)TypeN)writec                   @   sN   e Zd ZU dZeed< dZeed< edefi Z	e
e ed< defdd	Zd
S )LiveWhisperSTTToolzLive Whisper STT Toolnamez_Records user's voice from microphone until silence is detected, then transcribes using Whisper.description
EmptyInputargs_schemareturnc              
   C   s~  zd}t d| d d}d}d}d}g }t }t }tj|d|d	>}		 |	|\}
}tj|
}||
	  t }||krE|}|| |krPt d n|| |kr[t d nq'W d    n1 sfw   Y  tj
|dd}dt jd d  d}tjd|}tjdd
d t||| td}||}|d |dW S  ty } zd| d dW  Y d }~S d }~ww )N   z%Speak now. Recording will stop after z seconds of silence...i>  i   g{Gz?<      )
sampleratechannels	blocksizeTu*   🔕 Silence detected. Stopping recording.u,   ⏱️ Max recording time reached. Stopping.r   )axisuser_answer_   z.wav
recordings)exist_okbasetext)transcription
audio_filez(An error occurred during transcription: )printtimesdInputStreamreadnplinalgnormappendcopyconcatenateuuiduuid4hexospathjoinmakedirsr   whisper
load_model
transcribe	Exception)selfsilence_durationr   r   silence_thresholdmax_record_secondsaudio_bufferlast_voice_time
start_timestreamblock_volumecurrent_time
audio_datafilenamefilepathmodelresulte rD   ~/home/azureuser/microlearn/backend/user_journey_with_openai/agentic_workflow/src/user_journey_service/tools/custom_stt_tool.py_run   sV   

zLiveWhisperSTTTool._runN)__name__
__module____qualname__r   str__annotations__r	   typer   r   r   rF   rD   rD   rD   rE   r      s
   
 r   )crewai.toolsr   pydanticr   r   typingr   r   sounddevicer   numpyr!   r'   r*   scipy.io.wavfiler   r.   r   rD   rD   rD   rE   <module>   s    