§
    Û×j£I  ã                   ó    — d Z ddlmZ ddlZddlmZ ddlmZ ddl	m
Z
mZmZmZmZ dZd	Zd
ZdZdZdZdZeZdZdZdZdZdZ G d„ d¦  «        ZdS )zÁWanGP integration for inclusionAI Ming-Image-0.1-Design.

The checkpoint URLs describe the prepared DeepBeepMeep layout.  The
upstream code and checkpoint provenance are recorded in README.md.
é    )ÚPathN)Úfiles_locator)Úbuild_hf_urlé   )ÚCLASSIC_IMAGEÚCLASSIC_TEXTÚ
JSON_IMAGEÚ	JSON_TEXTÚ
LAYER_PLANzDeepBeepMeep/MingImageÚ
ming_imagezBailingMM2-Ming-ImagezBailingMM2-Ming-Image-CoreÚming_image_sharedÚming_image_0_1_designzMing-Image-0.1-DesignÚming_image_layerÚming_image_0_1_design_layerzMing-Image-0.1-Design-Layer)zconfig.jsonzpreprocessor_config.jsonzspecial_tokens_map.jsonztokenizer.jsonztokenizer_config.jsonué  {
  "canvas_settings": {
    "aspect_ratio": "1:1, 2048 Ã— 2048 px",
    "ambient_lighting": "Soft, even studio light",
    "image_style": "Clean modern editorial poster"
  },
  "layers": [
    {
      "description": "Full-canvas deep blue background with subtle paper texture.",
      "coordinates": "cx: 0.500, cy: 0.500, w: 1.000, h: 1.000",
      "hierarchy_and_relation": "Backmost layer covering the canvas.",
      "color_specs": ["#14213D"]
    },
    {
      "description": "Large centered white title \"OPEN HOUSE\" in bold sans-serif lettering.",
      "coordinates": "cx: 0.500, cy: 0.250, w: 0.700, h: 0.160",
      "hierarchy_and_relation": "Top layer centered above the main artwork.",
      "color_specs": ["#FFFFFF"]
    }
  ]
}c                   óJ  — e Zd Zed„ ¦   «         Zed„ ¦   «         Zed„ ¦   «         Zed„ ¦   «         Zed„ ¦   «         Zed„ ¦   «         Z	ed„ ¦   «         Z
ed„ ¦   «         Zed	„ ¦   «         Zed
„ ¦   «         Zed„ ¦   «         Zedd„¦   «         Zed„ ¦   «         Ze	 	 dd„¦   «         ZdS )Úfamily_handlerc                 óL   — |                      d¦  «        dk    rdnd}i | ¥d|i¥S )NÚtext_encoder_quantizationÚint8Úint8_convrotÚbf16Úming_encoder_variant)Úget)Ú	model_defÚruntime_contextÚvariants      úFC:\Users\johnn\OneDrive\Documents\AI\models\ming_image\ming_handler.pyÚresolve_runtime_model_defz(family_handler.resolve_runtime_model_def=   s<   € à$3×$7Ò$7Ð8SÑ$TÔ$TÐX^Ò$^Ð$^�.�.ÐdjˆØ=�)Ð=Ð3°WÐ=Ð=Ð=ó    c                 óH   — | t           k    rdS ddlm}  || |||||¦  «        S )NÚ r   )Úrender_prompt_helper)ÚLAYER_ARCHITECTUREÚprompt_helperr"   )Ú
model_typer   Ú	prompt_idÚpopup_idÚprompt_elem_idÚresolution_elem_idr"   s          r   r"   z#family_handler.render_prompt_helperB   sB   € àÕ+Ò+Ð+Ø�2Ø7Ð7Ð7Ð7Ð7Ð7à#Ð# J°	¸9ÀhÐP^Ð`rÑsÔsÐsr   c                  ó"   — ddl m}   | ¦   «         S )Nr   ©Úget_prompt_helper_css)r$   r,   r+   s    r   r,   z$family_handler.get_prompt_helper_cssJ   s#   € à8Ð8Ð8Ð8Ð8Ð8à$Ð$Ñ&Ô&Ð&r   c                  ó"   — ddl m}   | ¦   «         S )Nr   ©Úget_prompt_helper_javascript)r$   r/   r.   s    r   r/   z+family_handler.get_prompt_helper_javascriptP   s#   € à?Ð?Ð?Ð?Ð?Ð?à+Ð+Ñ-Ô-Ð-r   c                  ó   — t           t          gS ©N)ÚARCHITECTUREr#   © r   r   Úquery_supported_typesz$family_handler.query_supported_typesV   s   € åÕ0Ð1Ð1r   c                  ó   — dS )Nr   r3   r3   r   r   Úquery_model_familyz!family_handler.query_model_familyZ   s   € àˆ|r   c                  ó
   — ddiS )Nr   )iˆ  zMing Image 0.1r3   r3   r   r   Úquery_family_infosz!family_handler.query_family_infos^   s   € àÐ6Ð7Ð7r   c                  ó
   — i i fS r1   r3   r3   r   r   Úquery_family_mapsz family_handler.query_family_mapsb   s   € à�2ˆvˆr   c                 ó4   — | t           k    rt          nt          S r1   )r#   ÚLAYER_PROJECTÚPROJECT)Úbase_model_types    r   Úget_lora_dirzfamily_handler.get_lora_dirf   s   € à /Õ3EÒ EÐ E�}ˆ}Í7ÐRr   c                 ó   — ddl m}m} ||fS )Nr   )ÚRGB_BIASÚRGB_FACTORS)Úrgb_factorsrA   rB   )r>   rA   rB   s      r   Úget_rgb_factorszfamily_handler.get_rgb_factorsj   s%   € à6Ð6Ð6Ð6Ð6Ð6Ð6Ð6à˜HÐ$Ð$r   c                 ó<  — | t           k    rpi dd“dd“dd“dd“dd“d	d“d
