
    1_]j#                     6   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d Zedk    r ej         e                       dS 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digest_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digest_file.<locals>.<lambda>   s    &++o">"> r   r   )r   r	   openiterupdater
   )pathhasherblockr   s      @r   digest_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 ]n}|                                	                    d          rE|
                    dd          d                                                             d          c c S ot          j                    d	z  }|                                ri }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.envz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;      sQ   	z~~o&& +z/**)++ BBDIKKRiDikoktkvkvyk  AJ A A{{}} 	>>##..00 	A 	ACyy{{%%&677 Ayya((+113399%@@@@@@@A	A Y[[00F}} $##%%0022 	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appendr/   collect_imageslist)objfoundr9   r:   s       r   rG   rG   9   s   #t 
)))++ 	- 	-JC+++
5#0F0F+3u::X\K\K\h.////*UC"8"8U=M=MNe=f=feU^,,,,ue,,,,	- 	- 
C		 ) 	) 	)E5%(((() )	) 	)r   c                    g }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   replace)errorszdata:   z[DONE]r   z0Image response contained no usable image payloadr?      timeout)decoder-   r/   r.   rG   jsonloadsJSONDecodeError	Exceptionr3   r?   	b64decodeurllibrequesturlopenr   )r   rJ   eventsr5   payloadkindr:   responses           r   	parse_sser`   G   s   EF{{){,,7799 
 
~~g&& 	abb'--// 	'X--!	4:g..6666# 	 	 	D	 	4:d++U3333 	 	 	D	 OMNNN)KD%x&&..			s		3	3 'x}}&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 's6   0"BB%$B%+"C 
CC7EE!Ec                    |d         |          }|d         }| dk    r	 dS d                     |d                   }d| dk    rd	nd
 d|d          d|d          d|d          dd                     |d                    dd                     |d                    d|d          d|d          dd                     |d                    d| d| dS ) Nassetscanonical_title
left_panelu  Standalone vertical cinematic CHANNEL BRANDING PANEL, not a story poster. No people, faces, silhouettes, story title or exclusivity sentence. Dark premium secure-print-room textures, charcoal paper fibers and metal press surfaces, restrained amber warning lights, subtle dust haze and abstract carbon-paper impressions. Let the design and typography flow naturally with the artwork; do not use rigid boxes, coordinate instructions or fixed margins. Render exactly two clear Vietnamese text blocks and no other main headline: Huyền An Audio; Like • Chia sẻ • Đăng ký. Keep both blocks readable, balanced, not cropped and not overlapping. No school brand, watermark, QR, real logo or pseudo headline.z | required_textzCreate a premium cinematic right_panelvertical
horizontalz^ key art poster for a completely fictional Chinese-style Vietnamese audio drama. Composition: compositionz
 Setting: worldz Protagonist: protagonistz Supporting cast: z; supporting_castz
. Motifs: z, motifsz. Palette: palettez. Semi-realistic Asian fictional cast, layered cinematic depth, controlled low-key lighting, detailed faces, fabric, atmospheric haze and reflections. Reveal limit: reveal_limitz Negative constraints: negative_constraintsa  . Typography must be integrated naturally into the key art. Let the provider choose attractive positions and line breaks; do not force text into fixed coordinates, percentage bands, boxes or numerical safe margins. Keep the protagonist's eyes and face unobstructed, and keep all main text readable and uncropped. Render directly and exactly these three Vietnamese text blocks with all accents preserved, without rewriting or adding a slogan: z. Canonical title authority: z;. No watermark, QR, real brand or additional main headline.)join)assetbriefspectitleexacts        r   
prompt_forrw   c   s   ?5!D#$ER	
 	
 JJtO,--E	iE]4J4JjjP\ 	i 	i]+	i 	i7<W~	i 	iUZ[hUi	i 	i ~B  ~G  ~G  HM  N_  H`  ~a  ~a	i 	i99U8_--	i 	i:?	:J	i 	i ~.	i 	i HLyyQVWmQnGoGo	i 	i MR	i 	i ',	i 	i 	i	r   c                 R    t          |           dk    o|                     d          S )Ni s   PNG

)rE   r/   r   s    r   
verify_pngry   {   s%    t99vG$//2F"G"GGr   c                     t                                           st          d          t          j        t                                                     } |                     d          dus|                     d          st          d          t                      }g }dddd	}t                                          rId
 t          j        t                                                                        dg           D             ni }|	                                D ]\  }}t          dz  |z  }t          ||           }t          |                                          }	|                    |i           }
|                                r|
                    d          du rt|
                    d          | d         k    rU|
                    d          |	k    r<|
                    d          t          |          k    r|                    |
           t           |ddddddd}d }t#          dd          D ]}	 t$          j                            t*          t          j        |d                                          ddd|z   dd          }t$          j                            |d          5 }|                                }d d d            n# 1 swxY w Y   t3          |          \  }}t5          |          st          d           |                    d!          }|                    |           |                    |           || d         t           t*          |	t=          |                    t                              |                                 j!        |t          |          d|d"}|                    |            nX# tD          $ r7}t=          |          }|d#k     rtG          j$        d$|z             Y d }~d }~ww xY wt          | d%|           t          j%        &                    dd&           t          '                    t          j        dtQ          |          d#k    | d         |tS          j*        tV          j,                  -                                d'dd$(          d)z              t]          t          j        |dt=          |          d*d          d+           dtQ          |          d#k    ot_          d, |D                       | d         |tS          j*        tV          j,                  -                                d-}t          '                    t          j        |dd$(          d)z              t]          t          j        |d         tQ          |          t=          t                    d.d                     |d         rd/ndS )0Nz+Image brief missing; provider spend blockedverifiedTsource_canon_sha256zImage brief is not verifiedzintro-poster-master.pngzright-panel-master.pngzleft-panel-master.png)intro_posterrf   rd   c                 :    i | ]}|                     d           |S )rr   )r)   .0rows     r   
<dictcomp>zmain.<locals>.<dictcomp>   s&    ccc#  #cccr   rb   r=   verified_fileprompt_sha256r	   r   autohighpng)r7   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  rQ   z'Generated image failed PNG verificationz	.png.part)rr   r|   r7   endpointr   r   bytesr\   r	   r   attempts      z	 failed: )parentsexist_ok)versionr{   r|   rb   
updated_at)r   indent
)rr   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|   rb   
created_at)r{   rb   receiptr   )0BRIEFr+   r3   rT   rU   r,   r)   r;   RECEIPTrC   PROJECTrw   r   encoder   rF   MODELrangerY   rZ   RequestENDPOINTdumpsr[   r   r`   ry   with_suffixwrite_bytesrL   rD   relative_tostatst_sizerW   timesleepparentmkdir
write_textrE   r   nowr   utc	isoformatprintall)rs   r9   rowsmappingexistingrr   filenameoutputr   prompt_hashpriorr]   lastattemptrZ   r_   r   r=   r\   tempr   excr   s                          r   mainr      sb   <<>> JHIIIJu(())EyyD((		:O0P0P(8999
**CD8Ia  rI  J  JGgngugugwgwccG<M<M<O<O1P1P1T1TU]_a1b1bcccc}H"==??  s  sx7"X-E5))"6==??33UB''==?? 	uyy99TAAeiiPeFfFfjo  qF  kG  GG  GG  LQ  LU  LU  Ve  Lf  Lf  ju  Lu  Lu  z  zC  zC  DL  zM  zM  Q\  ]c  Qd  Qd  zd  zdKK!V!VX^nt  GM  `e  f  fQ{{ 	: 	:G- .00
7af@g@g@g@n@n@p@p  L^  j}  PY  \_  P_  {`  {`  io0  p  p^++GS+AA +X#==??D+ + + + + + + + + + + + + + + )$v!%(( R&'PQQQ))+66  '''V$$$ %eDY>Zej  yA  T_  il  ms  m  m  @G  mH  mH  iI  iI  TZ  T_  T_  Ta  Ta  Ti  u{  GR  SY  GZ  GZ  mq  F  G  GC    - - -3xxQ;;JqG|,,,-
 %88$88999TD9994:!Tahm  oD  iE  QU  em  eq  rz  r~  e  e  eI  eI  eK  eK  'L  'L  [`  ij  k  k  k  nr  r  	s  	s  	sdj5dCKKPP_deeemqrrrrrTa)eC?e?e`d?e?e?e<e<e  D  EZ  [  gk  {C  {G  HP  HT  {U  {U  {_  {_  {a  {a  b  bGtz'aHHH4OPPP	$*'*"5TWZ[bWcWcddsx
y
y
yzzz
#*11*s>   <A6O2KOKOKC.O
P,PP__main__)r?   r   rT   r'   sysr   urllib.requestrY   r   r   pathlibr   __file__resolver   r   r   r   r   r   r   r   r;   rG   r`   rw   ry   r   __name__exitr   r   r   <module>r      sa      				 



      ' ' ' ' ' ' ' '      
$x..
 
 
"
"
*1
-**
/
/=, , ,  7 7 74) ) )' ' '8  0H H H.+ .+ .+b zCHTTVV r   