-
Notifications
You must be signed in to change notification settings - Fork 464
Adding text generation samples #405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MarkDaoust
merged 2 commits into
google-gemini:main
from
shilpakancharla:text_generation_samples
Jun 28, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2023 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import PIL.Image | ||
from absl.testing import absltest | ||
|
||
import google.generativeai as genai | ||
import pathlib | ||
|
||
media = pathlib.Path(__file__).parents[1] / "third_party" | ||
|
||
|
||
class UnitTests(absltest.TestCase): | ||
def test_text_gen_text_only_prompt(self): | ||
# [START text_gen_text_only_prompt] | ||
model = genai.GenerativeModel("gemini-1.5-flash") | ||
response = model.generate_content("Give me python code to sort a list") | ||
print(response.text) | ||
# [END text_gen_text_only_prompt] | ||
|
||
def test_text_gen_text_only_prompt_streaming(self): | ||
# [START text_gen_text_only_prompt_streaming] | ||
model = genai.GenerativeModel("gemini-1.5-flash") | ||
response = model.generate_content("Give me python code to sort a list", stream=True) | ||
for chunk in response: | ||
print(chunk.text) | ||
print("_" * 80) | ||
# [END text_gen_text_only_prompt_streaming] | ||
|
||
def test_text_gen_multimodal_one_image_prompt(self): | ||
# [START text_gen_multimodal_one_image_prompt] | ||
import PIL | ||
model = genai.GenerativeModel("gemini-1.5-flash") | ||
organ = PIL.Image.open(media / "organ.jpg") | ||
response = model.generate_content(["Tell me about this instrument", organ]) | ||
print(response.text) | ||
# [END text_gen_multimodal_one_image_prompt] | ||
|
||
def test_text_gen_multimodal_one_image_prompt_streaming(self): | ||
# [START text_gen_multimodal_one_image_prompt_streaming] | ||
import PIL | ||
model = genai.GenerativeModel("gemini-1.5-flash") | ||
organ = PIL.Image.open(media / "organ.jpg") | ||
response = model.generate_content(["Tell me about this instrument", organ], stream=True) | ||
for chunk in response: | ||
print(chunk.text) | ||
print("_" * 80) | ||
# [END text_gen_multimodal_one_image_prompt_streaming] | ||
|
||
def test_text_gen_multimodal_multi_image_prompt(self): | ||
# [START text_gen_multimodal_multi_image_prompt] | ||
import PIL | ||
model = genai.GenerativeModel("gemini-1.5-flash") | ||
organ = PIL.Image.open(media / "organ.jpg") | ||
cajun_instrument = PIL.Image.open(media / "Cajun_instruments.jpg") | ||
response = model.generate_content( | ||
["What is the difference between both of these instruments?", organ, cajun_instrument] | ||
) | ||
print(response.text) | ||
# [END text_gen_multimodal_multi_image_prompt] | ||
|
||
def test_text_gen_multimodal_multi_image_prompt_streaming(self): | ||
# [START text_gen_multimodal_multi_image_prompt_streaming] | ||
import PIL | ||
model = genai.GenerativeModel("gemini-1.5-flash") | ||
organ = PIL.Image.open(media / "organ.jpg") | ||
cajun_instrument = PIL.Image.open(media / "Cajun_instruments.jpg") | ||
response = model.generate_content( | ||
["What is the difference between both of these instruments?", organ, cajun_instrument], | ||
stream=True, | ||
) | ||
for chunk in response: | ||
print(chunk.text) | ||
print("_" * 80) | ||
# [END text_gen_multimodal_multi_image_prompt_streaming] | ||
|
||
def test_text_gen_multimodal_audio(self): | ||
# [START text_gen_multimodal_audio] | ||
model = genai.GenerativeModel("gemini-1.5-flash") | ||
sample_audio = genai.upload_file(media / "sample.mp3") | ||
response = model.generate_content(["Give me a summary of this audio file.", sample_audio]) | ||
print(response.text) | ||
# [END text_gen_multimodal_audio] | ||
|
||
def test_text_gen_multimodal_video_prompt(self): | ||
# [START text_gen_multimodal_video_prompt] | ||
model = genai.GenerativeModel("gemini-1.5-flash") | ||
video = genai.upload_file(media / "Big_Buck_Bunny.mp4") | ||
response = model.generate_content(["Describe this video clip.", video]) | ||
print(response.text) | ||
# [END text_gen_multimodal_video_prompt] | ||
|
||
def test_text_gen_multimodal_video_prompt_streaming(self): | ||
# [START text_gen_multimodal_video_prompt_streaming] | ||
model = genai.GenerativeModel("gemini-1.5-flash") | ||
video = genai.upload_file(media / "Big_Buck_Bunny.mp4") | ||
response = model.generate_content(["Describe this video clip.", video], stream=True) | ||
for chunk in response: | ||
print(chunk.text) | ||
print("_" * 80) | ||
# [END text_gen_multimodal_video_prompt_streaming] | ||
|
||
|
||
if __name__ == "__main__": | ||
absltest.main() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.