t           g“dd“dt          “dd„ dD ¦   «         “ddgdddœ“dd“dd“dd“ddgddiddœ“dt          “dd“d d!gd"d#œd$d%d&gd'd#œgd(d)d*d+d,œ¥S i dd“dd“dd“d-d.d/g“dg d0¢d1d2d3d4d0œddœ“d5t          “dt          “d6t
          “d7t          “d8d“dd“d9d:“d;d:“dd“d	d“d
t          g“dd<“t          d=„ dD ¦   «         d>d?gdd@œdddAd%dBdCdDdEdFgdGdHœgd$d%dIgdJd#œdKdLdMgdNd#œgdOdPt          z   dQz   dRdSdTœ¥S )UNÚimage_outputsTÚdevice_explicitÚskip_prompt_templateÚembedded_guidanceFÚguidance_max_phasesr   ÚNAGÚprofiles_dirÚresolution_budgetÚ	1024x1024Útext_encoder_folderÚtext_encoder_URLsc                 óT   — g | ]%}t          t          t          t          |z   ¦  «        ‘Œ&S r3   ©r   ÚREPOÚENCODERÚENCODER_CORE©Ú.0Úsuffixs     r   ú
<listcomp>z2family_handler.query_model_def.<locals>.<listcomp>}   s9   € ð &ð &ð &àõ !¥¥wµ¸vÑ0EÑFÔFð&ð &ð &r   )z_bf16.safetensorsz_int8_convrot.safetensorsÚimage_ref_choices)zDecompose the Reference ImageÚIr[   )ÚchoicesÚletters_filterÚdefaultÚone_image_ref_neededÚone_image_ref_onlyÚno_background_removalÚprompt_enhancer_defÚTIz6Build a Layer Plan from Text Prompt and {image_inputs}r!   )Ú	selectionÚlabelsr^   Ú"image_prompt_enhancer_instructionsÚ image_prompt_enhancer_max_tokensi   zlayer decompositionztransparent design layerszCSeparates one flattened design into ordered transparent PNG layers.)ÚnameÚaliasesÚdescriptionzalpha outputÚRGBAztransparent layersz7Separate a supplied design into transparent PNG layers.aì  **Ming Image Design-Layer** turns a finished poster, infographic, card, or similar design into separate transparent image layers. Select one Reference Image, then describe the pieces you want to separate in Text Prompt, starting with the frontmost. For example, put the words on one layer, the panel behind them on another, the illustration on a third, and the background last. Describe only elements that are visible in the image.

