Skip to content

Add create_audio and create_text to samples/files.py #414

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
merged 3 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions samples/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_chat(self):
]
)
response = chat.send_message("I have 2 dogs in my house.")
print(response.text)
print(response.text)
response = chat.send_message("How many paws are in my house?")
print(response.text)
# [END chat]
Expand Down Expand Up @@ -62,11 +62,14 @@ def test_chat_streaming_with_images(self):
model = genai.GenerativeModel("gemini-1.5-flash")
chat = model.start_chat()

response = chat.send_message("Hello, I'm interested in learning about musical instruments. Can I show you one?", stream=True)
response = chat.send_message(
"Hello, I'm interested in learning about musical instruments. Can I show you one?",
stream=True,
)
for chunk in response:
print(chunk.text) # Yes.
print("_" * 80)

organ = genai.upload_file(media / "organ.jpg")
response = chat.send_message(
["What family of intruments does this instrument belong to?", organ], stream=True
Expand Down
28 changes: 25 additions & 3 deletions samples/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@


class UnitTests(absltest.TestCase):
def test_files_create(self):
# [START files_create]
def test_files_create_text(self):
# [START files_create_text]
myfile = genai.upload_file(media / "poem.txt")
print(f"{myfile=}")

Expand All @@ -32,7 +32,29 @@ def test_files_create(self):
[myfile, "\n\n", "Can you add a few more lines to this poem?"]
)
print(f"{result.text=}")
# [END files_create]
# [END files_create_text]

def test_files_create_image(self):
# [START files_create_image]
myfile = genai.upload_file(media / "Cajun_instruments.jpg")
print(f"{myfile=}")

model = genai.GenerativeModel("gemini-1.5-flash")
result = model.generate_content(
[myfile, "\n\n", "Can you tell me about the instruments in this photo?"]
)
print(f"{result.text=}")
# [END files_create_image]

def test_files_create_audio(self):
# [START files_create_audio]
myfile = genai.upload_file(media / "sample.mp3")
print(f"{myfile=}")

model = genai.GenerativeModel("gemini-1.5-flash")
result = model.generate_content([myfile, "Describe this audio clip"])
print(f"{result.text=}")
# [END files_create_audio]

def test_files_create_video(self):
# [START files_create_video]
Expand Down
Loading