o
    %h+/                     @   s   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	m
Z
 d dlmZ d d	lmZ d d
lmZ e Ze Ze Ze Zd dlmZ d dlZd dlZd dlmZ d dlmZ G dd dZdS )    )HTTPExceptionN   )HashGenerator)UserJourney)MicrolearningDurationEstimator)CourseOutlineParser)ContentReviewer)Synthesizer)LiveWhisperSTTTool)Path)gTTS)AudioSegmentc                   @   st   e Z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d Zdd Zdd Zdd Zdd ZdS )ContentCreationServicec                 C   sn   t  | _|| _t|| _d| j d| _d| j d| _d| j d| _d| j d| _	d| j d| _
d S )Nz	research/.mdzoutput/zoutput1/zoutput2/zparsed_course_content/z.json)r   crew_instance
input_datar   generate_input_hash
input_hashresearch_fileoutput_fileoutput_file_1output_file_2json_output_path)selfr    r   /home/azureuser/microlearn/backend/user_journey_with_openai/agentic_workflow/src/user_journey_service/services/content_creation_service.py__init__   s   zContentCreationService.__init__c              
   C   sj  zt j| jsdddW S t j| jrtd t| j}| }t jt j| j	dd |
| j	| td|d  d	 |d }t|d
 ddD ]I\}}td|  |  r| |rs| |ritd qKtd | | qKtd | ||| | | qKtd | ||| | | qKdd| jdW S W dS  ty } z	tdd| dd}~ww )z6Creates content for each stage and run user evaluationfailurezUser journey is not created.)statusmessagezThe user journey is createdTexist_okzMain Heading: main_heading
stages   startzThe iteration : z3Audio files for content and questions are availablezAudio files are not availablez%content and question is not availablesuccesszcontents are created.)r   r   hashidi  zError parsing result: )status_codedetailN)ospathexistsr   printr   parse_contentmakedirsdirnamer   export_to_json	enumerate_is_first_iteration_has_updated_content_questions _has_audio_content_and_questions_create_audio_files_create_new_stage!_create_audio_files_without_checkr   	Exceptionr   )r   parserparsed_resultr"   idxstageer   r   r   run_content_creation#   s>   