WanGP adds every layer to the gallery and saves them together in a ZIP. You can hide or rearrange the layers in an image editor; text remains part of the image rather than editable type. Start with 12 steps, guidance 2, and 1024x1024 for a detailed square result. Choose 512x512 when speed matters more than detail.a;  Write `Decompose this image into N layers with the following specifications:` followed by `Number of layers: N` and one `Layer i:` line for each layer. Order layers front to back; put readable text on a front layer, any card or panel directly behind it on its own layer, the main subject on its own layer, and the background last. Quote actual visible words exactly and do not invent unreadable lettering. For a quick request, `Decompose this image into 4 layers.` also works.

**Example:**
```text
Decompose this image into 4 layers with the following specifications:
Number of layers: 4
Layer 1: Exact visible headline and subtitle on transparent pixels.
Layer 2: The colored title card directly behind the text.
Layer 3: The main foreground illustration.
Layer 4: The complete background and remaining supporting shapes.
```a@  Ming Image Design-Layer separates one supplied design image into transparent layers. Ask for the visible pieces from front to back, with the background last. Each layer appears in the gallery and in a ZIP. Lettering remains image pixels. Start with 12 steps, guidance 2, and 1024x1024; choose 512x512 for a faster draft.am  Ask for `Decompose this image into N layers` or supply `Number of layers: N` and exact `Layer 1:` through `Layer N:` descriptions. The first layer is topmost and the last is the background. Ground every described object and quoted string in the reference image. Place text, its supporting panel, the main subject, and the background on separate layers when present.)ÚspecialitiesÚinfosÚprompt_infosÚdeepy_infosÚdeepy_prompt_infosÚprompt_helper_popup_dimséV   é^   )ÚTrc   ÚT1ÚTI1z1A Classic Image Prompt using existing Text PromptzIA Classic Image Edit Prompt using existing Text Prompt and {image_inputs}z3A Ming JSON Image Prompt using existing Text PromptzKA Ming JSON Image Edit Prompt using existing Text Prompt and {image_inputs}Ú!text_prompt_enhancer_instructionsÚ"text_prompt_enhancer_instructions1Ú#image_prompt_enhancer_instructions1Útext_prompt_enhancer_max_tokensÚ text_prompt_enhancer_max_tokens1i   Ú!image_prompt_enhancer_max_tokens1Ú	2048x2048c                 óT   — g | ]%}t          t          t          t          |z   ¦  «        ‘Œ&S r3   rR   rV   s     r   rY   z2family_handler.query_model_def.<locals>.<listcomp>×   s9   € ð "ð "ð "àõ �T¥7­L¸6Ñ,AÑBÔBð"ð "ð "r   )ÚNoner!   )zEdit the Reference Imager[   )r\   r]   ÚrgbazRGBA OutputÚdropdownÚDisabled)r‚   r‚   )ÚEnabledrƒ   zlPreserve the model-generated alpha channel in PNG. Transparency depends on the prompt and is not guaranteed.)Úidrh   ÚlabelÚtyper^   r\   Úinfoztransparent backgroundz–Set custom_settings.rgba to Enabled and request a transparent background to preserve model-generated alpha in PNG. Transparency depends on the prompt.zvisual designz	UI designzposter designzACreates text-rich visual designs, posters, and interface mockups.a9  **Ming Image Design** creates posters, infographics, interface mockups, and other visual designs from text. You can also select one Reference Image and describe a change while saying what to keep. For a simple image, write a normal prompt. For a precise layout with titles and labels, use the Ming JSON prompt enhancer or arrange the design in Prompt Helper. Put any words that must appear exactly in quotes.

Start with 12 steps and guidance 1. The default 2048x2048 size gives more detail; choose 1024x1024 to save time and memory. Reference edits work best at 1024x1024; larger edit outputs are resized. To request transparency, enable RGBA Output and ask for a transparent background, though the result may still be opaque. For separating an existing design into individual layers, choose Ming Image Design-Layer instead.a  For text-to-image designs with exact layout, paste one JSON object into Prompt. It has exactly two top-level keys: `canvas_settings` (`aspect_ratio`, `ambient_lighting`, `image_style`) and `layers` (backmost to frontmost). Each layer has `description`, `coordinates`, `hierarchy_and_relation`, and `color_specs` (an array of hex colors). Write `coordinates` as one normalized string: `cx: 0.500, cy: 0.500, w: 1.000, h: 1.000`. Keep each box inside the canvas, describe one complete visible group per layer, and quote every exact rendered string once in its owning layer's description. Match the JSON aspect ratio to the selected WanGP resolution. Use Prompt Helper to draw or edit layer boxes; it updates the size field from the selected resolution.

