o
    o›Xjd/  ã                   @  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
mZ d dlmZ d dlmZ d d	lmZ d d
lmZmZ G dd„ dƒZdS )é    )ÚannotationsN)ÚPath)ÚImageGenerator)ÚAudioModule)ÚFetchedImageÚfetch_news_image)ÚSoraVideoGenerator)ÚVideoDirector)ÚVisualRenderer)Ú	PLATFORMSÚContentIdeac                   @  sf   e Zd Zddddddddœd2dd„Zd3dd„Zd4d5d"d#„Zd6d(d)„Zd7d,d-„Zd7d.d/„Zd8d0d1„Z	dS )9ÚAssetProducerNTÚsoraé   F)Úimage_generatorÚsora_generatorÚenable_videoÚvideo_engineÚvideo_max_secondsÚenable_ai_imagesÚenable_audioÚrendererr
   r   úImageGenerator | Noner   úSoraVideoGenerator | Noner   Úboolr   Ústrr   Úintr   r   c          	      C  sV   || _ || _|| _|| _|pd ¡  ¡ | _|o|d u| _t||d�| _	t
|d�| _d S )Nr   )Úmax_secondsr   )Úenabled)r   r   r   r   ÚstripÚlowerr   r   r	   Úffmpeg_videor   Úaudio)	Úselfr   r   r   r   r   r   r   r   © r$   úsrc/generators/assets.pyÚ__init__   s   zAssetProducer.__init__Úidear   Úout_dirr   ÚreturnÚdictc           
   
   C  s.  |j ddd� i d|j“d|j“d|j“d|j“d|j“d|j“d	|j“d
|j“d|j	“d|j
“d|j“d|j“d|j“d|j“d|j“d|j“d|j“|ji d d d d | j ¡ jg dœ¥}|j	|j
|j|j› d|j› d|j	› �dœ}|jdkrydnd}|jdkr�|d  |  |||||¡¡ nC|jdkr¯| jr¯|d  |  ||||¡¡ |j|d< |j|d< n#|  ||||¡}|j|d< | jrÆt | jd d ƒnd |d!< |d  !|j"¡ t#D ]*}| $|¡}|› d"�}	||	 j%| &¡ d#d$� |j'|j(|	d%œ|d& |< |d  !|	¡ qÔ|d' j%t)j*|d(d)d*�d#d$� |d  !d'¡ |S )+NT)ÚparentsÚexist_okÚidÚformatÚtopicÚhookÚnews_urlÚ
news_titleÚnews_categoryÚcreative_directionÚlayoutÚpaletteÚpalette_nameÚ
typographyÚbackground_styleÚillustration_styleÚwhy_it_mattersÚkey_factÚvisual_brief)Úsource_refsÚ	platformsÚimage_sourceÚimage_modelr   Úvideo_modelr"   Úfilesú-)r5   r6   r8   Úcreative_keyÚportrait)é8  iF  )rG   rG   ÚcarouselrC   ÚvideoÚvideo_scriptÚvideo_styler@   ÚmodelrA   z.txtúutf-8©Úencoding)ÚpostÚhashtagsÚfiler?   z	copy.jsonFé   )Úensure_asciiÚindent)+Úmkdirr-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r"   ÚplanÚ__dict__ÚaspectÚextendÚ_produce_carouselr   Ú_produce_videorJ   rK   Ú_produce_singleÚsourcer   ÚgetattrÚappendÚpathr   ÚplatformÚ
write_textÚas_publish_textrP   rQ   ÚjsonÚdumps)
r#   r'   r(   ÚmetaÚstyle_kwargsÚsizeÚherorb   rP   Úfnamer$   r$   r%   Úproduce%   s’   ÿþýüûúùø	÷
öõôóòñðï
çü




ýzAssetProducer.produceÚpromptú
str | Noneútuple[Path | None, str]c           
   
   C  sè   |p
|j p
|jp
|j ¡ }| jrb|rb|dtt|ƒƒd › d� }z$| jj||j	|j
|j|j|jd�}| jj|||jp;dd� || jjfW S  tya } z|d jt|ƒdd	� W Y d }~nd }~ww |d
 }t|j|ƒ}	|	rr|dfS dS )NÚbg_ai_i€–˜ ú.png)Úbase_promptÚcategoryr:   r9   r6   r0   Úsquare)rY   zai_image_error.txtrM   rN   zbg_news.jpgÚnews_og)NÚ	generated)Úimage_promptr=   r0   r   r   ÚabsÚhashr   Úbuild_promptr3   r:   r9   r6   ÚgeneraterY   rL   Ú	Exceptionrc   r   r   r1   )
r#   r'   r(   rm   Úprompt_textÚai_pathÚfullÚexcÚrawÚfetchedr$   r$   r%   Ú_generate_bgf   s.   
ú"€ÿzAssetProducer._generate_bgrh   ri   útuple[int, int]r   c           	      C  sl   |   ||¡\}}|d }|jp|jp|jp|j}| jj|f|j|d d… | ¡ ||dœ|¤Ž t	|j
|d�S )Nzhero.pngé    )r0   ÚbodyÚfooterÚbg_imageri   )ra   r^   )rƒ   r;   r<   r=   r/   r   Úcompose_heror0   Únews_source_labelr   Úname)	r#   r'   r(   rh   ri   Úbgr^   Úoutr†   r$   r$   r%   r]   ~   s   ÿ
úù	zAssetProducer._produce_singlerg   ú	list[str]c                 C  sÌ  |j p"|j|jddœd|jddœd|jddœd|jddœd	d
ddœg}|  ||¡\}}||d< |dv r5|n||d< d }	| jrdt|ƒdkrd|t|ƒd  }
|
 d¡pY|j› d|j	› �}| j|||d�\}	}ddl
