Skip to content

Commit f8b049f

Browse files
authored
Add pdf samples (#484)
* Add pdf samples Change-Id: I835c4805081af3aa6ce26a8871a62b5c435f18bf * Fix streaming video Change-Id: Iec0000da192231a7a5f97faabaeae9d3ebe64475 * format Change-Id: I51705e0f3b96d825952a3183bc55cbed5cb158c0
1 parent 353dc4f commit f8b049f

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

samples/count_tokens.py

+11
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@ def test_tokens_multimodal_video_audio_file_api(self):
167167

168168
# [END tokens_multimodal_video_audio_file_api]
169169

170+
def test_tokens_multimodal_pdf_file_api(self):
171+
# [START tokens_multimodal_pdf_file_api]
172+
model = genai.GenerativeModel("gemini-1.5-flash")
173+
sample_pdf = genai.upload_file(media / "test.pdf")
174+
token_count = model.count_tokens(["Give me a summary of this document.", sample_pdf])
175+
print(f"{token_count=}")
176+
177+
response = model.generate_content(["Give me a summary of this document.", sample_pdf])
178+
print(response.usage_metadata)
179+
# [END tokens_multimodal_pdf_file_api]
180+
170181
def test_tokens_cached_content(self):
171182
# [START tokens_cached_content]
172183
import time

samples/files.py

+8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ def test_files_create_video(self):
7575
print(f"{result.text=}")
7676
# [END files_create_video]
7777

78+
def test_files_create_pdf(self):
79+
# [START files_create_pdf]
80+
model = genai.GenerativeModel("gemini-1.5-flash")
81+
sample_pdf = genai.upload_file(media / "test.pdf")
82+
response = model.generate_content(["Give me a summary of this pdf file.", sample_pdf])
83+
print(response.text)
84+
# [END files_create_pdf]
85+
7886
def test_files_list(self):
7987
# [START files_list]
8088
print("My files:")

samples/text_generation.py

+46-4
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ def test_text_gen_multimodal_audio(self):
9696
print(response.text)
9797
# [END text_gen_multimodal_audio]
9898

99+
def test_text_gen_multimodal_audio_streaming(self):
100+
# [START text_gen_multimodal_audio_streaming]
101+
model = genai.GenerativeModel("gemini-1.5-flash")
102+
sample_audio = genai.upload_file(media / "sample.mp3")
103+
response = model.generate_content(["Give me a summary of this audio file.", sample_audio])
104+
105+
for chunk in response:
106+
print(chunk.text)
107+
print("_" * 80)
108+
# [END text_gen_multimodal_audio_streaming]
109+
99110
def test_text_gen_multimodal_video_prompt(self):
100111
# [START text_gen_multimodal_video_prompt]
101112
import time
@@ -111,20 +122,51 @@ def test_text_gen_multimodal_video_prompt(self):
111122
myfile = genai.get_file(myfile.name)
112123

113124
model = genai.GenerativeModel("gemini-1.5-flash")
114-
result = model.generate_content([myfile, "Describe this video clip"])
115-
print(f"{result.text=}")
125+
response = model.generate_content([myfile, "Describe this video clip"])
126+
print(f"{response.text=}")
116127
# [END text_gen_multimodal_video_prompt]
117128

118129
def test_text_gen_multimodal_video_prompt_streaming(self):
119130
# [START text_gen_multimodal_video_prompt_streaming]
131+
import time
132+
133+
# Video clip (CC BY 3.0) from https://peach.blender.org/download/
134+
myfile = genai.upload_file(media / "Big_Buck_Bunny.mp4")
135+
print(f"{myfile=}")
136+
137+
# Videos need to be processed before you can use them.
138+
while myfile.state.name == "PROCESSING":
139+
print("processing video...")
140+
time.sleep(5)
141+
myfile = genai.get_file(myfile.name)
142+
120143
model = genai.GenerativeModel("gemini-1.5-flash")
121-
video = genai.upload_file(media / "Big_Buck_Bunny.mp4")
122-
response = model.generate_content(["Describe this video clip.", video], stream=True)
144+
145+
response = model.generate_content([myfile, "Describe this video clip"])
123146
for chunk in response:
124147
print(chunk.text)
125148
print("_" * 80)
126149
# [END text_gen_multimodal_video_prompt_streaming]
127150

151+
def test_text_gen_multimodal_pdf(self):
152+
# [START text_gen_multimodal_pdf]
153+
model = genai.GenerativeModel("gemini-1.5-flash")
154+
sample_pdf = genai.upload_file(media / "test.pdf")
155+
response = model.generate_content(["Give me a summary of this document:", sample_pdf])
156+
print(f"{response.text=}")
157+
# [END text_gen_multimodal_pdf]
158+
159+
def test_text_gen_multimodal_pdf_streaming(self):
160+
# [START text_gen_multimodal_pdf_streaming]
161+
model = genai.GenerativeModel("gemini-1.5-flash")
162+
sample_pdf = genai.upload_file(media / "test.pdf")
163+
response = model.generate_content(["Give me a summary of this document:", sample_pdf])
164+
165+
for chunk in response:
166+
print(chunk.text)
167+
print("_" * 80)
168+
# [END text_gen_multimodal_pdf_streaming]
169+
128170

129171
if __name__ == "__main__":
130172
absltest.main()

third_party/LICENSE.txt

+3
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@
88
* This is the first paragraph from Shakespeare's "spring", public domain.
99
* Cajun_instruments.jpg
1010
* This image is from Wikimedia Commons, a public domain (https://commons.wikimedia.org/wiki/Category:Musical_instruments#/media/File:Cajun_instruments.jpg).
11+
* test.pdf
12+
* This is the first 2 pages of https://arxiv.org/abs/2403.05530 by Google Gemini Team.
13+
* License: CC-BY 4.0

third_party/test.pdf

802 KB
Binary file not shown.

0 commit comments

Comments
 (0)