from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessorfrom qwen_vl_utils import process_vision_infoimport torchfrom PIL import Image # Load the modelmodel = Qwen2_5_VLForConditionalGeneration.from_pretrained( "daniel3303/QwenStoryteller", torch_dtype="auto", device_map="auto") # Load processorprocessor = AutoProcessor.from_pretrained("daniel3303/QwenStoryteller") # Load imagesimages = [ Image.open("image1.jpg"), Image.open("image2.jpg"), Image.open("image3.jpg"), Image.open("image4.jpg"), Image.open("image5.jpg")] # Create image content listimage_content = []for img in images: image_content.append({ "type": "image", "image": img, }) # Add text prompt at the endimage_content.append({"type": "text", "text": "Generate a story based on these images."}) # Create messages with system promptmessages = [ { "role": "system", "content": "You are an AI storyteller that can analyze sequences of images and create creative narratives. First think step-by-step to analyze characters, objects, settings, and narrative structure. Then create a grounded story that maintains consistent character identity and object references across frames. Use <think></think> tags to show your reasoning process before writing the final story." }, { "role": "user", "content": image_content, }] # Preparation for inferencetext = processor.apply_chat_template( messages, tokenize=False, add_generation_prompt=True)image_inputs, video_inputs = process_vision_info(messages)inputs = processor( text=[text], images=image_inputs, videos=video_inputs, padding=True, return_tensors="pt",)inputs = inputs.to(model.device) # Inference: Generation of the outputgenerated_ids = model.generate( **inputs, max_new_tokens=4096, do_sample=True, temperature=0.7, top_p=0.9)generated_ids_trimmed = [ out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)]story = processor.batch_decode( generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0] print(story)