8686 "execution_count" : null ,
8787 "metadata" : {},
8888 "outputs" : [],
89- "source" : [
90- " from transformers import GenerationConfig\n " ,
91- " \n " ,
92- " generation_config = GenerationConfig(\n " ,
93- " do_sample=True,\n " ,
94- " temperature=0.3,\n " ,
95- " min_p=0.15,\n " ,
96- " repetition_penalty=1.05,\n " ,
97- " max_new_tokens=512,\n " ,
98- " )\n " ,
99- " \n " ,
100- " prompt = \" Explain quantum computing in simple terms.\"\n " ,
101- " inputs = tokenizer.apply_chat_template(\n " ,
102- " [{\" role\" : \" user\" , \" content\" : prompt}],\n " ,
103- " add_generation_prompt=True,\n " ,
104- " return_tensors=\" pt\" ,\n " ,
105- " return_dict=True,\n " ,
106- " ).to(model.device)\n " ,
107- " \n " ,
108- " output = model.generate(**inputs, generation_config=generation_config)\n " ,
109- " input_length = inputs[\" input_ids\" ].shape[1]\n " ,
110- " response = tokenizer.decode(output[0][input_length:], skip_special_tokens=True)\n " ,
111- " print(response)"
112- ]
89+ "source" : " from transformers import GenerationConfig\n\n generation_config = GenerationConfig(\n do_sample=True,\n temperature=0.1,\n top_k=50,\n repetition_penalty=1.05,\n max_new_tokens=512,\n )\n\n prompt = \" Explain quantum computing in simple terms.\"\n inputs = tokenizer.apply_chat_template(\n [{\" role\" : \" user\" , \" content\" : prompt}],\n add_generation_prompt=True,\n return_tensors=\" pt\" ,\n return_dict=True,\n ).to(model.device)\n\n output = model.generate(**inputs, generation_config=generation_config)\n input_length = inputs[\" input_ids\" ].shape[1]\n response = tokenizer.decode(output[0][input_length:], skip_special_tokens=True)\n print(response)"
11390 },
11491 {
11592 "cell_type" : " markdown" ,
154131 "execution_count" : null ,
155132 "metadata" : {},
156133 "outputs" : [],
157- "source" : [
158- " from transformers import AutoProcessor, AutoModelForImageTextToText\n " ,
159- " from transformers.image_utils import load_image\n " ,
160- " \n " ,
161- " # Load vision model and processor\n " ,
162- " model_id = \" LiquidAI/LFM2.5-VL-1.6B\"\n " ,
163- " vision_model = AutoModelForImageTextToText.from_pretrained(\n " ,
164- " model_id,\n " ,
165- " device_map=\" auto\" ,\n " ,
166- " dtype=\" bfloat16\"\n " ,
167- " )\n " ,
168- " \n " ,
169- " # IMPORTANT: tie lm_head to input embeddings (transformers v5 bug)\n " ,
170- " vision_model.lm_head.weight = vision_model.get_input_embeddings().weight\n " ,
171- " \n " ,
172- " processor = AutoProcessor.from_pretrained(model_id)\n " ,
173- " \n " ,
174- " # Load image\n " ,
175- " url = \" https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg\"\n " ,
176- " image = load_image(url)\n " ,
177- " \n " ,
178- " # Create conversation\n " ,
179- " conversation = [\n " ,
180- " {\n " ,
181- " \" role\" : \" user\" ,\n " ,
182- " \" content\" : [\n " ,
183- " {\" type\" : \" image\" , \" image\" : image},\n " ,
184- " {\" type\" : \" text\" , \" text\" : \" What is in this image?\" },\n " ,
185- " ],\n " ,
186- " },\n " ,
187- " ]\n " ,
188- " \n " ,
189- " # Generate response\n " ,
190- " inputs = processor.apply_chat_template(\n " ,
191- " conversation,\n " ,
192- " add_generation_prompt=True,\n " ,
193- " return_tensors=\" pt\" ,\n " ,
194- " return_dict=True,\n " ,
195- " tokenize=True,\n " ,
196- " ).to(vision_model.device)\n " ,
197- " \n " ,
198- " outputs = vision_model.generate(**inputs, max_new_tokens=64)\n " ,
199- " response = processor.batch_decode(outputs, skip_special_tokens=True)[0]\n " ,
200- " print(response)"
201- ]
134+ "source": "from transformers import AutoProcessor, AutoModelForImageTextToText\nfrom transformers.image_utils import load_image\n\n# Load vision model and processor\nmodel_id = \"LiquidAI/LFM2.5-VL-1.6B\"\nvision_model = AutoModelForImageTextToText.from_pretrained(\n model_id,\n device_map=\"auto\",\n dtype=\"bfloat16\"\n)\n\n# IMPORTANT: tie lm_head to input embeddings (transformers v5 bug)\nvision_model.lm_head.weight = vision_model.get_input_embeddings().weight\n\nprocessor = AutoProcessor.from_pretrained(model_id)\n\n# Load image\nurl = \"https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg\"\nimage = load_image(url)\n\n# Create conversation\nconversation = [\n {\n \"role\": \"user\",\n \"content\": [\n {\"type\": \"image\", \"image\": image},\n {\"type\": \"text\", \"text\": \"What is in this image?\"},\n ],\n },\n]\n\n# Generate response\ninputs = processor.apply_chat_template(\n conversation,\n add_generation_prompt=True,\n return_tensors=\"pt\",\n return_dict=True,\n tokenize=True,\n).to(vision_model.device)\n\noutputs = vision_model.generate(**inputs, do_sample=True, temperature=0.1, min_p=0.15, repetition_penalty=1.05, max_new_tokens=64)\nresponse = processor.batch_decode(outputs, skip_special_tokens=True)[0]\nprint(response)"
202135 },
203136 {
204137 "cell_type" : " markdown" ,
228161 },
229162 "nbformat" : 4 ,
230163 "nbformat_minor" : 0
231- }
164+ }
0 commit comments