|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -import google.generativeai as genai |
| 15 | +from google import genai |
16 | 16 | import os
|
17 | 17 | from dotenv import load_dotenv
|
18 | 18 |
|
|
21 | 21 | api_key = os.environ["GOOGLE_API_KEY"]
|
22 | 22 |
|
23 | 23 | # Initialize Google API Client
|
24 |
| -genai.configure(api_key=api_key) |
| 24 | +client=genai.Client(api_key=api_key) |
25 | 25 |
|
26 |
| -# Prepare file to upload to GenAI File API |
27 |
| -file_path = "sample_data/gemini_logo.png" |
| 26 | +# Upload a sample file to the client.files API |
| 27 | +file_path = "/content/image.png" |
28 | 28 | display_name = "Gemini Logo"
|
29 |
| -file_response = genai.upload_file(path=file_path, display_name=display_name) |
| 29 | +file_response = client.files.upload( |
| 30 | + file=open(file_path, "rb"), |
| 31 | + config={ |
| 32 | + "mime_type":"image/png", |
| 33 | + "display_name":display_name |
| 34 | + } |
| 35 | +) |
30 | 36 | print(f"Uploaded file {file_response.display_name} as: {file_response.uri}")
|
31 | 37 |
|
32 |
| -# Verify the file is uploaded to the API |
33 |
| -get_file = genai.get_file(name=file_response.name) |
| 38 | +# Retrieve the uploaded file from the client.files.get |
| 39 | +get_file =client.files.get(name=file_response.name) |
34 | 40 | print(f"Retrieved file {get_file.display_name} as: {get_file.uri}")
|
35 | 41 |
|
36 |
| -# Make Gemini 1.5 API LLM call |
| 42 | +# Generate content using the client.models API |
37 | 43 | prompt = "Describe the image with a creative description"
|
38 |
| -model_name = "models/gemini-2.0-flash" |
39 |
| -model = genai.GenerativeModel(model_name=model_name) |
40 |
| -response = model.generate_content([prompt, file_response]) |
41 |
| -print(response) |
| 44 | +model_name = "gemini-2.0-flash" |
| 45 | +response = client.models.generate_content( |
| 46 | + model=model_name, |
| 47 | + contents=[ |
| 48 | + prompt, |
| 49 | + file_response |
| 50 | + ] |
| 51 | +) |
| 52 | +print(response.text) |
42 | 53 |
|
43 | 54 | # Delete the sample file
|
44 |
| -genai.delete_file(name=file_response.name) |
| 55 | +client.files.delete(name=file_response.name) |
45 | 56 | print(f"Deleted file {file_response.display_name}")
|
0 commit comments