Instructions to use Yosemat/designvlm with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Yosemat/designvlm with Transformers:
# Use a pipeline as a high-level helper # Warning: Pipeline type "visual-question-answering" is no longer supported in transformers v5. # You must load the model directly (see below) or downgrade to v4.x with: # pip install "transformers<5.0.0" from transformers import pipeline pipe = pipeline("visual-question-answering", model="Yosemat/designvlm", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("Yosemat/designvlm", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload folder using huggingface_hub
Browse files34 unmodified lines35363738394011 unmodified lines52535455565745 unmodified lines103104105106107108109135 unmodified lines245246247248249250251252253254255256330 unmodified lines58758858959059159259382 unmodified lines67667767867968068168268368468568668736 unmodified lines72472572672772872973014 unmodified lines74574674774874975075155 unmodified lines80780880981081181281381432 unmodified lines84784884985085185285385445 unmodified lines90090190290390490590690715 unmodified lines92392492592692792892993052 unmodified lines9839849859869879889899904 unmodified lines99599699734 unmodified linesStoppingCriteriaList,set_seed,)from transformers.generation.streamers import BaseStreamerfrom transformers.modeling_outputs import CausalLMOutputWithPastfrom transformers.utils import (11 unmodified lines)_CONFIG_FOR_DOC = "InternLMXcomposer2Config"image_extensions = {".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp"}video_extensions = {".mp4", ".avi", ".mkv", ".mov", ".wmv"}45 unmodified linesself.model = InternLM2Model(config)self.vocab_size = config.vocab_sizeself.output = nn.Linear(config.hidden_size, config.vocab_size, bias=False)self.tokenizer = Noneself.hd_num = 25self.font = get_font()135 unmodified linesself.max_length = max_lengthprompt = ""if meta_instruction:prompt += (f"""[UNUSED_TOKEN_146]system\n{meta_instruction}[UNUSED_TOKEN_145]\n""")for record in history:prompt += f"""[UNUSED_TOKEN_146]user\n{record[0]}[UNUSED_TOKEN_145]\n[UNUSED_TOKEN_146]assistant\n{record[1]}[UNUSED_TOKEN_145]\n"""prompt += f"""[UNUSED_TOKEN_146]user\n{query}[UNUSED_TOKEN_145]\n[UNUSED_TOKEN_146]assistant\n"""image_nums = len(image)if image_nums == 1 and prompt.find("<ImageHere>") == -1:330 unmodified linesshift_labels = labels[..., 1:].contiguous()# Flatten the tokensloss_fct = CrossEntropyLoss()shift_logits = shift_logits.view(-1, self.config.vocab_size)shift_labels = shift_labels.view(-1)# Enable model parallelismshift_labels = shift_labels.to(shift_logits.device)82 unmodified lines):prompt = ""if meta_instruction:prompt += f"""<s>[UNUSED_TOKEN_146]system\n{meta_instruction}[UNUSED_TOKEN_145]\n"""else:prompt += "<s>"for record in history:prompt += f"""[UNUSED_TOKEN_146]user\n{record[0]}[UNUSED_TOKEN_145]\n[UNUSED_TOKEN_146]assistant\n{record[1]}[UNUSED_TOKEN_145]\n"""prompt += f"""[UNUSED_TOKEN_146]user\n{query}[UNUSED_TOKEN_145]\n[UNUSED_TOKEN_146]assistant\n"""return tokenizer([prompt], return_tensors="pt")@torch.no_grad()36 unmodified lines# also add end-of-assistant token in eos token id to avoid unnecessary generationeos_token_id = [tokenizer.eos_token_id,tokenizer.convert_tokens_to_ids(["[UNUSED_TOKEN_145]"])[0],]outputs = self.generate(**inputs,14 unmodified lineselse:outputs = outputs[0].cpu().tolist()response = tokenizer.decode(outputs, skip_special_tokens=True)response = response.split("[UNUSED_TOKEN_145]")[0]history = history + [(query, response)]return response, history55 unmodified linesresponse = generate[0].tolist()response = self.tokenizer.decode(response, skip_special_tokens=True) # type: ignore# remove eoaresponse = response.replace("[UNUSED_TOKEN_145]", "")response = response.replace("[UNUSED_TOKEN_146]", "")return response32 unmodified linesresponse = generate[0].tolist()response = self.tokenizer.decode(response, skip_special_tokens=True) # type: ignore# remove eoaresponse = response.replace("[UNUSED_TOKEN_145]", "")out = response.replace("[UNUSED_TOKEN_146]", "")image_type = "random"pattern = r"""https://source\.unsplash\.com/random/(\d+)x(\d+)/\?([^'"]+)"""if image_type == "placeholder":45 unmodified linesresponse = generate[0].tolist()response = self.tokenizer.decode(response, skip_special_tokens=True) # type: ignore# remove eoaresponse = response.replace("[UNUSED_TOKEN_145]", "")html = response.replace("[UNUSED_TOKEN_146]", "")if seed != -1:set_random_seed(seed, set_cudnn=True)15 unmodified linesresponse = generate[0].tolist()response = self.tokenizer.decode(response, skip_special_tokens=True) # type: ignore# remove eoaresponse = response.replace("[UNUSED_TOKEN_145]", "")js = response.replace("[UNUSED_TOKEN_146]", "")if re.search(r"</script>", html):js = re.findall(r"<script>([\s\S]*?)<\/script>", js)52 unmodified linesresponse = generate[0].tolist()response = self.tokenizer.decode(response, skip_special_tokens=True) # type: ignore# remove eoaresponse = response.replace("[UNUSED_TOKEN_145]", "")out = response.replace("[UNUSED_TOKEN_146]", "")image_type = "random"pattern = r"""https://source\.unsplash\.com/random/(\d+)x(\d+)/\?([^'"]+)"""if image_type == "placeholder":4 unmodified lineswith open(task.replace(" ", "_") + ".html", "w") as f:f.write(out)return out34 unmodified lines3536373839404111 unmodified lines535455565758596045 unmodified lines106107108109110111112135 unmodified lines248249250251252253254255256257258259330 unmodified lines59059159259359459559682 unmodified lines67968068168268368468568668768868969069169236 unmodified lines72973073173273373473514 unmodified lines75075175275375475575655 unmodified lines81281381481581681781881932 unmodified lines85285385485585685785885945 unmodified lines90590690790890991091191215 unmodified lines92892993093193293393493552 unmodified lines9889899909919929939949954 unmodified lines10001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103234 unmodified linesStoppingCriteriaList,set_seed,)from transformers import PreTrainedTokenizerfrom transformers.generation.streamers import BaseStreamerfrom transformers.modeling_outputs import CausalLMOutputWithPastfrom transformers.utils import (11 unmodified lines)_CONFIG_FOR_DOC = "InternLMXcomposer2Config"FROM_TOKEN_1 = "[UNUSED_TOKEN_146]"FROM_TOKEN_2 = "[UNUSED_TOKEN_145]"image_extensions = {".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp"}video_extensions = {".mp4", ".avi", ".mkv", ".mov", ".wmv"}45 unmodified linesself.model = InternLM2Model(config)self.vocab_size = config.vocab_sizeself.output = nn.Linear(config.hidden_size, config.vocab_size, bias=False)self.tokenizer: PreTrainedTokenizer = None # type: ignoreself.hd_num = 25self.font = get_font()135 unmodified linesself.max_length = max_lengthprompt = ""if meta_instruction:prompt += f"""{FROM_TOKEN_1}system\n{meta_instruction}{FROM_TOKEN_2}\n"""for record in history:prompt += f"""{FROM_TOKEN_1}user\n{record[0]}{FROM_TOKEN_2}\n{FROM_TOKEN_1}assistant\n{record[1]}{FROM_TOKEN_2}\n"""prompt += (f"""{FROM_TOKEN_1}user\n{query}{FROM_TOKEN_2}\n{FROM_TOKEN_1}assistant\n""")image_nums = len(image)if image_nums == 1 and prompt.find("<ImageHere>") == -1:330 unmodified linesshift_labels = labels[..., 1:].contiguous()# Flatten the tokensloss_fct = CrossEntropyLoss()shift_logits = shift_logits.view(-1, self.vocab_size)shift_labels = shift_labels.view(-1)# Enable model parallelismshift_labels = shift_labels.to(shift_logits.device)82 unmodified lines):prompt = ""if meta_instruction:prompt += f"""<s>{FROM_TOKEN_1}system\n{meta_instruction}{FROM_TOKEN_2}\n"""else:prompt += "<s>"for record in history:prompt += f"""{FROM_TOKEN_1}user\n{record[0]}{FROM_TOKEN_2}\n{FROM_TOKEN_1}assistant\n{record[1]}{FROM_TOKEN_2}\n"""prompt += (f"""{FROM_TOKEN_1}user\n{query}{FROM_TOKEN_2}\n{FROM_TOKEN_1}assistant\n""")return tokenizer([prompt], return_tensors="pt")@torch.no_grad()36 unmodified lines# also add end-of-assistant token in eos token id to avoid unnecessary generationeos_token_id = [tokenizer.eos_token_id,tokenizer.convert_tokens_to_ids([FROM_TOKEN_2])[0],]outputs = self.generate(**inputs,14 unmodified lineselse:outputs = outputs[0].cpu().tolist()response = tokenizer.decode(outputs, skip_special_tokens=True)response = response.split(FROM_TOKEN_2)[0]history = history + [(query, response)]return response, history55 unmodified linesresponse = generate[0].tolist()response = self.tokenizer.decode(response, skip_special_tokens=True) # type: ignore# remove eoaresponse = response.replace(FROM_TOKEN_2, "")response = response.replace(FROM_TOKEN_1, "")return response32 unmodified linesresponse = generate[0].tolist()response = self.tokenizer.decode(response, skip_special_tokens=True) # type: ignore# remove eoaresponse = response.replace(FROM_TOKEN_2, "")out = response.replace(FROM_TOKEN_1, "")image_type = "random"pattern = r"""https://source\.unsplash\.com/random/(\d+)x(\d+)/\?([^'"]+)"""if image_type == "placeholder":45 unmodified linesresponse = generate[0].tolist()response = self.tokenizer.decode(response, skip_special_tokens=True) # type: ignore# remove eoaresponse = response.replace(FROM_TOKEN_2, "")html = response.replace(FROM_TOKEN_1, "")if seed != -1:set_random_seed(seed, set_cudnn=True)15 unmodified linesresponse = generate[0].tolist()response = self.tokenizer.decode(response, skip_special_tokens=True) # type: ignore# remove eoaresponse = response.replace(FROM_TOKEN_2, "")js = response.replace(FROM_TOKEN_1, "")if re.search(r"</script>", html):js = re.findall(r"<script>([\s\S]*?)<\/script>", js)52 unmodified linesresponse = generate[0].tolist()response = self.tokenizer.decode(response, skip_special_tokens=True) # type: ignore# remove eoaresponse = response.replace(FROM_TOKEN_2, "")out = response.replace(FROM_TOKEN_1, "")image_type = "random"pattern = r"""https://source\.unsplash\.com/random/(\d+)x(\d+)/\?([^'"]+)"""if image_type == "placeholder":4 unmodified lineswith open(task.replace(" ", "_") + ".html", "w") as f:f.write(out)return outdef add_tokens(self, new_tokens: list[str]):self.tokenizer.add_tokens(new_tokens) # type: ignoreself.model.resize_token_embeddings(len(self.tokenizer))self.vocab_size = len(self.tokenizer)# self.output needs to be resized accordingly but without loosing the weightnew_output = nn.Linear(self.model.config.hidden_size,self.vocab_size,bias=False,dtype=self.output.weight.dtype,device=self.output.weight.device,).to(self.device)new_output.weight.data[: self.output.weight.shape[0]] = self.output.weight.datadummy_input_for_output = torch.zeros(1,1,self.model.config.hidden_size,device=new_output.weight.device,dtype=new_output.weight.dtype,).type_as(new_output.weight)# Check if output has same behaviordummy_old_output: torch.Tensor = self.output(dummy_input_for_output)dummy_new_output = new_output(dummy_input_for_output)assert dummy_old_output.allclose(dummy_new_output[:, :, : self.output.weight.shape[0]])self.output = new_output
modeling_internlm_xcomposer2.py+57-22