o
    S6iA                  
   @   s   d 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
mZ dd	 Zd
eeB defddZdeeB dededeeB def
ddZdefddZdS )z4Gemini API client for image analysis and generation.    N)Path)genai)types   )GOOGLE_API_KEYGEMINI_MODELGEMINI_IMAGE_MODELc                   C   s   t jtdS )zGet Gemini client instance.)Zapi_key)r   ZClientr    r	   r	   P/var/www/tkim.planitai.co.jp/blog/20251208-make-apng-tool/./src/gemini_client.py
get_client	   s   r   
image_pathreturnc                 C   s   t  }t| } t| d}| }W d   n1 sw   Y  tjj|d| jdd   d}d}|j	j
t||gd}|jt| dS )	z
    Analyze a face image to extract features for lip sync generation.

    Args:
        image_path: Path to the image file

    Returns:
        dict with face analysis results
    rbNimage/r   data	mime_typea^  Analyze this face image for lip sync animation creation.

Please identify and describe:
1. Face position and angle (frontal, 3/4 view, etc.)
2. Mouth region location and shape
3. Art style (realistic photo, anime, cartoon, etc.)
4. Skin tone and lip color
5. Any distinctive features around the mouth area

Return the analysis in a structured format.modelcontents)Zanalysisr   )r   r   openreadr   Part
from_bytessuffixlowermodelsgenerate_contentr   textstr)r   clientf
image_data
image_partpromptresponser	   r	   r
   analyze_face_image   s"   

r&   original_image_pathvisemeviseme_descriptionoutput_pathc              	   C   s0  t  }t| } t|}t| d}| }W d   n1 sw   Y  tjj|d| jdd   d}d| d| d| d	}|j	j
t||gtjd
dgdd}	|	jd jjD ]9}
|
jr|
jjdr|
jj}|jjddd t|d}|| W d   |  S 1 sw   Y  |  S qZtd)ag  
    Generate a viseme (mouth shape) variation of the original image.

    Args:
        original_image_path: Path to the original face image
        viseme: Viseme code (A, E, I, O, U, etc.)
        viseme_description: Description of the mouth shape
        output_path: Path to save the generated image

    Returns:
        Path to the generated image
    r   Nr   r   r   zJEdit this face image to change ONLY the mouth shape.

Target mouth shape: z - z

Instructions:
1. Keep the face, eyes, hair, and all other features exactly the same
2. Only modify the mouth/lips area
3. The mouth should show: z
4. Maintain the same art style and quality
5. The transition should look natural

Generate the edited image with the new mouth shape.ZIMAGEZTEXT)Zresponse_modalities)r   r   configr   T)Zparentsexist_okwbzNo image generated in response)r   r   r   r   r   r   r   r   r   r   r   r   ZGenerateContentConfigZ
candidatesZcontentZpartsZinline_datar   
startswithr   parentmkdirwrite
ValueError)r'   r(   r)   r*   r    r!   r"   r#   r$   r%   partZimage_bytesr	   r	   r
   generate_viseme_image;   sH   
	
r4   c               
   C   sb   zt  } | jjtdd}td|j  W dS  ty0 } ztd|  W Y d}~dS d}~ww )zTest the Gemini API connection.z,Say 'API connection successful' in one line.r   z
API Test: TzAPI Test Failed: NF)r   r   r   r   printr   	Exception)r    r%   er	   r	   r
   test_connection{   s   r8   )__doc__base64Zpathlibr   Zgoogler   Zgoogle.genair   r+   r   r   r   r   r   dictr&   r4   boolr8   r	   r	   r	   r
   <module>   s(    -
@