-
Notifications
You must be signed in to change notification settings - Fork 460
Added curl examples for files #480
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
Changes from all commits
Commits
Show all changes
4 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,251 @@ | ||
set -eu | ||
|
||
SCRIPT_DIR=$(dirname "$0") | ||
MEDIA_DIR=$(realpath ${SCRIPT_DIR}/../../third_party) | ||
|
||
TEXT_PATH=${MEDIA_DIR}/poem.txt | ||
IMG_PATH=${MEDIA_DIR}/organ.jpg | ||
IMG_PATH_2=${MEDIA_DIR}/Cajun_instruments.jpg | ||
AUDIO_PATH=${MEDIA_DIR}/sample.mp3 | ||
VIDEO_PATH=${MEDIA_DIR}/Big_Buck_Bunny.mp4 | ||
|
||
BASE_URL="https://generativelanguage.googleapis.com" | ||
|
||
echo "[START files_create_text]" | ||
# [START files_create_text] | ||
MIME_TYPE=$(file -b --mime-type "${TEXT_PATH}") | ||
NUM_BYTES=$(wc -c < "${TEXT_PATH}") | ||
DISPLAY_NAME=TEXT | ||
|
||
tmp_header_file=upload-header.tmp | ||
|
||
# Initial resumable request defining metadata. | ||
# The upload url is in the response headers dump them to a file. | ||
curl "${BASE_URL}/upload/v1beta/files?key=${GOOGLE_API_KEY}" \ | ||
-D upload-header.tmp \ | ||
-H "X-Goog-Upload-Protocol: resumable" \ | ||
-H "X-Goog-Upload-Command: start" \ | ||
-H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \ | ||
-H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \ | ||
-H "Content-Type: application/json" \ | ||
-d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null | ||
|
||
upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r") | ||
rm "${tmp_header_file}" | ||
|
||
# Upload the actual bytes. | ||
curl "${upload_url}" \ | ||
-H "Content-Length: ${NUM_BYTES}" \ | ||
-H "X-Goog-Upload-Offset: 0" \ | ||
-H "X-Goog-Upload-Command: upload, finalize" \ | ||
--data-binary "@${TEXT_PATH}" 2> /dev/null > file_info.json | ||
|
||
file_uri=$(jq ".file.uri" file_info.json) | ||
echo file_uri=$file_uri | ||
|
||
# Now generate content using that file | ||
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY" \ | ||
-H 'Content-Type: application/json' \ | ||
-X POST \ | ||
-d '{ | ||
"contents": [{ | ||
"parts":[ | ||
{"text": "Can you add a few more lines to this poem?"}, | ||
{"file_data":{"mime_type": "text/plain", "file_uri": '$file_uri'}}] | ||
}] | ||
}' 2> /dev/null > response.json | ||
|
||
cat response.json | ||
echo | ||
|
||
jq ".candidates[].content.parts[].text" response.json | ||
|
||
echo "[START files_get]" | ||
# [START files_get] | ||
name=$(jq ".file.name" file_info.json) | ||
# Get the file of interest to check state | ||
curl https://generativelanguage.googleapis.com/v1beta/files/$name > file_info.json | ||
# Print some information about the file you got | ||
name=$(jq ".file.name" file_info.json) | ||
echo name=$name | ||
file_uri=$(jq ".file.uri" file_info.json) | ||
echo file_uri=$file_uri | ||
# [END files_get] | ||
|
||
echo "[START files_delete]" | ||
# [START files_delete] | ||
curl --request "DELETE" https://generativelanguage.googleapis.com/v1beta/files/$name?key=$GOOGLE_API_KEY | ||
# [END files_delete] | ||
|
||
# [END files_create_text] | ||
|
||
echo "[START files_create_image]" | ||
# [START files_create_image] | ||
MIME_TYPE=$(file -b --mime-type "${IMG_PATH_2}") | ||
NUM_BYTES=$(wc -c < "${IMG_PATH_2}") | ||
DISPLAY_NAME=TEXT | ||
|
||
tmp_header_file=upload-header.tmp | ||
|
||
# Initial resumable request defining metadata. | ||
# The upload url is in the response headers dump them to a file. | ||
curl "${BASE_URL}/upload/v1beta/files?key=${GOOGLE_API_KEY}" \ | ||
-D upload-header.tmp \ | ||
-H "X-Goog-Upload-Protocol: resumable" \ | ||
-H "X-Goog-Upload-Command: start" \ | ||
-H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \ | ||
-H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \ | ||
-H "Content-Type: application/json" \ | ||
-d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null | ||
|
||
upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r") | ||
rm "${tmp_header_file}" | ||
|
||
# Upload the actual bytes. | ||
curl "${upload_url}" \ | ||
-H "Content-Length: ${NUM_BYTES}" \ | ||
-H "X-Goog-Upload-Offset: 0" \ | ||
-H "X-Goog-Upload-Command: upload, finalize" \ | ||
--data-binary "@${IMG_PATH_2}" 2> /dev/null > file_info.json | ||
|
||
file_uri=$(jq ".file.uri" file_info.json) | ||
echo file_uri=$file_uri | ||
|
||
# Now generate content using that file | ||
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY" \ | ||
-H 'Content-Type: application/json' \ | ||
-X POST \ | ||
-d '{ | ||
"contents": [{ | ||
"parts":[ | ||
{"text": "Can you tell me about the instruments in this photo?"}, | ||
{"file_data": | ||
{"mime_type": "image/jpeg", | ||
"file_uri": '$file_uri'} | ||
}] | ||
}] | ||
}' 2> /dev/null > response.json | ||
|
||
cat response.json | ||
echo | ||
|
||
jq ".candidates[].content.parts[].text" response.json | ||
# [END files_create_image] | ||
|
||
echo "[START files_create_audio]" | ||
# [START files_create_audio] | ||
MIME_TYPE=$(file -b --mime-type "${AUDIO_PATH}") | ||
NUM_BYTES=$(wc -c < "${AUDIO_PATH}") | ||
DISPLAY_NAME=AUDIO | ||
|
||
tmp_header_file=upload-header.tmp | ||
|
||
# Initial resumable request defining metadata. | ||
# The upload url is in the response headers dump them to a file. | ||
curl "${BASE_URL}/upload/v1beta/files?key=${GOOGLE_API_KEY}" \ | ||
-D upload-header.tmp \ | ||
-H "X-Goog-Upload-Protocol: resumable" \ | ||
-H "X-Goog-Upload-Command: start" \ | ||
-H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \ | ||
-H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \ | ||
-H "Content-Type: application/json" \ | ||
-d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null | ||
|
||
upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r") | ||
rm "${tmp_header_file}" | ||
|
||
# Upload the actual bytes. | ||
curl "${upload_url}" \ | ||
-H "Content-Length: ${NUM_BYTES}" \ | ||
-H "X-Goog-Upload-Offset: 0" \ | ||
-H "X-Goog-Upload-Command: upload, finalize" \ | ||
--data-binary "@${AUDIO_PATH}" 2> /dev/null > file_info.json | ||
|
||
file_uri=$(jq ".file.uri" file_info.json) | ||
echo file_uri=$file_uri | ||
|
||
# Now generate content using that file | ||
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY" \ | ||
-H 'Content-Type: application/json' \ | ||
-X POST \ | ||
-d '{ | ||
"contents": [{ | ||
"parts":[ | ||
{"text": "Describe this audio clip"}, | ||
{"file_data":{"mime_type": "audio/mp3", "file_uri": '$file_uri'}}] | ||
}] | ||
}' 2> /dev/null > response.json | ||
|
||
cat response.json | ||
echo | ||
|
||
jq ".candidates[].content.parts[].text" response.json | ||
# [END files_create_audio] | ||
|
||
echo "[START files_create_video]" | ||
# [START files_create_video] | ||
MIME_TYPE=$(file -b --mime-type "${VIDEO_PATH}") | ||
NUM_BYTES=$(wc -c < "${VIDEO_PATH}") | ||
DISPLAY_NAME=VIDEO_PATH | ||
|
||
# Initial resumable request defining metadata. | ||
# The upload url is in the response headers dump them to a file. | ||
curl "${BASE_URL}/upload/v1beta/files?key=${GOOGLE_API_KEY}" \ | ||
-D upload-header.tmp \ | ||
-H "X-Goog-Upload-Protocol: resumable" \ | ||
-H "X-Goog-Upload-Command: start" \ | ||
-H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \ | ||
-H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \ | ||
-H "Content-Type: application/json" \ | ||
-d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null | ||
|
||
upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r") | ||
rm "${tmp_header_file}" | ||
|
||
# Upload the actual bytes. | ||
curl "${upload_url}" \ | ||
-H "Content-Length: ${NUM_BYTES}" \ | ||
-H "X-Goog-Upload-Offset: 0" \ | ||
-H "X-Goog-Upload-Command: upload, finalize" \ | ||
--data-binary "@${VIDEO_PATH}" 2> /dev/null > file_info.json | ||
|
||
file_uri=$(jq ".file.uri" file_info.json) | ||
echo file_uri=$file_uri | ||
shilpakancharla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
state=$(jq ".file.state" file_info.json) | ||
echo state=$state | ||
|
||
# Ensure the state of the video is 'ACTIVE' | ||
while [[ "($state)" = *"PROCESSING"* ]]; | ||
do | ||
echo "Processing video..." | ||
sleep 5 | ||
# Get the file of interest to check state | ||
curl https://generativelanguage.googleapis.com/v1beta/files/$name > file_info.json | ||
state=$(jq ".file.state" file_info.json) | ||
done | ||
|
||
# Now generate content using that file | ||
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY" \ | ||
-H 'Content-Type: application/json' \ | ||
-X POST \ | ||
-d '{ | ||
"contents": [{ | ||
"parts":[ | ||
{"text": "Describe this video clip"}, | ||
{"file_data":{"mime_type": "video/mp4", "file_uri": '$file_uri'}}] | ||
}] | ||
}' 2> /dev/null > response.json | ||
|
||
cat response.json | ||
echo | ||
|
||
jq ".candidates[].content.parts[].text" response.json | ||
# [END files_create_video] | ||
|
||
echo "[START files_list]" | ||
# [START files_list] | ||
echo "My files: " | ||
shilpakancharla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
curl "https://generativelanguage.googleapis.com/v1beta/files?key=$GOOGLE_API_KEY" | ||
# [END files_list] |
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.