|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2025 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 pathlib |
| 18 | + |
| 19 | +media = pathlib.Path(__file__).parents[1] / "third_party" |
| 20 | + |
| 21 | + |
| 22 | +class UnitTests(absltest.TestCase): |
| 23 | + def test_text_gen_text_only_prompt(self): |
| 24 | + # [START text_gen_text_only_prompt] |
| 25 | + from google import genai |
| 26 | + |
| 27 | + client = genai.Client() |
| 28 | + response = client.models.generate_content( |
| 29 | + model="gemini-2.0-flash", |
| 30 | + contents="Write a story about a magic backpack." |
| 31 | + ) |
| 32 | + print(response.text) |
| 33 | + # [END text_gen_text_only_prompt] |
| 34 | + |
| 35 | + def test_text_gen_text_only_prompt_streaming(self): |
| 36 | + # [START text_gen_text_only_prompt_streaming] |
| 37 | + from google import genai |
| 38 | + |
| 39 | + client = genai.Client() |
| 40 | + response = client.models.generate_content_stream( |
| 41 | + model="gemini-2.0-flash", |
| 42 | + contents="Write a story about a magic backpack." |
| 43 | + ) |
| 44 | + for chunk in response: |
| 45 | + print(chunk.text) |
| 46 | + print("_" * 80) |
| 47 | + # [END text_gen_text_only_prompt_streaming] |
| 48 | + |
| 49 | + def test_text_gen_multimodal_one_image_prompt(self): |
| 50 | + # [START text_gen_multimodal_one_image_prompt] |
| 51 | + from google import genai |
| 52 | + |
| 53 | + import PIL.Image |
| 54 | + |
| 55 | + client = genai.Client() |
| 56 | + organ = PIL.Image.open(media / "organ.jpg") |
| 57 | + response = client.models.generate_content( |
| 58 | + model="gemini-2.0-flash", |
| 59 | + contents=["Tell me about this instrument", organ] |
| 60 | + ) |
| 61 | + print(response.text) |
| 62 | + # [END text_gen_multimodal_one_image_prompt] |
| 63 | + |
| 64 | + def test_text_gen_multimodal_one_image_prompt_streaming(self): |
| 65 | + # [START text_gen_multimodal_one_image_prompt_streaming] |
| 66 | + from google import genai |
| 67 | + |
| 68 | + import PIL.Image |
| 69 | + |
| 70 | + client = genai.Client() |
| 71 | + organ = PIL.Image.open(media / "organ.jpg") |
| 72 | + response = client.models.generate_content_stream( |
| 73 | + model="gemini-2.0-flash", |
| 74 | + contents=["Tell me about this instrument", organ] |
| 75 | + ) |
| 76 | + for chunk in response: |
| 77 | + print(chunk.text) |
| 78 | + print("_" * 80) |
| 79 | + # [END text_gen_multimodal_one_image_prompt_streaming] |
| 80 | + |
| 81 | + def test_text_gen_multimodal_multi_image_prompt(self): |
| 82 | + # [START text_gen_multimodal_multi_image_prompt] |
| 83 | + from google import genai |
| 84 | + |
| 85 | + import PIL.Image |
| 86 | + |
| 87 | + client = genai.Client() |
| 88 | + organ = PIL.Image.open(media / "organ.jpg") |
| 89 | + cajun_instrument = PIL.Image.open(media / "Cajun_instruments.jpg") |
| 90 | + response = client.models.generate_content( |
| 91 | + model="gemini-2.0-flash", |
| 92 | + contents=["What is the difference between both of these instruments?", organ, cajun_instrument] |
| 93 | + ) |
| 94 | + print(response.text) |
| 95 | + # [END text_gen_multimodal_multi_image_prompt] |
| 96 | + |
| 97 | + def test_text_gen_multimodal_multi_image_prompt_streaming(self): |
| 98 | + # [START text_gen_multimodal_multi_image_prompt_streaming] |
| 99 | + from google import genai |
| 100 | + |
| 101 | + import PIL.Image |
| 102 | + |
| 103 | + client = genai.Client() |
| 104 | + organ = PIL.Image.open(media / "organ.jpg") |
| 105 | + cajun_instrument = PIL.Image.open(media / "Cajun_instruments.jpg") |
| 106 | + response = client.models.generate_content_stream( |
| 107 | + model="gemini-2.0-flash", |
| 108 | + contents=["What is the difference between both of these instruments?", organ, cajun_instrument] |
| 109 | + ) |
| 110 | + for chunk in response: |
| 111 | + print(chunk.text) |
| 112 | + print("_" * 80) |
| 113 | + # [END text_gen_multimodal_multi_image_prompt_streaming] |
| 114 | + |
| 115 | + def test_text_gen_multimodal_audio(self): |
| 116 | + # [START text_gen_multimodal_audio] |
| 117 | + from google import genai |
| 118 | + |
| 119 | + client = genai.Client() |
| 120 | + sample_audio = client.files.upload(file=media / "sample.mp3") |
| 121 | + response = client.models.generate_content( |
| 122 | + model="gemini-2.0-flash", |
| 123 | + contents=["Give me a summary of this audio file.", sample_audio] |
| 124 | + ) |
| 125 | + print(response.text) |
| 126 | + # [END text_gen_multimodal_audio] |
| 127 | + |
| 128 | + def test_text_gen_multimodal_audio_streaming(self): |
| 129 | + # [START text_gen_multimodal_audio_streaming] |
| 130 | + from google import genai |
| 131 | + |
| 132 | + client = genai.Client() |
| 133 | + sample_audio = client.files.upload(file=media / "sample.mp3") |
| 134 | + response = client.models.generate_content_stream( |
| 135 | + model="gemini-2.0-flash", |
| 136 | + contents=["Give me a summary of this audio file.", sample_audio] |
| 137 | + ) |
| 138 | + for chunk in response: |
| 139 | + print(chunk.text) |
| 140 | + print("_" * 80) |
| 141 | + # [END text_gen_multimodal_audio_streaming] |
| 142 | + |
| 143 | + def test_text_gen_multimodal_video_prompt(self): |
| 144 | + # [START text_gen_multimodal_video_prompt] |
| 145 | + from google import genai |
| 146 | + |
| 147 | + import time |
| 148 | + |
| 149 | + client = genai.Client() |
| 150 | + # Video clip (CC BY 3.0) from https://peach.blender.org/download/ |
| 151 | + myfile = client.files.upload(file=media / "Big_Buck_Bunny.mp4") |
| 152 | + print(f"{myfile=}") |
| 153 | + |
| 154 | + # Videos need to be processed before you can use them. |
| 155 | + while myfile.state.name == "PROCESSING": |
| 156 | + print("processing video...") |
| 157 | + time.sleep(5) |
| 158 | + myfile = client.files.get(name=myfile.name) |
| 159 | + |
| 160 | + response = client.models.generate_content( |
| 161 | + model="gemini-2.0-flash", |
| 162 | + contents=[myfile, "Describe this video clip"] |
| 163 | + ) |
| 164 | + print(f"{response.text=}") |
| 165 | + # [END text_gen_multimodal_video_prompt] |
| 166 | + |
| 167 | + def test_text_gen_multimodal_video_prompt_streaming(self): |
| 168 | + # [START text_gen_multimodal_video_prompt_streaming] |
| 169 | + from google import genai |
| 170 | + |
| 171 | + import time |
| 172 | + |
| 173 | + client = genai.Client() |
| 174 | + # Video clip (CC BY 3.0) from https://peach.blender.org/download/ |
| 175 | + myfile = client.files.upload(file=media / "Big_Buck_Bunny.mp4") |
| 176 | + print(f"{myfile=}") |
| 177 | + |
| 178 | + # Videos need to be processed before you can use them. |
| 179 | + while myfile.state.name == "PROCESSING": |
| 180 | + print("processing video...") |
| 181 | + time.sleep(5) |
| 182 | + myfile = client.files.get(name=myfile.name) |
| 183 | + |
| 184 | + response = client.models.generate_content_stream( |
| 185 | + model="gemini-2.0-flash", |
| 186 | + contents=[myfile, "Describe this video clip"] |
| 187 | + ) |
| 188 | + for chunk in response: |
| 189 | + print(chunk.text) |
| 190 | + print("_" * 80) |
| 191 | + # [END text_gen_multimodal_video_prompt_streaming] |
| 192 | + |
| 193 | + def test_text_gen_multimodal_pdf(self): |
| 194 | + # [START text_gen_multimodal_pdf] |
| 195 | + from google import genai |
| 196 | + |
| 197 | + client = genai.Client() |
| 198 | + sample_pdf = client.files.upload(file=media / "test.pdf") |
| 199 | + response = client.models.generate_content( |
| 200 | + model="gemini-2.0-flash", |
| 201 | + contents=["Give me a summary of this document:", sample_pdf] |
| 202 | + ) |
| 203 | + print(f"{response.text=}") |
| 204 | + # [END text_gen_multimodal_pdf] |
| 205 | + |
| 206 | + def test_text_gen_multimodal_pdf_streaming(self): |
| 207 | + # [START text_gen_multimodal_pdf_streaming] |
| 208 | + from google import genai |
| 209 | + |
| 210 | + client = genai.Client() |
| 211 | + sample_pdf = client.files.upload(file=media / "test.pdf") |
| 212 | + response = client.models.generate_content_stream( |
| 213 | + model="gemini-2.0-flash", |
| 214 | + contents=["Give me a summary of this document:", sample_pdf] |
| 215 | + ) |
| 216 | + |
| 217 | + for chunk in response: |
| 218 | + print(chunk.text) |
| 219 | + print("_" * 80) |
| 220 | + # [END text_gen_multimodal_pdf_streaming] |
| 221 | + |
| 222 | + |
| 223 | +if __name__ == "__main__": |
| 224 | + absltest.main() |
0 commit comments