
    $I`j+                        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	m
Z
 d dlmZ  ee                                          j        d         Zedz  Zedz  ZdZdZd	 Zd
 Zd Zd Zd Zd Zd Zedk    rS	  ej         e                       dS # e$ r0Z e de ej!                    ej        d           Y dZ[dS dZ[ww xY wdS )    N)datetimetimezone)Path   zimage/image-brief.jsonzlog/image-generation.jsonz0http://192.168.40.11:20128/v1/images/generationszcx/gpt-5.5-imagec                 N    t          j        |                                           S )N)hashlibsha256	hexdigest)datas    script/generate-images.py	sha_bytesr      s    >$))+++    c                    t          j                    }|                     d          5 t          fdd          D ]}|                    |           	 d d d            n# 1 swxY w Y   |                                S )Nrbc                  .                          d          S )Ni   )read)handles   r   <lambda>zsha_file.<locals>.<lambda>   s    &++o">"> r   r   )r   r	   openiterupdater
   )pathdigestblockr   s      @r   sha_filer      s    ^F	4 !F>>>>DD 	! 	!EMM%    	!! ! ! ! ! ! ! ! ! ! ! ! ! ! ! s   ,A##A'*A'c                     t           j                            d          rt           j        d         S t          j                    dz  t          j                    dz  t          j                    dz  g} | D ]}|                                s|                    dd                                          D ]n}|                                	                    d          rE|
                    d	d
          d
                                                             d          c c S ot          j                    dz  }|                                ri }d}|                    dd                                          D ]}|r3|d                                         s|                                dk    }7|rld|v rh|                                
                    dd
          \  }}|                                                    d                              d          ||<   |                    dd                              d          dk    r|                    d          r|d         S t          d          )NIMAGE_API_KEYz .config/huyen-an-audio/image.envz.config/image-api.envz.envutf-8ignore)encodingerrorszIMAGE_API_KEY==r   z"'z.hermes/config.yamlFr   zmodel::"'base_url /zhttp://192.168.40.11:20128/v1api_keyzIMAGE_API_KEY is unavailable)osenvirongetr   homeexists	read_text
splitlinesstrip
startswithsplitisspacerstripRuntimeError)
candidatesr   rawconfigmodelin_modelkeyvalues           r   find_keyr>       s^   	z~~o&& +z/**)++ BBDIKKRiDikoktkvkvyk  AJ A A{{}} 	>>78>DDOOQQ 	A 	ACyy{{%%&677 Ayya((+113399%@@@@@@@A	A Y[[00F}} $##WX#FFQQSS 	A 	AC 3q6>>++ 99;;(2 AC3JJ YY[[..sA66
U"[[]]0055;;C@@c
99Z$$++C004SSSX]XaXabkXlXlS##
5
6
66r   c                    t          | t                    r|                                 D ]\  }}|dv r@t          |t                    r+t	          |          dk    r|                    d|f           I|dk    rBt          |t                    r-|                    d          r|                    d|f           t          ||           d S t          | t                    r| D ]}t          ||           d S d S )N>   imageb64_jsoni  base64url)zhttp://zhttps://)	
isinstancedictitemsstrlenappendr2   collectlist)objfoundr<   r=   s       r   rJ   rJ   :   s   #t 
"))++ 	& 	&JC+++
5#0F0F+3u::X\K\K\h.////*UC"8"8U=M=MNe=f=feU^,,,,u%%%%	& 	& 
C		 " 	" 	"EE5!!!!" "	" 	"r   c                    g }d}|                      dd                                          D ]z}|                    d          s|dd                                          }|r|dk    r=|dz  }	 t	          t          j        |          |           f# t
          j        $ r Y ww xY w|s4	 t	          t          j        |           |           n# t          $ r Y nw xY w|st          d	          |d
         \  }}|dk    rt          j        |          |fS t          j                            |d          5 }|                                |fcd d d            S # 1 swxY w Y   d S )Nr   r   replace)r!   zdata:   z[DONE]r   z)Image provider response contains no imagerB      timeout)decoder0   r2   r1   rJ   jsonloadsJSONDecodeError	Exceptionr6   rB   	b64decodeurllibrequesturlopenr   )r   rM   eventsr8   payloadkindr=   responses           r   parse_responserb   H   s   EF{{79{55@@BB 
 
~~g&& 	abb'--// 	'X--!	DJw''////# 	 	 	D	 	DJt$$e,,,, 	 	 	D	 HFGGG)KD%x&&..			s		3	3 'x}}&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 's6   1"BB&%B&,"C 
CC8EE"Ec                    |d         |          }d                     |d                   }| dk    rdnd}| dk    rdnd	}| dk    r!d
}d|d          d|d          d|d          d}n+| dk    r!d
}d|d          d|d          d|d          d}nd}d}d| d|d          d| d|d          d|d          dd                     |d                     d!|z   d"| d#| d$z   S )%Nassetsz | required_textintro_posterzhorizontal 16:9vertical
left_paneltwothreezyUse exactly one fictional adult Asian woman and one fictional adult Asian man. No second woman, crowd or romantic rival. zWorld: worldz Protagonist: protagonistz Love interest: love_interestu  . The station exit must remain visibly open. The man stands to one side and never blocks, grabs or corners the woman. Use only an inactive fictional station platform with an old glass canopy; no moving train, danger, readable timetable, railway logo or real transit branding. The canonical title MUST end exactly with the words Một Chiều and MUST NOT add a period, full stop, dot or terminal punctuation after Chiều. right_panelz. Keep the exit open and the man clearly off the woman's path. No moving train, danger, readable signage or real transit branding. zNNo people, faces, silhouettes, characters, props, setting or narrative scene. u  This asset is abstract channel branding only; do not inherit story world, cast, station, train, ticket, contract, clock or whistle motifs. The call-to-action MUST end exactly with the word ký and MUST NOT add a period, full stop, dot or terminal punctuation after ký. zPremium cinematic z_ artwork for a completely fictional Chinese-style Vietnamese romance audio drama. Composition: composition z	Palette: palettez. Reveal limit: reveal_limitz. Negative constraints: z; negative_constraintsz. a  Keep the visible emotional focus on an adult woman freely choosing to leave with a blank one-way ticket while a man respects her open path; do not imply coercion, a triangle, investigation or mystery. Typography must be rendered directly by the image provider as part of this same artwork; no blank text area and no separate text layer. Let typography flow naturally, never cover eyes or faces, and keep every required block fully visible, readable and uncropped. Render exactly these zK Vietnamese text blocks with every accent preserved and without rewriting: a  . ABSOLUTE TEXT RULE: the required blocks are the only readable or letter-like characters anywhere. Every prop, surface and decorative element must contain no other letters, numbers, signatures, pseudo-text or glyphs. No watermark, QR code, real logo or extra caption.)join)assetbriefspecexactorientation
text_count	cast_rulecontexts           r   
prompt_forr}   d   s   ?5!DJJtO,--E','>'>##JK<//WJ P	beGn b bE-4H b bZ_`oZp b b b 	 
-		 P	PeGn P PE-4H P PZ_`oZp P P P 	
 e	R 	
	N[ 	N 	N]+	N 	N.5	N 	N@Ei@P	N 	Nbghvbw	N 	N!%51G+H!I!I	N 	N 	N 		= !+	= 	= x}	= 	= 	=	
	=r   c                  (   t          j        t          j        t	          t
          dz            dgt
                    } | j        rt          d          t          	                                st          d          t          j        t                              d                    }|                    d          d	us|                    d
          st          d          t                      }dddd}i }t                                          rt          j        t                              d                    }|                    d
          |d
         k    rL|                    d          |                    d          k    r d |                    dg           D             }g }|                                D ]\  }}t
          dz  |z  }	t%          ||          }
t'          |
                    d                    }|                    |i           }|	                                rl|                    d          d	u rU|                    d          |k    r<|                    d          t+          |	          k    r|                    |           t.          |
ddddddd}d }t1          dd          D ]}	 t2          j                            t8          t          j        |d                              d          dd d!|z   d"d#$          }t2          j                            |d%&          5 }|                                }d d d            n# 1 swxY w Y   tA          |          \  }}tC          |          d'k     s|"                    d(          st          d)          |	#                    d*          }|$                    |           tK          j&        ||	           ||d
         |d         |d         |         d+         t.          t8          |t	          |	'                    t
                              |	(                                j)        t+          |	          ||d	d	dd,}|                    |            nX# tT          $ r7}t	          |          }|d-k     rtW          j,        d.|z             Y d }~d }~ww xY wt          | d/|           t          j-        .                    d	d	0           t          /                    t          j        d.d|d
         |d         |ta          j1        td          j3                  4                                d1dd.2          d3z   d           tk          t          j        |d	t	          |	          d4d          d	5           d.tC          |          d-k    otm          d6 |D                       |d
         |d         d	|ta          j1        td          j3                  4                                d7}t          /                    t          j        |dd.2          d3z   d           tk          t          j        |d         tC          |          d8d                     |d         rd9ndS ):Nz script/validate-workflow-copy.pyzpre-provider)cwdz#workflow-copy authority gate failedzverified image brief missingr   )r    verifiedTsource_canon_sha256zimage brief not verifiedzintro-poster-master.pngzright-panel-master.pngzleft-panel-master.png)rf   rn   rh   workflow_copy_authority_sha256c                 :    i | ]}|                     d           |S )ru   )r,   .0rows     r   
<dictcomp>zmain.<locals>.<dictcomp>   s&    SSSC#'''**CSSSr   rd   r@   verified_fileprompt_sha256r	   r   autohighpng)r:   promptnsizequality
backgroundimage_detailoutput_format   F)ensure_asciizapplication/jsonztext/event-streamzBearer )zContent-TypeAcceptAuthorizationPOST)r   headersmethodi  rS   i s   PNG

z'generated asset failed PNG verificationz	.part.pngre   )ru   r   r   re   r:   endpointr   r   bytesr	   r^   attemptsr   text_rendered_by_providerpostprocessed_text      z	 failed: )parentsexist_ok)versionr   r   r   rd   
updated_at)r   indent
)ru   r   r   )flushc              3   &   K   | ]}|d          V  dS )r   N r   s     r   	<genexpr>zmain.<locals>.<genexpr>   s(      ?e?eY\O@T?e?e?e?e?e?er   )r   r   r   r   provider_rendered_text_onlyrd   
created_at)r   rd   r   )7
subprocessrunsys
executablerG   PROJECT
returncoder6   BRIEFis_filerV   rW   r/   r,   r>   RECEIPTr.   rF   r}   r   encoder   rI   MODELranger[   r\   RequestENDPOINTdumpsr]   r   rb   rH   r2   with_suffixwrite_bytesr*   rO   relative_tostatst_sizerY   timesleepparentmkdir
write_textr   nowr   utc	isoformatprintall)gaterv   r<   mapping
prior_rowspriorrowsru   filenameoutputr   
prompt_shaoldr_   lastattemptr\   ra   r   r@   r^   tempr   excreceipts                            r   mainr      sL   >3>3w9[/[+\+\^lmsz{{{D B@AAA==?? ;9:::Ju8899EyyD((		:O0P0P(5666
**C8Ia  rI  J  JGJ~~ T
7,,g,>>??99*++u5J/KKKPUPYPYZzP{P{  @E  @I  @I  Jj  @k  @k  Qk  QkSS599Xr;R;RSSSJD"==??  x  xx7"X-E5))v}}W5566
nnUB''==?? 	sww774??CGGOD\D\`jDjDjorovovw  pA  pA  EM  NT  EU  EU  pU  pUKK!V!VX^nt  GM  `e  f  fQ{{ 	: 	:G- .00
7af@g@g@g@n@nov@w@w  Se  qD  W`  cf  Wf  Bg  Bg  pv0  w  w^++GS+AA +X#==??D+ + + + + + + + + + + + + + + .t 4 4vu::&&e.>.>?S.T.T&&'PQQQ))+66  '''
4((( %eDY>Z  D  Ee  f  y~  G  yH  IN  yO  P_  y`  kp  ~F  Yc  mp  qw  qC  qC  DK  qL  qL  mM  mM  X^  Xc  Xc  Xe  Xe  Xm  yA  BH  yI  yI  U[  ip  CG  fj  BG  H  HC    - - -3xxQ;;JqG|,,,-
 %88$88999TD9994:!_dez_{  `e  fF  `G  SW  go  gs  t|  t@  gA  gA  gK  gK  gM  gM  'N  'N  ]b  kl  m  m  m  pt  t  F  	G  	G  	Gdj54VUUdijjjrvwwwwwTa)eC?e?e`d?e?e?e<e<e  D  EZ  [  D  Ee  f  GK  W[  ks  kw  x@  xD  kE  kE  kO  kO  kQ  kQ  R  RGtz'aHHH4OZabbb	$*'*"5TKKZ_
`
`
`aaa
#*11*s>   A7R9MRMR!M"D#R
S,SS__main__zImage generation blocked: )file)"rB   r   rV   r*   r   r   r   urllib.requestr[   r   r   pathlibr   __file__resolver   r   r   r   r   r   r   r   r>   rJ   rb   r}   r   __name__exitrY   r   r   stderrr   r   r   <module>r      s      				     



      ' ' ' ' ' ' ' '      
$x..
 
 
"
"
*1
-**
/
/=, , ,  7 7 74" " "' ' '8% % %P5+ 5+ 5+p z   0300szBBBB s   B C %CC