You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GPT-4-Vision-Preview在Colab批量生成图片元描述时中途报错求助

问题诊断

前100张图片正常处理,后续批量生成时出现"error",核心原因集中在以下几点:

  • OpenAI API速率限制:GPT-4-Vision-Preview有严格的调用频次与令牌配额,短时间密集请求会触发429限流错误
  • 错误处理不完善:原代码仅判断HTTP 200状态码,未捕获API返回的具体错误(如限流、余额不足、格式不兼容)
  • 无请求间隔:循环无延迟发送请求,极易触发平台限流机制
  • 图片格式不匹配:将PNG图片强制用data:image/jpeg;base64格式传递,可能导致解析失败

解决方案

以下是修复后的代码,包含限流处理、错误日志、请求间隔、格式修正等关键优化:

import os
import base64
import requests
import pandas as pd
from google.colab import drive
import time
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

# 挂载Google Drive
drive.mount('/content/drive')

# 配置参数
image_folder = '/content/drive/MyDrive/Work related/FS/Imagenes/Metadescripciones HC '
api_key = 'your_api_key_here'
REQUEST_INTERVAL = 7  # 每次请求间隔7秒,适配GPT-4V基础速率限制
MAX_RETRIES = 3  # 临时错误自动重试次数

# 创建带重试机制的请求会话
session = requests.Session()
retry = Retry(
    total=MAX_RETRIES,
    backoff_factor=1,
    status_forcelist=[429, 500, 502, 503, 504]
)
adapter = HTTPAdapter(max_retries=retry)
session.mount("https://", adapter)
session.mount("http://", adapter)

def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {api_key}"
}

# 新增错误详情列,便于排查问题
results_df = pd.DataFrame(columns=['Nombre del Archivo', 'Metadescripcion', 'Error Detalle'])

# 整理并排序图片列表,保证处理顺序稳定
image_files = [f for f in os.listdir(image_folder) if f.endswith((".png", ".jpg", ".jpeg"))]
image_files.sort()

for idx, filename in enumerate(image_files):
    image_path = os.path.join(image_folder, filename)
    # 根据图片后缀动态设置data URI前缀,避免格式不匹配
    ext = filename.split('.')[-1].lower()
    data_uri_prefix = f"data:image/{ext};base64,"
    
    try:
        base64_image = encode_image(image_path)
        
        payload = {
            "model": "gpt-4-vision-preview",
            "messages": [
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "text",
                            "text": "Write a meta description for the image of this product, optimized for SEO and in less than 150 words"
                        },
                        {
                            "type": "image_url",
                            "image_url": {
                                "url": f"{data_uri_prefix}{base64_image}",
                                "detail": "low"
                            }
                        }
                    ]
                }
            ],
            "max_tokens": 150  # 匹配元描述长度需求,减少令牌消耗
        }

        response = session.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
        response.raise_for_status()  # 主动触发HTTP错误异常
        response_json = response.json()
        metadescription = response_json['choices'][0]['message']['content']
        error_detail = "Success"
        
        print(f"Processed {idx+1}/{len(image_files)}: {filename}")
        
    except requests.exceptions.HTTPError as e:
        # 捕获并记录API返回的具体错误信息
        error_detail = f"HTTP Error: {response.status_code} - {response.json().get('error', {}).get('message', 'Unknown error')}"
        metadescription = "Error"
    except Exception as e:
        # 捕获文件读取、JSON解析等其他异常
        error_detail = f"General Error: {str(e)}"
        metadescription = "Error"
    
    # 高效追加数据到DataFrame,避免频繁创建新对象
    results_df.loc[len(results_df)] = {'Nombre del Archivo': filename, 'Metadescripcion': metadescription, 'Error Detalle': error_detail}
    
    # 最后一张图片无需添加间隔
    if idx != len(image_files) - 1:
        time.sleep(REQUEST_INTERVAL)

# 保存结果到Excel
results_df.to_excel('/content/drive/MyDrive/Work related/FS/Imagenes/Metadescripciones.xlsx', index=False)
print("Processing completed. Results saved to Excel.")

关键优化说明
  • 速率控制:添加固定请求间隔,避免触发API限流机制
  • 重试机制:自动重试限流、服务器错误等临时问题,提升任务成功率
  • 错误排查:新增错误详情列,记录具体失败原因,便于定位问题
  • 格式兼容:根据图片后缀动态设置data URI前缀,避免格式解析错误
  • 性能优化:用df.loc替代pd.concat追加数据,提升大数据量处理效率
  • 令牌节省:调整max_tokens至合理范围,减少不必要的令牌消耗

内容的提问来源于stack exchange,提问作者Yair Y

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 08:57:28