**Example:**
```json
a2  
```

Plain-language prompts also work. For image editing, describe the change and what to preserve, for example: Change the headline to "OPEN STUDIO" while keeping the layout and colors. For transparency, enable RGBA Output and begin a plain-language prompt with `RGBA, 4-channel, transparent background`.aý  Ming Image Design creates posters, infographics, and other visual designs from text or edits one reference image. Use a plain prompt for simple work or structured JSON for precise layouts. Quote exact visible words. For an edit, name the change and what to preserve. Start with 12 steps and guidance 1; 2048x2048 gives more detail, while 1024x1024 is faster and suits edits. For transparency, enable RGBA Output and request a transparent background. Use Design-Layer to separate an existing image into layers.aß  For a text-to-image design needing exact layout or lettering, write one JSON object with exactly `canvas_settings` (`aspect_ratio`, `ambient_lighting`, `image_style`) and `layers` in back-to-front order. Each visible semantic layer contains exactly `description`, `coordinates`, `hierarchy_and_relation`, and `color_specs` (hex-color array). `coordinates` is one normalized string in the form `cx: 0.500, cy: 0.500, w: 1.000, h: 1.000`; boxes stay inside the canvas. Keep people and objects intact, use the fewest groups that preserve the layout, and put each exact visible string once, quoted verbatim, in its owning description. Use relationships for alignment, containment, stacking and occlusion, with no duplicate text or invisible layers. Match `aspect_ratio` to the chosen WanGP output size. For simple requests, a concrete plain-language prompt is valid. For reference editing, use a specific plain-language change plus what must remain unchanged. Do not request layer decomposition.)rO   rP   rZ   r`   ra   Úcustom_settingsrl   rm   rn   ro   rp   )	r#   rT   r   r   r   r
   r	   r2   ÚSTRUCTURED_PROMPT_EXAMPLE)r>   r   s     r   Úquery_model_defzfamily_handler.query_model_defp   sµ  € àÕ0Ò0Ð0ðGØ ðGà! 4ðGð '¨ðGð $ Uð	Gð
 & qðGð �uðGð Õ!3Ð 4ðGð $ [ðGð &¥wðGð $ð &ð &à"Tð&ñ &ô &ðGð $Ø FÐGØ&)Ø"ð&ð &ðGð& '¨ð'Gð( % dð)Gð* (¨ð+Gð, &Ø"& Ø#Ð%]Ð^Ø!ð(ð (ð-Gð6 5µjð7Gð8 3°Dð9Gð< 2Ø ;Ð<Ø#hð"ð "ð +¸Ð@TÐ7Uð  g`ð  að  að	!bðJð
]ðHð$ðCGð Gð Gð GðPd
Ø˜Tðd
à˜tðd
ð # Dðd
ð '¨¨R¨ð	d
ð
 "Ø5Ð5Ð5àLØeØOØhð	ð ð ð	$ð 	$ðd
ð 0µðd
ð  1µ-ð!d
ð" 1µ)ð#d
ð$ 2µ:ð%d
ð& .¨tð'd
ð( /°ð)d
ð* /°ð+d
ð, 0°ð-d
ð.   ð/d
ð0 �5ð1d
ð2 �\˜Nð3d
ð4   ð5d
õ6 $+ð"ð "àPð"ñ "ô "ð !Ø5ðð #&ð"ð "ð #'Ø%)àØØ&Ø"Ø%Ø4Ð6LÐMð Gð!ð !ð  ð (°VÐ=UÐ4Vð  h@ð  Að  AØ(°kÀ?Ð5Sð  ehð  ið  iðð
	Mð*õ -FñFðIwñwðfð	vðsd
ð d
ð d
ð d	
r   Nc           	      ó¾  — |pi                       dd¦  «        }d|› d�}d|› d�}|t          k    r`t          t          gdddd	|ggd
œt          t
          g|ggd
œt          t          gt          t          ¦  «        gd
œt          t          gddggd
œgS t          t          gdddddd	|ggd
œt          t
          g|ggd
œt          t          gt          t          ¦  «        gd
œgS )Nr   r   Úvision_encoder_z.safetensorsÚconditioning_ztransformer_config.jsonzconnector_config.jsonzmlp_config.jsonzscheduler_config.json)ÚrepoIdÚsourceFolderListÚfileListzvae_config.jsonzvae.safetensors)
r   r#   Ú
LAYER_REPOr<   rS   ÚSHARED_PROJECTrT   ÚlistÚTOKENIZER_FILESr=   )ÚcomputeListr>   r   r   Úvision_fileÚconditioner_files         r   Úquery_model_filesz family_handler.query_model_files!  sF  € à�? ×'Ò'Ð(>ÀÑGÔGˆØ=¨Ð=Ð=Ð=ˆØ@¨7Ð@Ð@Ð@ÐØÕ0Ò0Ð0å%½M¸?Ø-Ð/FÐHYØ+Ð-=ðZð Yð ð õ  µnÐ5EÐU`ÐTaÐSbÐcÐcÝµg°YÍTÕRaÑMbÔMbÐLcÐdÐdÝµg°YØ%Ð'8ðNð Mð ð ð
ð 
õ µ'°Ø)Ð+BÐDUØ!Ð#4Ð6MÐO_ðJð Ið ð õ µ.Ð1AÐQ\ÐP]ÈÐ_Ð_Ýµ'°ÍÍoÑI^ÔI^ÐH_Ð`Ð`ð
ð 	
r   c           
      ó”   — | t           k    r|                     dddddddd¬¦  «         d S |                     dd	dd
