pythonflaskgoogle-generativeai

Google GenerativeAI unable to load video files


I was building a small Flask app with HTML templates where a user could upload a video and I would use an LLM to describe the video.

I receive the file URL through an HTML form, which I then save to a local folder.

file_url = request.form[f'media-url-{i}']
extension = file_url.split('?')[0].split('.')[-1]
filename = str(uuid.uuid4()) + '.' + extension
file_path = os.path.join(os.environ['TMP_DIR'], filename)

The file works successfully with the image/video being saved.

I then try to use the LLM as such:

genai.configure(api_key=os.environ["GEMINI_API_KEY"])
model = genai.GenerativeModel('gemini-1.5-flash') 

gen_file = genai.upload_file(path=file_path)
response = model.generate_content([llm_prompt, gen_file).text

This seems to work pretty well with image files.

When I upload videos, this doesn't work on my Flask app (although the mechanism works when I test on a Jupyter notebook).

This is the error I get:

line 91, in generate_posts
    response = model.generate_content([llm_prompt, gen_file).text
File "C:\miniconda3\lib\site-packages\google\generativeai\generative_models.py", line 331, in generate_content
    response = self._client.generate_content(
File "C:\miniconda3\lib\site-packages\google\ai\generativelanguage_v1beta\services\generative_service\client.py", line 830, in generate_content
    response = rpc(
File "C:\miniconda3\lib\site-packages\google\api_core\gapic_v1\method.py", line 131, in __call__
    return wrapped_func(*args, **kwargs)
File "C:\miniconda3\lib\site-packages\google\api_core\retry\retry_unary.py", line 293, in retry_wrapped_func
    return retry_target(
File "C:\miniconda3\lib\site-packages\google\api_core\retry\retry_unary.py", line 153, in retry_target
    _retry_error_helper(
File "C:\miniconda3\lib\site-packages\google\api_core\retry\retry_base.py", line 212, in _retry_error_helper
    raise final_exc from source_exc
File "C:\miniconda3\lib\site-packages\google\api_core\retry\retry_unary.py", line 144, in retry_target
    result = target()
File "C:\miniconda3\lib\site-packages\google\api_core\timeout.py", line 120, in func_with_timeout
    return func(*args, **kwargs)
File "C:\miniconda3\lib\site-packages\google\api_core\grpc_helpers.py", line 78, in error_remapped_callable
    raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.FailedPrecondition: 400 The File shtw63tpykp2 is not in an ACTIVE state and usage is not allowed.

Would you guys have any idea?


Solution

  • The problem is that the video is not not being ready for inference immediately after upload:

    Note: Video files have a State field in the File API. When a video is uploaded, it will be in the PROCESSING state until it is ready for inference. Only ACTIVE files can be used for model inference.

    To ensure that the file is ready before performing inference. you need to check its status and wait until it becomes ACTIVE. Here’s how you can do it:

    gen_file = genai.upload_file(path=file_path)
    
    # Check whether the file is ready to be used.
    while video_file.state.name == "PROCESSING":
        print('.', end='')
        time.sleep(10)
        gen_file = genai.get_file(gen_file.name)
    
    if video_file.state.name == "FAILED":
      raise ValueError(video_file.state.name)
    
    response = model.generate_content([llm_prompt, gen_file).text
    

    Refer to the Verify file upload and check state for more informations.