treadon commited on
Commit
e9f8a15
·
verified ·
1 Parent(s): a8441ec

Upload nucleus_image/text_encoder.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. nucleus_image/text_encoder.py +33 -0
nucleus_image/text_encoder.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Text encoder for Nucleus-Image: Qwen3-VL-8B via PyTorch hybrid."""
2
+
3
+ import torch
4
+ import numpy as np
5
+ import mlx.core as mx
6
+ from transformers import Qwen3VLForConditionalGeneration, Qwen3VLProcessor
7
+
8
+
9
+ class TextEncoder:
10
+ def __init__(self, model, processor):
11
+ self.model = model
12
+ self.processor = processor
13
+
14
+ @staticmethod
15
+ def from_pretrained(model_id="NucleusAI/Nucleus-Image"):
16
+ processor = Qwen3VLProcessor.from_pretrained(model_id, subfolder="processor")
17
+ model = Qwen3VLForConditionalGeneration.from_pretrained(
18
+ model_id, subfolder="text_encoder", torch_dtype=torch.bfloat16
19
+ )
20
+ model.eval()
21
+ return TextEncoder(model, processor)
22
+
23
+ @torch.no_grad()
24
+ def encode(self, text: str) -> mx.array:
25
+ """Encode text → mx.array [T, 4096]."""
26
+ inputs = self.processor(text=[text], return_tensors="pt", padding=True)
27
+ outputs = self.model.model(
28
+ input_ids=inputs["input_ids"],
29
+ attention_mask=inputs["attention_mask"],
30
+ output_hidden_states=True,
31
+ )
32
+ hidden = outputs.hidden_states[-2][0] # second-to-last, [T, 4096]
33
+ return mx.array(hidden.cpu().float().numpy())