dddd¬¦  «         d S )Nr   rN   é   g       @r!   r[   r   )Ú
image_modeÚ
resolutionÚnum_inference_stepsÚguidance_scaleÚ
batch_sizeÚprompt_enhancerÚvideo_prompt_typeÚremove_background_images_refr}   g      ð?)r#   Úupdate)r>   r   Úui_defaultss      r   Úupdate_default_settingsz&family_handler.update_default_settings;  s€   € àÕ0Ò0Ð0Ø×Ò¨!¸Ø35ÀcØ*+¸RÐSVØ<=ð ñ ?ô ?ð ?ð ˆFØ×Ò a°KØ/1À#Ø&'¸ÈrØ89ð 	ñ 	;ô 	;ð 	;ð 	;ð 	;r   Fc                 ó<   — ddl m}  || d         |||||¬¦  «        S )Nr   )Úload_componentsr   )Útransformer_filenameÚtext_encoder_filenamer%   Úsave_quantizedÚquantize_transformerr   )Úming_pipeliner§   )	Úmodel_filenamer%   r>   r   r©   rª   ÚquantizeTransformerÚkwargsr§   s	            r   Ú
load_modelzfamily_handler.load_modelH  sF   € ð 	3Ð2Ð2Ð2Ð2Ð2àˆØ!/°Ô!2Ø"7Ø!Ø)Ø!4Øð
ñ 
ô 
ð 	
r   r1   )NFF)Ú__name__Ú
__module__Ú__qualname__Ústaticmethodr   r"   r,   r/   r4   r6   r8   r:   r?   rD   rŠ   r˜   r¥   r°   r3   r   r   r   r   <   s¦  € € € € € Øð>ð >ñ „\ð>ð ðtð tñ „\ðtð ð'ð 'ñ „\ð'ð
 ð.ð .ñ „\ð.ð
 ð2ð 2ñ „\ð2ð ðð ñ „\ðð ð8ð 8ñ „\ð8ð ðð ñ „\ðð ðSð Sñ „\ðSð ð%ð %ñ „\ð%ð
 ðn
ð n
ñ „\ðn
ð` ð
ð 
ð 
ñ „\ð
ð2 ð
;ð 
;ñ „\ð
;ð à>CØ',ð
ð 
ð 
ñ „\ð
ð 
ð 
r   r   )Ú__doc__Úpathlibr   ÚtorchÚshared.utilsr   ÚflÚshared.utils.hfr   Úprompt_enhancersr   r   r	   r
   r   rS   r=   rT   rU   r’   r2   ÚTRANSFORMERr‘   r<   r#   ÚLAYER_TRANSFORMERr”   r‰   r   r3   r   r   ú<module>r¾      s   ððð ð Ð Ð Ð Ð Ð à €€€à ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø (Ð (Ð (Ð (Ð (Ð (à \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ð \ð  €Ø
€Ø
!€Ø+€Ø$€Ø&€Ø%€Ø€
Ø"€Ø2Ð Ø1Ð ð€ðÐ ð.Y
ð Y
ð Y
ð Y
ð Y
ñ Y
ô Y
ð Y
ð Y
ð Y
r   