MCPcopy Create free account
hub / github.com/AMAP-ML/EMF / TextToImageInference

Class TextToImageInference

inference.py:34–75  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

32 top_k: int = 1200
33
34class TextToImageInference:
35 def __init__(self, config: T2IConfig):
36 self.config = config
37 self.device = torch.device(config.device)
38 self._load_models()
39
40 def _load_models(self):
41 self.model = blip3oQwenForInferenceLM.from_pretrained(self.config.model_path, torch_dtype=self.config.dtype).to(self.device)
42 self.tokenizer = AutoTokenizer.from_pretrained(self.config.model_path)
43
44 def generate_image(self, prompt: str,cfg_guidance,num_step) -> Image.Image:
45
46 batch_messages = []
47
48
49 messages = [
50 {"role": "system", "content": "You are a helpful assistant."},
51 {"role": "user", "content": f"Please generate image based on the following caption: {prompt}"}
52 ]
53 input_text = self.tokenizer.apply_chat_template(
54 messages,
55 tokenize=False,
56 add_generation_prompt=True)
57 input_text += f"<im_start><S{self.config.scale}>"
58
59 batch_messages.append(input_text)
60
61 # tokenize as a batch
62 inputs = self.tokenizer(batch_messages, return_tensors="pt", padding=True, truncation=True, padding_side="left")
63 _, output_image = self.model.generate_images(
64 inputs.input_ids.to(self.device),
65 inputs.attention_mask.to(self.device),
66 max_new_tokens=self.config.seq_len,
67 # image_sizes=512,
68 do_sample=True,
69 top_p=self.config.top_p,
70 top_k=self.config.top_k,
71 guidance_scale=cfg_guidance,
72 num_inference_steps=num_step,)
73
74 print(output_image)
75 return output_image[0]
76
77
78def main():

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected