所有文章 > AI驱动 > AI实践之路:详解Gemini Pro API在Google Colab和本地Python中的应用策略
AI实践之路:详解Gemini Pro API在Google Colab和本地Python中的应用策略

AI实践之路:详解Gemini Pro API在Google Colab和本地Python中的应用策略

今天介绍下Gemini Pro API在colab和本地python运行的方案。

在Google Colab中使用Gemini模型进行自动聊天和日志记录

代码实现了以下功能:

  1. Google Cloud服务的认证和访问:
    • 使用auth.authenticate_user()进行Google Cloud服务的身份验证。
    • 通过google.colab.auth库,该代码确保用户已经通过Google账户登录并授权访问Google Cloud资源。
  2. 安装必要的库:
    • 使用!pip install命令安装了google-generativeai库,这是用于访问Google的Gemini模型的必要库。
  3. 挂载Google Drive:
    • 使用drive.mount('/content/drive')将用户的Google Drive挂载到Colab环境中。这使得代码可以直接读写Google Drive中的文件。
  4. 导入必要的库:
    • 导入了google.generativeaigoogle.colab模块。这些模块提供访问和操作Gemini模型以及用户数据的功能。
  5. 配置Gemini API密钥:
    • 设定了一个变量gemini_api_secret_name来存储API密钥的名称。
    • 使用userdata.get方法从Colab的用户数据中获取Gemini API密钥。
  6. 初始化Gemini模型:
    • 使用genai.GenerativeModel('gemini-pro')初始化Gemini模型,允许代码进行自然语言生成操作。
  7. 开始聊天会话:
    • 定义了一个空列表messages来存储聊天信息。
    • 使用一个无限循环,接受用户输入,并将其作为消息添加到messages列表中。
    • 使用Gemini模型生成响应,并将其打印出来。
    • 这个过程模仿了一个简单的聊天机器人。
  8. 聊天日志的记录:
    • 指定了一个路径chat_log_path,用于在Google Drive中保存聊天日志。
    • 在每次用户和模型交互后,代码将对话内容追加到指定的文件中,实现聊天记录的持久化。
# Authenticate and grant access to Google Cloud servicesfrom google.colab import authauth.authenticate_user()
# Install necessary libraries!pip install -q -U google-generativeai
# Mount Google Drivefrom google.colab import drivedrive.mount('/content/drive')
# Import necessary librariesimport google.generativeai as genaifrom google.colab import userdata
# Configure Gemini API keygemini_api_secret_name = 'GOOGLE_API_KEY' # Change this if your secret name is different
try: # Retrieve the Gemini API key from Colab's secrets GOOGLE_API_KEY = userdata.get(gemini_api_secret_name) genai.configure(api_key=GOOGLE_API_KEY)except userdata.SecretNotFoundError as e: print(f"Secret not found: Ensure you have a secret named {gemini_api_secret_name} in Colab.") raise eexcept Exception as e: print("There was an error retrieving the API key.") raise e
# Initialize the Gemini modelmodel = genai.GenerativeModel('gemini-pro')
# Start a chatmessages = []
# Specify the path for the chat log file in your Google Drivechat_log_path = '/content/drive/My Drive/chat_log.txt'
while True: message = input("You: ") messages.append({ "role": "user", "parts": [message], })
response = model.generate_content(messages)
# Check if response is valid if not response.parts or not response.parts[0].text: print("Gemini: [No response or the response does not contain text]") continue
response_text = response.parts[0].text
messages.append({ "role": "model", "parts": [response_text], })
print("Gemini: " + response_text)
# Append the conversation to the file with open(chat_log_path, 'a') as file: file.write(f"You: {message}\n") file.write(f"Gemini: {response_text}\n")

上面的代码支持持续对话,且可以帮对话保存到谷歌云盘里。

在Google Colab中使用Gemini模型进行图像分析和处理

代码的主要功能是使用Google Colab环境和Gemini模型对上传的图像进行分析。具体步骤和功能如下:

  1. Google Cloud服务的认证:
    • 通过auth.authenticate_user()进行Google Cloud服务的身份验证,确保用户已经通过Google账户登录并授权访问Google Cloud资源。
  2. 安装必要的库:
    • 安装了google-generativeaipillow库。google-generativeai用于访问Google的Gemini模型,而pillow是处理图像的Python库。
  3. 导入必要的库:
    • 导入了用于操作Gemini模型和图像处理的相关Python库,如google.generativeaiPIL.Imagegoogle.colab.userdatagoogle.colab.files
  4. 配置Gemini API密钥:
    • 设置一个变量来存储API密钥的名称,并从Colab的用户数据中获取Gemini API密钥。
  5. 初始化Gemini模型(视觉版):
    • 使用genai.GenerativeModel('gemini-pro-vision')初始化了Gemini模型的视觉处理版,允许代码对图像进行分析和生成操作。
  6. 上传和读取图像:
    • 使用files.upload()从本地文件系统上传图像。
    • 利用PIL.Image.open读取上传的图像文件。
  7. 使用Gemini模型生成内容:
    • 将上传的图像和一段文本(请求详细描述图片内容)作为输入,使用Gemini模型对图像进行分析。
    • 打印模型的响应文本,这通常包含对上传图像的描述或分析。
  8. 验证文件上传和图像显示:
    • 使用!ls -lh命令列出当前目录的内容,以验证文件是否已上传。
    • 使用IPython.display.Image显示上传的图像,确保图像正确上传并可以在Notebook中查看。
# Authenticate and grant access to Google Cloud servicesfrom google.colab import authauth.authenticate_user()
# Install necessary libraries!pip install -q -U google-generativeai pillow
# Import necessary librariesimport google.generativeai as genaiimport PIL.Imagefrom google.colab import userdatafrom google.colab import files
# Configure Gemini API keygemini_api_secret_name = 'GOOGLE_API_KEY' # Change this if your secret name is different
try: # Retrieve the Gemini API key from Colab's secrets GOOGLE_API_KEY = userdata.get(gemini_api_secret_name) genai.configure(api_key=GOOGLE_API_KEY)except userdata.SecretNotFoundError as e: print(f"Secret not found: Ensure you have a secret named {gemini_api_secret_name} in Colab.") raise eexcept Exception as e: print("There was an error retrieving the API key.") raise e
# Initialize the Gemini model for visionmodel = genai.GenerativeModel('gemini-pro-vision')
# Upload an image from your local file systemuploaded = files.upload()image_name = next(iter(uploaded))
# Read the image directly using the filenametry: img = PIL.Image.open(image_name)except PIL.UnidentifiedImageError: print("Error: The file uploaded is not a recognizable image. Please upload a valid image file.") raiseexcept Exception as e: print(f"An unexpected error occurred: {e}") raise
# Generate content based on the imageresponse = model.generate_content(["Tell me the content of the pic, please be detailed", img])
# Print the responseprint(response.text)# List the content of the directory to verify the file is uploaded!ls -lh
# Optionally: Display the image using a different library to check if it's correctly uploadedfrom IPython.display import Image, displaydisplay(Image(filename=image_name))

直接复制上述代码到colab即可,在左侧钥匙图标处,添加自己的谷歌API key。

在本地运行Python使用Gemini Pro API
我找到了一个github仓库:
https://github.com/unconv/gemini-testing作者从易到难给了好几个示例,直接git clone下来到本地学习即可。

我在本地运行py文件时先遇到了这个问题:

RetryError: Deadline of 60.0s exceeded while calling target function, last exception: 503 failed to connect to all addresses; last error: UNKNOWN:

解决办法是设置环境变量$http-proxy 和$https_proxy,可以直接把报错内容,你电脑的代理服务器、端口发给GPT4,它会给你详细指导。

解决好环境问题后,出现新的问题。

设置ADC地址:https://cloud.google.com/docs/authentication/provide-credentials-adc?hl=zh-cn#how-to

虽然它这里写了使用的API密钥,无需设置ADC。

但是它在本地运行时出现的消息是让我设置ADC。

谷歌的文档很细致,于是我跟着文档一步步操作下去。

安装后,我测试发现还有问题。

于是,接着下面的操作。

Gradio运行Gemini Pro API:

import google.generativeai as genaiimport gradio as grimport os
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))model = genai.GenerativeModel('gemini-pro')
chat = model.start_chat (history=[])
def chat_fun(message, history): return chat.send_message(message).text
gr.ChatInterface(chat_fun).queue().launch()

gradio会提供一个webui链接(在运行结果里出现)。

在这篇文章中,我探索了如何在不同环境下使用Gemini Pro API——从Google Colab的自动聊天和日志记录功能,到图像分析处理,再到本地Python环境中的实践应用。我还分享了可能遇到的问题及其解决方案,并提供了通过Gradio运行Gemini Pro API的示例代码。通过这些步骤,希望你能够顺利地使用Gemini Pro API。

文章转自微信公众号@kate人不错

#你可能也喜欢这些API文章!

我们有何不同?

API服务商零注册

多API并行试用

数据驱动选型,提升决策效率

查看全部API→
🔥

热门场景实测,选对API

#AI文本生成大模型API

对比大模型API的内容创意新颖性、情感共鸣力、商业转化潜力

25个渠道
一键对比试用API 限时免费

#AI深度推理大模型API

对比大模型API的逻辑推理准确性、分析深度、可视化建议合理性

10个渠道
一键对比试用API 限时免费