|
| 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 | +import PIL.Image |
| 16 | +from absl.testing import absltest |
| 17 | + |
| 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_text_gen_text_only_prompt(self): |
| 26 | + # [START text_gen_text_only_prompt] |
| 27 | + model = genai.GenerativeModel("gemini-1.5-flash") |
| 28 | + response = model.generate_content("Give me python code to sort a list") |
| 29 | + print(response.text) |
| 30 | + # [END text_gen_text_only_prompt] |
| 31 | + |
| 32 | + def test_text_gen_text_only_prompt_streaming(self): |
| 33 | + # [START text_gen_text_only_prompt_streaming] |
| 34 | + model = genai.GenerativeModel("gemini-1.5-flash") |
| 35 | + response = model.generate_content("Give me python code to sort a list", stream=True) |
| 36 | + for chunk in response: |
| 37 | + print(chunk.text) |
| 38 | + print("_" * 80) |
| 39 | + # [END text_gen_text_only_prompt_streaming] |
| 40 | + |
| 41 | + def test_text_gen_multimodal_one_image_prompt(self): |
| 42 | + # [START text_gen_multimodal_one_image_prompt] |
| 43 | + import PIL |
| 44 | + model = genai.GenerativeModel("gemini-1.5-flash") |
| 45 | + organ = PIL.Image.open(media / "organ.jpg") |
| 46 | + response = model.generate_content(["Tell me about this instrument", organ]) |
| 47 | + print(response.text) |
| 48 | + # [END text_gen_multimodal_one_image_prompt] |
| 49 | + |
| 50 | + def test_text_gen_multimodal_one_image_prompt_streaming(self): |
| 51 | + # [START text_gen_multimodal_one_image_prompt_streaming] |
| 52 | + import PIL |
| 53 | + model = genai.GenerativeModel("gemini-1.5-flash") |
| 54 | + organ = PIL.Image.open(media / "organ.jpg") |
| 55 | + response = model.generate_content(["Tell me about this instrument", organ], stream=True) |
| 56 | + for chunk in response: |
| 57 | + print(chunk.text) |
| 58 | + print("_" * 80) |
| 59 | + # [END text_gen_multimodal_one_image_prompt_streaming] |
| 60 | + |
| 61 | + def test_text_gen_multimodal_multi_image_prompt(self): |
| 62 | + # [START text_gen_multimodal_multi_image_prompt] |
| 63 | + import PIL |
| 64 | + model = genai.GenerativeModel("gemini-1.5-flash") |
| 65 | + organ = PIL.Image.open(media / "organ.jpg") |
| 66 | + cajun_instrument = PIL.Image.open(media / "Cajun_instruments.jpg") |
| 67 | + response = model.generate_content( |
| 68 | + ["What is the difference between both of these instruments?", organ, cajun_instrument] |
| 69 | + ) |
| 70 | + print(response.text) |
| 71 | + # [END text_gen_multimodal_multi_image_prompt] |
| 72 | + |
| 73 | + def test_text_gen_multimodal_multi_image_prompt_streaming(self): |
| 74 | + # [START text_gen_multimodal_multi_image_prompt_streaming] |
| 75 | + import PIL |
| 76 | + model = genai.GenerativeModel("gemini-1.5-flash") |
| 77 | + organ = PIL.Image.open(media / "organ.jpg") |
| 78 | + cajun_instrument = PIL.Image.open(media / "Cajun_instruments.jpg") |
| 79 | + response = model.generate_content( |
| 80 | + ["What is the difference between both of these instruments?", organ, cajun_instrument], |
| 81 | + stream=True, |
| 82 | + ) |
| 83 | + for chunk in response: |
| 84 | + print(chunk.text) |
| 85 | + print("_" * 80) |
| 86 | + # [END text_gen_multimodal_multi_image_prompt_streaming] |
| 87 | + |
| 88 | + def test_text_gen_multimodal_audio(self): |
| 89 | + # [START text_gen_multimodal_audio] |
| 90 | + model = genai.GenerativeModel("gemini-1.5-flash") |
| 91 | + sample_audio = genai.upload_file(media / "sample.mp3") |
| 92 | + response = model.generate_content(["Give me a summary of this audio file.", sample_audio]) |
| 93 | + print(response.text) |
| 94 | + # [END text_gen_multimodal_audio] |
| 95 | + |
| 96 | + def test_text_gen_multimodal_video_prompt(self): |
| 97 | + # [START text_gen_multimodal_video_prompt] |
| 98 | + model = genai.GenerativeModel("gemini-1.5-flash") |
| 99 | + video = genai.upload_file(media / "Big_Buck_Bunny.mp4") |
| 100 | + response = model.generate_content(["Describe this video clip.", video]) |
| 101 | + print(response.text) |
| 102 | + # [END text_gen_multimodal_video_prompt] |
| 103 | + |
| 104 | + def test_text_gen_multimodal_video_prompt_streaming(self): |
| 105 | + # [START text_gen_multimodal_video_prompt_streaming] |
| 106 | + model = genai.GenerativeModel("gemini-1.5-flash") |
| 107 | + video = genai.upload_file(media / "Big_Buck_Bunny.mp4") |
| 108 | + response = model.generate_content(["Describe this video clip.", video], stream=True) |
| 109 | + for chunk in response: |
| 110 | + print(chunk.text) |
| 111 | + print("_" * 80) |
| 112 | + # [END text_gen_multimodal_video_prompt_streaming] |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + absltest.main() |
0 commit comments