1
+ set -eu
2
+
3
+ SCRIPT_DIR=$( dirname " $0 " )
4
+ MEDIA_DIR=$( realpath ${SCRIPT_DIR} /../../third_party)
5
+
6
+ TEXT_PATH=${MEDIA_DIR} /poem.txt
7
+ IMG_PATH=${MEDIA_DIR} /organ.jpg
8
+ IMG_PATH_2=${MEDIA_DIR} /Cajun_instruments.jpg
9
+ AUDIO_PATH=${MEDIA_DIR} /sample.mp3
10
+ VIDEO_PATH=${MEDIA_DIR} /Big_Buck_Bunny.mp4
11
+
12
+ BASE_URL=" https://generativelanguage.googleapis.com"
13
+
14
+ echo " [START files_create_text]"
15
+ # [START files_create_text]
16
+ MIME_TYPE=$( file -b --mime-type " ${TEXT_PATH} " )
17
+ NUM_BYTES=$( wc -c < " ${TEXT_PATH} " )
18
+ DISPLAY_NAME=TEXT
19
+
20
+ tmp_header_file=upload-header.tmp
21
+
22
+ # Initial resumable request defining metadata.
23
+ # The upload url is in the response headers dump them to a file.
24
+ curl " ${BASE_URL} /upload/v1beta/files?key=${GOOGLE_API_KEY} " \
25
+ -D upload-header.tmp \
26
+ -H " X-Goog-Upload-Protocol: resumable" \
27
+ -H " X-Goog-Upload-Command: start" \
28
+ -H " X-Goog-Upload-Header-Content-Length: ${NUM_BYTES} " \
29
+ -H " X-Goog-Upload-Header-Content-Type: ${MIME_TYPE} " \
30
+ -H " Content-Type: application/json" \
31
+ -d " {'file': {'display_name': '${DISPLAY_NAME} '}}" 2> /dev/null
32
+
33
+ upload_url=$( grep -i " x-goog-upload-url: " " ${tmp_header_file} " | cut -d" " -f2 | tr -d " \r" )
34
+ rm " ${tmp_header_file} "
35
+
36
+ # Upload the actual bytes.
37
+ curl " ${upload_url} " \
38
+ -H " Content-Length: ${NUM_BYTES} " \
39
+ -H " X-Goog-Upload-Offset: 0" \
40
+ -H " X-Goog-Upload-Command: upload, finalize" \
41
+ --data-binary " @${TEXT_PATH} " 2> /dev/null > file_info.json
42
+
43
+ file_uri=$( jq " .file.uri" file_info.json)
44
+ echo file_uri=$file_uri
45
+
46
+ # Now generate content using that file
47
+ curl " https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY " \
48
+ -H ' Content-Type: application/json' \
49
+ -X POST \
50
+ -d ' {
51
+ "contents": [{
52
+ "parts":[
53
+ {"text": "Can you add a few more lines to this poem?"},
54
+ {"file_data":{"mime_type": "text/plain", "file_uri": ' $file_uri ' }}]
55
+ }]
56
+ }' 2> /dev/null > response.json
57
+
58
+ cat response.json
59
+ echo
60
+
61
+ jq " .candidates[].content.parts[].text" response.json
62
+
63
+ echo " [START files_get]"
64
+ # [START files_get]
65
+ name=$( jq " .file.name" file_info.json)
66
+ # Get the file of interest to check state
67
+ curl https://generativelanguage.googleapis.com/v1beta/files/$name > file_info.json
68
+ # Print some information about the file you got
69
+ name=$( jq " .file.name" file_info.json)
70
+ echo name=$name
71
+ file_uri=$( jq " .file.uri" file_info.json)
72
+ echo file_uri=$file_uri
73
+ # [END files_get]
74
+
75
+ echo " [START files_delete]"
76
+ # [START files_delete]
77
+ curl --request " DELETE" https://generativelanguage.googleapis.com/v1beta/files/$name ? key=$GOOGLE_API_KEY
78
+ # [END files_delete]
79
+
80
+ # [END files_create_text]
81
+
82
+ echo " [START files_create_image]"
83
+ # [START files_create_image]
84
+ MIME_TYPE=$( file -b --mime-type " ${IMG_PATH_2} " )
85
+ NUM_BYTES=$( wc -c < " ${IMG_PATH_2} " )
86
+ DISPLAY_NAME=TEXT
87
+
88
+ tmp_header_file=upload-header.tmp
89
+
90
+ # Initial resumable request defining metadata.
91
+ # The upload url is in the response headers dump them to a file.
92
+ curl " ${BASE_URL} /upload/v1beta/files?key=${GOOGLE_API_KEY} " \
93
+ -D upload-header.tmp \
94
+ -H " X-Goog-Upload-Protocol: resumable" \
95
+ -H " X-Goog-Upload-Command: start" \
96
+ -H " X-Goog-Upload-Header-Content-Length: ${NUM_BYTES} " \
97
+ -H " X-Goog-Upload-Header-Content-Type: ${MIME_TYPE} " \
98
+ -H " Content-Type: application/json" \
99
+ -d " {'file': {'display_name': '${DISPLAY_NAME} '}}" 2> /dev/null
100
+
101
+ upload_url=$( grep -i " x-goog-upload-url: " " ${tmp_header_file} " | cut -d" " -f2 | tr -d " \r" )
102
+ rm " ${tmp_header_file} "
103
+
104
+ # Upload the actual bytes.
105
+ curl " ${upload_url} " \
106
+ -H " Content-Length: ${NUM_BYTES} " \
107
+ -H " X-Goog-Upload-Offset: 0" \
108
+ -H " X-Goog-Upload-Command: upload, finalize" \
109
+ --data-binary " @${IMG_PATH_2} " 2> /dev/null > file_info.json
110
+
111
+ file_uri=$( jq " .file.uri" file_info.json)
112
+ echo file_uri=$file_uri
113
+
114
+ # Now generate content using that file
115
+ curl " https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY " \
116
+ -H ' Content-Type: application/json' \
117
+ -X POST \
118
+ -d ' {
119
+ "contents": [{
120
+ "parts":[
121
+ {"text": "Can you tell me about the instruments in this photo?"},
122
+ {"file_data":
123
+ {"mime_type": "image/jpeg",
124
+ "file_uri": ' $file_uri ' }
125
+ }]
126
+ }]
127
+ }' 2> /dev/null > response.json
128
+
129
+ cat response.json
130
+ echo
131
+
132
+ jq " .candidates[].content.parts[].text" response.json
133
+ # [END files_create_image]
134
+
135
+ echo " [START files_create_audio]"
136
+ # [START files_create_audio]
137
+ MIME_TYPE=$( file -b --mime-type " ${AUDIO_PATH} " )
138
+ NUM_BYTES=$( wc -c < " ${AUDIO_PATH} " )
139
+ DISPLAY_NAME=AUDIO
140
+
141
+ tmp_header_file=upload-header.tmp
142
+
143
+ # Initial resumable request defining metadata.
144
+ # The upload url is in the response headers dump them to a file.
145
+ curl " ${BASE_URL} /upload/v1beta/files?key=${GOOGLE_API_KEY} " \
146
+ -D upload-header.tmp \
147
+ -H " X-Goog-Upload-Protocol: resumable" \
148
+ -H " X-Goog-Upload-Command: start" \
149
+ -H " X-Goog-Upload-Header-Content-Length: ${NUM_BYTES} " \
150
+ -H " X-Goog-Upload-Header-Content-Type: ${MIME_TYPE} " \
151
+ -H " Content-Type: application/json" \
152
+ -d " {'file': {'display_name': '${DISPLAY_NAME} '}}" 2> /dev/null
153
+
154
+ upload_url=$( grep -i " x-goog-upload-url: " " ${tmp_header_file} " | cut -d" " -f2 | tr -d " \r" )
155
+ rm " ${tmp_header_file} "
156
+
157
+ # Upload the actual bytes.
158
+ curl " ${upload_url} " \
159
+ -H " Content-Length: ${NUM_BYTES} " \
160
+ -H " X-Goog-Upload-Offset: 0" \
161
+ -H " X-Goog-Upload-Command: upload, finalize" \
162
+ --data-binary " @${AUDIO_PATH} " 2> /dev/null > file_info.json
163
+
164
+ file_uri=$( jq " .file.uri" file_info.json)
165
+ echo file_uri=$file_uri
166
+
167
+ # Now generate content using that file
168
+ curl " https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY " \
169
+ -H ' Content-Type: application/json' \
170
+ -X POST \
171
+ -d ' {
172
+ "contents": [{
173
+ "parts":[
174
+ {"text": "Describe this audio clip"},
175
+ {"file_data":{"mime_type": "audio/mp3", "file_uri": ' $file_uri ' }}]
176
+ }]
177
+ }' 2> /dev/null > response.json
178
+
179
+ cat response.json
180
+ echo
181
+
182
+ jq " .candidates[].content.parts[].text" response.json
183
+ # [END files_create_audio]
184
+
185
+ echo " [START files_create_video]"
186
+ # [START files_create_video]
187
+ MIME_TYPE=$( file -b --mime-type " ${VIDEO_PATH} " )
188
+ NUM_BYTES=$( wc -c < " ${VIDEO_PATH} " )
189
+ DISPLAY_NAME=VIDEO_PATH
190
+
191
+ # Initial resumable request defining metadata.
192
+ # The upload url is in the response headers dump them to a file.
193
+ curl " ${BASE_URL} /upload/v1beta/files?key=${GOOGLE_API_KEY} " \
194
+ -D upload-header.tmp \
195
+ -H " X-Goog-Upload-Protocol: resumable" \
196
+ -H " X-Goog-Upload-Command: start" \
197
+ -H " X-Goog-Upload-Header-Content-Length: ${NUM_BYTES} " \
198
+ -H " X-Goog-Upload-Header-Content-Type: ${MIME_TYPE} " \
199
+ -H " Content-Type: application/json" \
200
+ -d " {'file': {'display_name': '${DISPLAY_NAME} '}}" 2> /dev/null
201
+
202
+ upload_url=$( grep -i " x-goog-upload-url: " " ${tmp_header_file} " | cut -d" " -f2 | tr -d " \r" )
203
+ rm " ${tmp_header_file} "
204
+
205
+ # Upload the actual bytes.
206
+ curl " ${upload_url} " \
207
+ -H " Content-Length: ${NUM_BYTES} " \
208
+ -H " X-Goog-Upload-Offset: 0" \
209
+ -H " X-Goog-Upload-Command: upload, finalize" \
210
+ --data-binary " @${VIDEO_PATH} " 2> /dev/null > file_info.json
211
+
212
+ file_uri=$( jq " .file.uri" file_info.json)
213
+ echo file_uri=$file_uri
214
+
215
+ state=$( jq " .file.state" file_info.json)
216
+ echo state=$state
217
+
218
+ # Ensure the state of the video is 'ACTIVE'
219
+ while [[ " ($state )" = * " PROCESSING" * ]];
220
+ do
221
+ echo " Processing video..."
222
+ sleep 5
223
+ # Get the file of interest to check state
224
+ curl https://generativelanguage.googleapis.com/v1beta/files/$name > file_info.json
225
+ state=$( jq " .file.state" file_info.json)
226
+ done
227
+
228
+ # Now generate content using that file
229
+ curl " https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY " \
230
+ -H ' Content-Type: application/json' \
231
+ -X POST \
232
+ -d ' {
233
+ "contents": [{
234
+ "parts":[
235
+ {"text": "Describe this video clip"},
236
+ {"file_data":{"mime_type": "video/mp4", "file_uri": ' $file_uri ' }}]
237
+ }]
238
+ }' 2> /dev/null > response.json
239
+
240
+ cat response.json
241
+ echo
242
+
243
+ jq " .candidates[].content.parts[].text" response.json
244
+ # [END files_create_video]
245
+
246
+ echo " [START files_list]"
247
+ # [START files_list]
248
+ echo " My files: "
249
+
250
+ curl " https://generativelanguage.googleapis.com/v1beta/files?key=$GOOGLE_API_KEY "
251
+ # [END files_list]
0 commit comments