我正在构建一个带有 HTML 模板的小型 Flask 应用程序,用户可以在其中上传视频,我将使用法学硕士来描述视频。
我通过 HTML 表单接收文件 URL,然后将其保存到本地文件夹。
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)
文件可以成功保存图像/视频。
然后我尝试使用法学硕士:
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
这似乎对图像文件非常有效。
当我上传视频时,这在我的 Flask 应用程序上不起作用(尽管当我在 Jupyter 笔记本上测试时该机制有效)。
这是我得到的错误:
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.
你们有什么想法吗?
问题在于视频上传后并未立即准备好进行推理:
注意:视频文件在文件 API 中有一个“状态”字段。视频上传后,它将处于 PROCESSING 状态,直到准备好进行推理。只有 ACTIVE 文件才能用于模型推理。
在执行推理之前确保文件已准备好。您需要检查其状态并等待它变为
ACTIVE
。具体方法如下:
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
请参阅验证文件上传并检查状态了解更多信息。