o
    .i)#                     @   sb   d dl mZ d dlZddlmZ d dlmZ d dlmZ d dl	Z	d dl
Z
d dlZG dd dZdS )	    )HTTPExceptionN   )HashGenerator)UserJourney)Pathc                   @   st   e Zd Zdd Zdd Zdd ZddlZd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )EvaluationServicec                 C   s   t  | _|| _t|| _td| j d| _td| j d| _td| j d| _	td| j d| _
td| j d| _d S )Nzparsed_course_content/z.jsonzaudio/stage//zaudio/questions/zupdated_content/z	question/)r   crew_instance
input_datar   generate_input_hash
input_hashr   parsedsession_audiosession_questionsession_contentsession_question_md)selfr
    r   /home/azureuser/microlearn/backend/user_journey_with_openai/agentic_workflow/src/user_journey_service/services/evaluation_service.py__init__   s   zEvaluationService.__init__c                 C   s   dd t | jD }dd t | jD }tdd |D }g }|D ]}t j|d }||vr6|| q#|r=d|fS d|fS )	Nc                 S      g | ]	}| d r|qS .mdendswith.0fr   r   r   
<listcomp>       z9EvaluationService.verify_stage_md_wav.<locals>.<listcomp>c                 S   r   ).wavr   r   r   r   r   r      r   c                 s   s     | ]}t j|d  V  qdS )r   N)ospathsplitextr   r   r   r   	<genexpr>   s    z8EvaluationService.verify_stage_md_wav.<locals>.<genexpr>r   FT)r!   listdirr   r   setr"   r#   append)r   md_files	wav_fileswav_stages_setmissingmd_file
stage_namer   r   r   verify_stage_md_wav   s   
z%EvaluationService.verify_stage_md_wavc                 C   sZ   t j| jst j| jsdddS t| jd}t| t| jd}t| |S )NfailurezAudio files are not generated.)statusmessage*.wav)r!   r"   existsr   r   sortedglobprint)r   audio_wav_filesquestion_wav_filesr   r   r   get_audio_files%   s   
z!EvaluationService.get_audio_filesr   Nc           
      C   s   |   \}}|rDtdd t| jD }g }|D ](}tj|d }| d}tj| j|}tj| j|}	|	|||	d q|S |S )Nc                 S   r   r   r   r   r   r   r   r   8   r   zAEvaluationService.generate_stage_file_mapping.<locals>.<listcomp>r   r    )stagemd_pathwav_path)
r.   r4   r!   r%   r   r"   r#   joinr   r'   )
r   matchr+   r(   
stage_datar,   r-   wav_filer;   r<   r   r   r   generate_stage_file_mapping1   s    

z-EvaluationService.generate_stage_file_mappingc                 C   s   t | jd}t|}W d    n1 sw   Y  t|d ddD ]+\}}d| d}d| d}d| |d< tj| j||d	< tj| j	||d
< q#|d S )Nrstages   )startr:   r    r   stage_nor;   r<   )
openr   jsonload	enumerater!   r"   r=   r   r   )r   filedatair:   r@   r,   r   r   r   update_parsed_course_contentL   s   z.EvaluationService.update_parsed_course_contentc                 C   s^   |d }t d|  |d }t|ddd}| }W d    n1 s%w   Y  ||dS )Nr;   zThe md path is : r<   rB   utf-8encoding)contentaudio)r6   rG   read)r   r?   r;   audio_contentr   rR   r   r   r   display_contentZ   s   

z!EvaluationService.display_contentc                 C   s   t d}|jjj}|jj}|j}td| td| td| |d }| j| }t|ddd}| }W d    n1 s>w   Y  z d	| d
}	t	|	|}
|
rb|

d }td| d| W n	   | |}Y ||dS )Nz=audio/questions/f3aafd4c95c9b2d02af0b6e68e65745d/stage1/1.wavzHash ID:zStage:zPrefix:r   rB   rO   rP   z### Qz:\s*(.*)rD   zQuestion for prefix :)questionrS   )r   parentnamestemr6   r   rG   rT   researchgroupstripwav_to_text_whisper)r   rK   phash_idr:   prefixqn_stage_filer   rR   patternr>   rX   r   r   r   display_questionb   s,   






z"EvaluationService.display_questionc           	      C   s|   | j  }td|  i }|d }|d }|d }t| jd | d }td|  tt|d}td|  |S )	NThe inputs are : r;   r<   rF   r   the audio questions are in : r2   the question wav files are : )r
   dictr6   strr   r4   r   r5   )	r   r?   inputsall_qns_answerstage_contentrU   stage_valueaudio_questionr)   r   r   r   list_qns~   s   
zEvaluationService.list_qnsc                 C   s   | j  }td td|  t|}t| | |}td td ttd}||d< ||d< | j }|j	|d}|j
S )	NzJust before wav printrg   /   🎤 Press Enter when you're ready to answer...    📢 Listening to your answer...Enter your answer: rX   answerrl   )r
   rj   r6   r   r`   inputrk   r	   evaluator_crewkickoffraw)r   r@   rl   r<   rX   ru   crewfeedbackr   r   r   evaluate_individual_qn   s   


z(EvaluationService.evaluate_individual_qnc                 C   s6   | j  }||d< ||d< | j }|j|d}|jS )NrX   ru   rv   )r
   rj   r	   rx   ry   rz   )r   rX   ru   rl   r{   r|   r   r   r   evaluate_individual_qn_ans_pair   s   


z1EvaluationService.evaluate_individual_qn_ans_pairc                 C   s   | j  }td|  i }|d }|d }|d }t| jd | d }td|  tt|d}td|  |D ]0}	t|	 | |	}
t	d	 td
 tt	d}|
|d< ||d< |||
< | j
 }|j|d q>||d< | j
 }|j|d d S )Nrg   r;   r<   r:   r   rh   r2   ri   rr   rs   rt   rX   ru   rv   users_response)r
   rj   r6   rk   r   r4   r   r5   r`   rw   r	   rx   ry   assessment_crew)r   r?   rl   rm   rn   rU   ro   rp   r)   r@   rX   ru   r{   r   r   r   run_evaluation   s0   



z EvaluationService.run_evaluationc                 C   s6   t d td}|t|}t d|d  |d S )Nzinside wave to text conversionbaseu   🎤 Transcription:text)r6   whisper
load_model
transcriberk   )r   r@   modelresultr   r   r   r`      s
   
z%EvaluationService.wav_to_text_whisper)__name__
__module____qualname__r   r.   r9   r!   rA   rN   rV   rf   rq   r}   r~   r   r`   r   r   r   r   r   
   s    

r   )fastapir   r!   utils.hashingr   user_journey_service.crewr   pathlibr   r   rH   r\   r   r   r   r   r   <module>   s    