|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2023 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 | +from absl.testing import absltest |
| 16 | + |
| 17 | +import google |
| 18 | +import google.generativeai as genai |
| 19 | +import pathlib |
| 20 | + |
| 21 | +media = pathlib.Path(__file__).parents[1] / "third_party" |
| 22 | + |
| 23 | + |
| 24 | +class UnitTests(absltest.TestCase): |
| 25 | + def test_files_create(self): |
| 26 | + # [START files_create] |
| 27 | + myfile = genai.upload_file(media / "poem.txt") |
| 28 | + print(f"{myfile=}") |
| 29 | + print() |
| 30 | + |
| 31 | + model = genai.GenerativeModel("gemini-1.5-flash") |
| 32 | + result = model.generate_content( |
| 33 | + [myfile, "\n\n", "Can you add a few more lines to this poem?"] |
| 34 | + ) |
| 35 | + print(f"{result.text=}") |
| 36 | + print() |
| 37 | + # [END files_create] |
| 38 | + |
| 39 | + def test_files_create_video(self): |
| 40 | + # [START files_create_video] |
| 41 | + import time |
| 42 | + |
| 43 | + # Video clip from https://peach.blender.org/download/ |
| 44 | + myfile = genai.upload_file(media / "Big_Buck_Bunny.mp4") |
| 45 | + print(f"{myfile=}") |
| 46 | + print() |
| 47 | + |
| 48 | + while myfile.state.name == "PROCESSING": |
| 49 | + print("processing video...") |
| 50 | + time.sleep(5) |
| 51 | + myfile = genai.get_file(myfile.name) |
| 52 | + |
| 53 | + model = genai.GenerativeModel("gemini-1.5-flash") |
| 54 | + result = model.generate_content([myfile, "Describe this video clip"]) |
| 55 | + print() |
| 56 | + print(f"{result.text=}") |
| 57 | + print() |
| 58 | + # [END files_create_video] |
| 59 | + |
| 60 | + def test_files_list(self): |
| 61 | + # [START files_list] |
| 62 | + print("My files:") |
| 63 | + for f in genai.list_files(): |
| 64 | + print(" ", f.name) |
| 65 | + # [END files_list] |
| 66 | + |
| 67 | + def test_files_delete(self): |
| 68 | + # [START files_delete] |
| 69 | + myfile = genai.upload_file(media / "poem.txt") |
| 70 | + |
| 71 | + myfile.delete() |
| 72 | + |
| 73 | + try: |
| 74 | + # Error. |
| 75 | + model = genai.GenerativeModel("gemini-1.5-flash") |
| 76 | + result = model.generate_content([myfile, "Describe this file."]) |
| 77 | + except google.api_core.exceptions.PermissionDenied: |
| 78 | + pass |
| 79 | + # [END files_delete] |
| 80 | + |
| 81 | + def test_files_get(self): |
| 82 | + # [START files_get] |
| 83 | + myfile = genai.upload_file(media / "poem.txt") |
| 84 | + file_name = myfile.name |
| 85 | + print(file_name) # "files/*" |
| 86 | + |
| 87 | + myfile = genai.get_file(file_name) |
| 88 | + print(myfile) |
| 89 | + # [END files_get] |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + absltest.main() |
0 commit comments