m} g }|j|v rw| |j¡nd}t|ƒD ]f\}}|d|d d›d� }||| t|ƒ  }|	r¢|t|ƒd kr¢|	n|}| jj|| dd¡| dd¡d d… |d › dt|ƒ› d| ¡ d d … › �||j|j||j› d!|› d"|› �|d#�
 | |j¡ q}|S )$Nr0   )Útitler†   ÚrolezWhat happenedÚwhatzWhy it mattersÚwhyzThe numbersÚstatsz	Your movezSave this for later.Úctar@   >   ru   rv   rA   é   rS   rw   u    â€” visual metaphor, )rm   r   ©ÚLAYOUTSÚslide_é   Ú02drq   r�   Ú r†   é´   ú/u    Â· é$   z-crD   )	r0   r†   r‡   r5   r6   r8   rˆ   rE   ri   )Úslidesr0   r;   r/   r<   rƒ   r   ÚlenÚgetr:   Úsrc.services.creativer—   r5   ÚindexÚ	enumerater   r‰   rŠ   r6   r8   r-   r`   r‹   )r#   r'   r(   rh   rg   ri   rŸ   Ú	shared_bgr^   Úalt_bgÚmidÚ
alt_promptÚ_r—   rC   ÚstartÚiÚslidera   r5   rŒ   r$   r$   r%   r[   �   sH   
û
&özAssetProducer._produce_carouselc              
   C  s:  |j pg }|d }| jdkr•| jdur•zT| jj|j|j|j|j|j|j	|j
|d�}|d j|dd� | jj||d|jd	kr?d
nd	d�}| j |d |¡}	d|d< | d¡|d< | d¡|d< d|d< |	jdgW S  ty” }
 z!|d jt|
ƒdd� t|
ƒdd… |d< | jdkrŠ‚ W Y d}
~
nd}
~
ww |  ||||¡S )zGPrefer Sora original footage; optional ffmpeg fallback (not preferred).ú	short.mp4r   N)r0   r/   r;   r<   r4   rK   r3   Úbeatszsora_prompt.txtrM   rN   Ú12Ú	landscaperF   )ÚsecondsrY   ra   r   rL   rB   Úvideo_idÚsora_video_idÚsora_nativer@   zsora_error.txtiô  Ú
sora_error)rJ   r   r   rz   r0   r/   r;   r<   r4   rK   r3   rc   r{   rY   r"   Úattachr¡   r‹   r|   r   Ú_produce_video_ffmpeg_fallback)r#   r'   r(   rh   rg   ÚscriptÚ
video_pathrm   ÚresultÚfinalr€   r$   r$   r%   r\   µ   sH   
ø
ü
þ€üzAssetProducer._produce_videoc                 C  sÊ  |j p#d|j|j|jdddœd|jpd|jddd	dœd
ddddddœg}|d }|jdd� |  ||¡\}}||d< ddlm	}	 g g g g f\}
}}}|j
|	v rU|	 |j
¡nd}t|ƒD ]j\}}|d|d d›d� }| jj|| d¡pu|j| d¡p{dd d… | ¡ |	||d  t|	ƒ  |j|j||j› d|› �d�	 |
 |¡ | | d¡p©dd d… ¡ | | d¡p·d¡ | t| d ¡pÂd!ƒ¡ q[|d" }| jj|
|||jpÔd#||d$� d%|d&< d |d'< |jgS )(Nr0   Úpunch_ing      @)ÚbeatÚ	on_screenÚ	voiceoverÚvisualÚmotionr±   ÚdetailsÚDetailsr›   Úzoom_iné   r”   zFollow for daily AI dropsÚzoom_outé   ÚframesT)r,   r@   r   r–   Úframe_r™   rš   rq   r¾   r¿   éx   rS   z-v)r0   r†   r‡   r5   r6   r8   rˆ   rE   éF   rÁ   r±   r•   r­   Ú	cinematic)Úmotion_styleÚmotionsÚ	durationsÚffmpeg_fallbackr   rB   )rJ   r0   r;   rw   r<   r/   rV   rƒ   r¢   r—   r5   r£   r¤   r   r‰   r¡   rŠ   r    r6   r8   r-   r`   Úfloatr!   ÚbuildrK   r‹   )r#   r'   r(   rh   rg   r¸   Ú
frames_dirr¥   r^   r—   Úframe_pathsÚcaptionsrÎ   rÏ   rª   r«   r½   Úframer¹   r$   r$   r%   r·   ß   sL   ý÷
ýz,AssetProducer._produce_video_ffmpeg_fallback)r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r'   r   r(   r   r)   r*   )N)r'   r   r(   r   rm   rn   r)   ro   )
r'   r   r(   r   rh   r*   ri   r„   r)   r   )
r'   r   r(   r   rh   r*   rg   r*   r)   rŽ   )r)   rŽ   )
Ú__name__Ú
__module__Ú__qualname__r&   rl   rƒ   r]   r[   r\   r·   r$   r$   r$   r%   r      s    ö
A


(*r   )Ú
__future__r   re   Úpathlibr   Úsrc.generators.ai_imager   Úsrc.generators.audior   Úsrc.generators.image_sourcer   r   Úsrc.generators.sora_videor   Úsrc.generators.videor	   Úsrc.generators.visualsr
   Úsrc.services.ideationr   r   r   r$   r$   r$   r%   Ú<module>   s    