Skip to content

Commit 75b97db

Browse files
authored
Add video metadata and error to File (#348)
* Add video metadata and error to File Change-Id: I721147d4e9abf526c7f0a60346761591d63ebb2f * add tests Change-Id: I41a7af34a3068549cee3c45aead9a042415219ee * fix tests Change-Id: I005e30219f49830f73658488e58588d6ed7ccd88
1 parent 6df10a7 commit 75b97db

File tree

2 files changed

+150
-1
lines changed

2 files changed

+150
-1
lines changed

google/generativeai/types/file_types.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from typing import Union
1919
from typing_extensions import TypedDict
2020

21+
from google.rpc.status_pb2 import Status
2122
from google.generativeai.client import get_default_file_client
2223

2324
import google.ai.generativelanguage as glm
@@ -29,7 +30,7 @@ def __init__(self, proto: glm.File | File | dict):
2930
proto = proto.to_proto()
3031
self._proto = glm.File(proto)
3132

32-
def to_proto(self):
33+
def to_proto(self) -> glm.File:
3334
return self._proto
3435

3536
@property
@@ -72,6 +73,14 @@ def uri(self) -> str:
7273
def state(self) -> glm.File.State:
7374
return self._proto.state
7475

76+
@property
77+
def video_metadata(self) -> glm.VideoMetadata:
78+
return self._proto.video_metadata
79+
80+
@property
81+
def error(self) -> Status:
82+
return self._proto.error
83+
7584
def delete(self):
7685
client = get_default_file_client()
7786
client.delete_file(name=self.name)

tests/test_files.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2024 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
from google.generativeai.types import file_types
17+
18+
import collections
19+
import datetime
20+
import os
21+
from typing import Iterable, Union
22+
import pathlib
23+
24+
import google
25+
import google.ai.generativelanguage as glm
26+
27+
import google.generativeai as genai
28+
from google.generativeai import client as client_lib
29+
from absl.testing import parameterized
30+
31+
32+
class FileServiceClient(client_lib.FileServiceClient):
33+
def __init__(self, test):
34+
self.test = test
35+
self.observed_requests = []
36+
self.responses = collections.defaultdict(list)
37+
38+
def create_file(
39+
self,
40+
path: Union[str, pathlib.Path, os.PathLike],
41+
*,
42+
mime_type: Union[str, None] = None,
43+
name: Union[str, None] = None,
44+
display_name: Union[str, None] = None,
45+
resumable: bool = True,
46+
) -> glm.File:
47+
self.observed_requests.append(
48+
dict(
49+
path=path,
50+
mime_type=mime_type,
51+
name=name,
52+
display_name=display_name,
53+
resumable=resumable,
54+
)
55+
)
56+
return self.responses["create_file"].pop(0)
57+
58+
def get_file(
59+
self,
60+
request: glm.GetFileRequest,
61+
**kwargs,
62+
) -> glm.File:
63+
self.observed_requests.append(request)
64+
return self.responses["get_file"].pop(0)
65+
66+
def list_files(
67+
self,
68+
request: glm.ListFilesRequest,
69+
**kwargs,
70+
) -> Iterable[glm.File]:
71+
self.observed_requests.append(request)
72+
for f in self.responses["list_files"].pop(0):
73+
yield f
74+
75+
def delete_file(
76+
self,
77+
request: glm.DeleteFileRequest,
78+
**kwargs,
79+
):
80+
self.observed_requests.append(request)
81+
return
82+
83+
84+
class UnitTests(parameterized.TestCase):
85+
def setUp(self):
86+
self.client = FileServiceClient(self)
87+
88+
client_lib._client_manager.clients["file"] = self.client
89+
90+
@property
91+
def observed_requests(self):
92+
return self.client.observed_requests
93+
94+
@property
95+
def responses(self):
96+
return self.client.responses
97+
98+
def test_video_metadata(self):
99+
self.responses["create_file"].append(
100+
glm.File(
101+
uri="https://test",
102+
state="ACTIVE",
103+
video_metadata=dict(video_duration=datetime.timedelta(seconds=30)),
104+
error=dict(code=7, message="ok?"),
105+
)
106+
)
107+
108+
f = genai.upload_file(path="dummy")
109+
self.assertEqual(google.rpc.status_pb2.Status(code=7, message="ok?"), f.error)
110+
self.assertEqual(
111+
glm.VideoMetadata(dict(video_duration=datetime.timedelta(seconds=30))), f.video_metadata
112+
)
113+
114+
@parameterized.named_parameters(
115+
[
116+
dict(
117+
testcase_name="FileDataDict",
118+
file_data=dict(file_uri="https://test_uri"),
119+
),
120+
dict(
121+
testcase_name="FileDict",
122+
file_data=dict(uri="https://test_uri"),
123+
),
124+
dict(
125+
testcase_name="FileData",
126+
file_data=glm.FileData(file_uri="https://test_uri"),
127+
),
128+
dict(
129+
testcase_name="glm.File",
130+
file_data=glm.File(uri="https://test_uri"),
131+
),
132+
dict(
133+
testcase_name="file_types.File",
134+
file_data=file_types.File(dict(uri="https://test_uri")),
135+
),
136+
]
137+
)
138+
def test_to_file_data(self, file_data):
139+
file_data = file_types.to_file_data(file_data)
140+
self.assertEqual(glm.FileData(file_uri="https://test_uri"), file_data)

0 commit comments

Comments
 (0)