!z+ContentCreationService.run_content_creationc                 C   s&   t d| jj  | jjdkrdS dS )NzInside feedback test : zFirst iterationTF)r/   r   feedback)r   r   r   r   r5   M   s   z*ContentCreationService._is_first_iterationc                 C   s@   t d| j d| d}t d| j d| d}| o| S )Nupdated_content//stager   	question/)r   r   r.   )r   r>   updated_content_filequestion_filer   r   r   r6   S   s   z5ContentCreationService._has_updated_content_questionsc                 C   s   t d td| j d| d}td| j d| d}td| j d| d}td| j d| d}t d	| oE| oE| oE|   | oX| oX| oX| S )
Nz.Inside audio files availability check functionaudio/stage/rD   .wavaudio/questions/z/1.wavz/2.wavz/3.wavzThe output is : )r/   r   r   r.   )r   r>   audio_contentaudio_question1audio_question2audio_question3r   r   r   r7   X   s   * z7ContentCreationService._has_audio_content_and_questionsc                 C   s   i }t d| j d| d}td|  t d| j d| d}t|d}|D ]-}t| | |}td td	 ttd
}||d}	|||< | j	 }
|
j
|	d q*d|i}	| j }
|
j
|	d d S )NrH   rD   rI   z$The audio content is available at : rJ   /*.wavu/   🎤 Press Enter when you're ready to answer...u    📢 Listening to your answer...zEnter your answer: )questionanswerinputsusers_response)r   r   r/   sortedglobwav_to_text_whisperinputstrr   evaluator_crewkickoffassessment_crew)r   r>   all_qns_answerrK   audio_question	wav_fileswav_filerQ   rR   rT   crewr   r   r   _run_evaluationb   s$   



z&ContentCreationService._run_evaluationc                 C   s  t d| j d| d}| rtd|  n=tjtjd| j ddd t d| j d| d	}t|d
dd}|	 }W d    n1 sJw   Y  | 
|| t d| j d| d}t|d}|stjtjd| j d| ddd td | | d S d S )NrH   rD   rI   z/Audio for the content is already available at :rO   Tr    rC   r   rutf-8encodingrJ   rP   question audio is not available)r   r   r.   r/   r,   r1   r-   r2   openreadtext_to_wavrV   rW   _create_audio_question)r   r>   rK   content_fileftext_contentr_   r`   r   r   r   r8   x   s     
&z*ContentCreationService._create_audio_filesc                 C   s   t d| j d| d}tjtjd| j ddd t d| j d| d}t|d	d
d}| }W d    n1 s>w   Y  | || tjtjd| j d| ddd t	d | 
| d S )NrH   rD   rI   rO   Tr    rC   r   rd   re   rf   rJ   rh   )r   r   r,   r1   r-   r2   ri   rj   rk   r/   rl   )r   r>   rK   rm   rn   ro   r   r   r   r:      s    
&z8ContentCreationService._create_audio_files_without_checkc              	   C   s   t d| j d| d}t|ddd}| }W d    n1 s"w   Y  t| |ddD ]&\}}td	|d  d
| d t d| j d| d| d}| || q0d S )NrE   rD   r   rd   re   rf   r%   r&   u   
🧠 Asking Question : r#   rJ   rO   rI   )r   r   ri   rj   r4   parse_questionsr/   rk   )r   r>   rG   rn   	questionsindexrQ   r_   r   r   r   rl      s   
z-ContentCreationService._create_audio_questionc                 C   s   d}t ||t jS )Nz&### Q\d+:\s*\n?(.*?)(?=\n### Q\d+:|\Z))refindallDOTALL)r   md_textpatternr   r   r   rq      s   z&ContentCreationService.parse_questionsc              	   C   s  | j  }td| d|d   |d }td|  td|d   |d }td|d   |d }td	|d
   |d
 }td|d   |d }	td|	  d| d| ||||	d}
td|
  d| j d| d}d| j d| d}td|}|rt| }|d }|d }td| d|  td| j j d ||d< ||d< |	|d< |
|d< | d| |d< | j	j
|d }|j|d! t|d"d#d$}| }W d    n1 sw   Y  d%d&d' |	D }td(|  t|||
|| d| }| j	j|d }||d)< |j|d! d*| j d| d}tjtj|d+d, t|d-d#d$}|| W d    d.S 1 sIw   Y  d.S )/NzStage rp   stage_titlez*The stage value for content creation is : z	  Focus: focusz  Outcome: outcomez  Duration: durationz  Topics Covered: topics_coveredz%The topics for content creation is : )StageFocusOutcomeDurationzTopics Coveredz+The user journey for content creation is : zcontent/rD   r   rE   z\d+x      z)The word count for content creation is : z to z/the level and motive for content creation is : z and self.input_data.motiver"   r?   topicsuser_journey
word_count)r   rS   rd   re   rf   z,and c                 S   s   g | ]}d | d qS )'r   ).0topicr   r   r   
<listcomp>   s    z<ContentCreationService._create_new_stage.<locals>.<listcomp>zThe topic sections are: contentrC   Tr    wz,content and questions created successfully..)r   dictr/   r   rt   searchintgroupLevelr   second_stage_crewr\   ri   rj   joincontent_reviewerreview_and_enrich_contentqa_gen_crewr,   r1   r-   r2   write)r   r>   r?   r"   rT   stage_norz   r{   r|   r   complete_datarm   rG   matchminuteslower_rangeupper_rangerb   rn   texttopic_sectionsupdated_dataupdated_content_pathout_filer   r   r   r9      sj   


z(ContentCreationService._create_new_stagec                 C   sD   t |dd}d}|| t|}|j|dd td|  d S )Nen)r   langztemp_audio.mp3wav)formatzAudio saved as )r   saver   from_mp3exportr/   )r   r   r   ttstemp_mp3audior   r   r   rk      s   

z"ContentCreationService.text_to_wavc                 C   s6   t d td}|t|}t d|d  |d S )Nzinside wave to text conversionbaseu   🎤 Transcription:r   )r/   whisper
load_model
transcriberZ   )r   ra   modelresultr   r   r   rX      s
   
z*ContentCreationService.wav_to_text_whisperN)__name__
__module____qualname__r   rA   r5   r6   r7   rc   r8   r:   rl   rq   r9   rk   rX   r   r   r   r   r      s    *
	9r   )fastapir   r,   utils.hashingr   user_journey_service.crewr   2user_journey_service.processors.duration_estimatorr   5user_journey_service.processors.StagewiseCourseParserr   0user_journey_service.processors.content_reviewerr   8user_journey_service.processors.user_journey_synthesizerr	   *user_journey_service.tools.custom_stt_toolr
   duration_estimatorr   synthesizerstt_toolpathlibr   rt   r   gttsr   pydubr   r   r   r   r   r   <module>   s&    