code: def generate_stream(self, params):
      prompt = params["prompt"]
      ori_prompt = prompt
      images = params.get("images", None)
      videos = params.get("videos", None)
      num_image_tokens = 0
  
      if len(videos) > 0 and len(images) == 0:
          yield json.dumps(
              {
                  "text": ori_prompt
                  + "Please switch to 'llama-vid-vicuna-7b-short' to chat with upload short videos. After switch, you can clear the conversaton then retry.",
                  "error_code": 0,
              }
          ).encode() + b"\0"
          return
  
      self.load_model_from_cpu()
      tokenizer, model, image_processor = (
          self.tokenizer,
          self.model,
          self.image_processor,
      )
  
      images = [load_image_from_base64(image) for image in images]
      image = np.array(images[0])
  
      movie_part = int(params.get("movie_part", 1))
  
      if image.shape[0] == 595 and image.shape[1] == 400:
          while True:
              video_file = f"/data/LLaMAVID/movie_feats_part/tt0120338_{movie_part}.pkl"
              print(video_file, os.path.exists(video_file), "+++")
              if os.path.exists(video_file):
                  break
              movie_part -= 1
      elif image.shape[0] == 590 and image.shape[1] == 400:
          while True:
              video_file = f"/data/LLaMAVID/movie_feats_part/tt0499549_{movie_part}.pkl"
              if os.path.exists(video_file):
                  break
              movie_part -= 1
      elif image.shape[0] == 582 and image.shape[1] == 400:
          while True:
              video_file = f"/data/LLaMAVID/movie_feats_part/tt0848228_{movie_part}.pkl"
              if os.path.exists(video_file):
                  break
              movie_part -= 1
      elif image.shape[0] == 592 and image.shape[1] == 400:
          while True:
              video_file = f"/data/LLaMAVID/movie_feats_part/tt0816692_{movie_part}.pkl"
              if os.path.exists(video_file):
                  break
              movie_part -= 1
      elif image.shape[0] == 580 and image.shape[1] == 400:
          while True:
              video_file = f"/data/LLaMAVID/movie_feats_part/tt0109830_{movie_part}.pkl"
              if os.path.exists(video_file):
                  break
              movie_part -= 1
      else:
          print(image.shape)
          self.dump_model_to_cpu()
          yield json.dumps(
              {
                  "text": ori_prompt
                  + "Custom video uploads are currently not supported",
                  "error_code": 0,
              }
          ).encode() + b"\0"
          return
  
      video_info = pickle.load(open(video_file, "rb"))
      input_prompt = video_info["inputs"]
      video = torch.from_numpy(video_info["feats"][:, 1:]).to(
          device=model.device, dtype=model.dtype
      )
      images = [video]
      image_args = {"images": images}
  
      start_prompt = "Below is a part of movie. Memorize the content and answer my question after this movie part. The movie part start.\n"
      end_prompt = "\nThe movie part end.\n"
      input_prompt = start_prompt + input_prompt + end_prompt
  
      cur_prompt = prompt.split("<image>\n")[1].split(" ASSISTANT:")[0]
      prompt = prompt.replace("<image>\n", input_prompt)
  
      temperature = float(params.get("temperature", 1.0))
      top_p = float(params.get("top_p", 1.0))
      max_context_length = getattr(model.config, "max_token", 2048)
  
      max_new_tokens = min(int(params.get("max_new_tokens", 256)), 1024)
      stop_str = params.get("stop", None)
      do_sample = True if temperature > 0.001 else False
  
      input_ids = (
          tokenizer_image_token(
              prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt"
          )
          .unsqueeze(0)
          .to(self.device)
      )
      keywords = [stop_str]
      stopping_criteria = KeywordsStoppingCriteria(
          keywords, tokenizer, input_ids
      )
      streamer = TextIteratorStreamer(
          tokenizer,
          skip_prompt=True,
          skip_special_tokens=True,
          timeout=120,
      )
  
      max_new_tokens = min(
          max_new_tokens, max_context_length - input_ids.shape[-1]
      )
  
      print(input_ids.shape, "-----", video.shape)
  
      if max_new_tokens < 1:
          self.dump_model_to_cpu()
          yield json.dumps(
              {
                  "text": ori_prompt
                  + "Exceeds max token length. Please start a new conversation, thanks.",
                  "error_code": 0,
              }
          ).encode() + b"\0"
          return
  
      torch.cuda.empty_cache()
  
      model.update_prompt([[cur_prompt]])
      thread = Thread(
          target=model.generate,
          kwargs=dict(
              inputs=input_ids,
              do_sample=do_sample,
              temperature=temperature,
              top_p=top_p,
              max_new_tokens=max_new_tokens,
              streamer=streamer,
              stopping_criteria=[stopping_criteria],
              use_cache=True,
              **image_args,
          ),
      )
      thread.start()
  
      generated_text = ori_prompt
      for new_text in streamer:
          generated_text += new_text
          if generated_text.endswith(stop_str):
              generated_text = generated_text[: -len(stop_str)]
          yield json.dumps(
              {"text": generated_text, "error_code": 0}
          ).encode() + b"\0"
  
      self.dump_model_to_cpu()
  
imports: import argparse
  import asyncio
  import json
  import time
  import threading
  import uuid
  import pickle
  import os
  import numpy
  from fastapi import FastAPI, Request, BackgroundTasks
  from fastapi.responses import StreamingResponse
  import requests
  import torch
  import uvicorn
  from functools import partial
  from llamavid.constants import WORKER_HEART_BEAT_INTERVAL
  from llamavid.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN
  from llamavid.model.builder import load_pretrained_model
  from llava.mm_utils import process_images, load_image_from_base64, tokenizer_image_token, KeywordsStoppingCriteria
  from llava.utils import build_logger, server_error_msg, pretty_print_semaphore
  from transformers import TextIteratorStreamer
  from threading import Thread
  from llamavid.train.llama_flash_attn_monkey_patch import replace_llama_attn_with_flash_attn
label_desc: Use image node_id 8 for calling the ModelWorker obj's underlying member method code with example usage: obj.generate_stream(params) without return types