Update gemini.py
Browse files1889 unmodified lines1890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091595 unmodified lines26872688268926901889 unmodified linesreturn result_text@app.route('/v1/images/generations', methods=['POST'])@require_api_authdef create_image():"""OpenAI 兼容的图片生成接口"""try:# 每次请求时清理过期图片cleanup_expired_images()data = request.json or {}prompt = data.get('prompt', '')if not prompt:return jsonify({"error": {"message": "prompt is required", "type": "invalid_request_error"}}), 400# OpenAI 兼容参数n = data.get('n', 1) # 生成图片数量,默认1size = data.get('size', '1024x1024') # 图片尺寸(提示用,实际由Gemini决定)quality = data.get('quality', 'standard') # 质量(提示用)response_format = data.get('response_format', 'url') # 响应格式:url 或 b64_jsonmodel = data.get('model', 'dall-e-3') # 模型名称(提示用)# 限制生成数量if n < 1 or n > 10:return jsonify({"error": {"message": "n must be between 1 and 10", "type": "invalid_request_error"}}), 400# 构建图片生成提示词image_prompt = f"请生成图片:{prompt}"if size:image_prompt += f",尺寸:{size}"if quality == 'hd':image_prompt += ",高质量"# 检查是否指定了特定账号specified_account_id = data.get('account_id')if specified_account_id is not None:# 使用指定的账号accounts = account_manager.accountsif specified_account_id < 0 or specified_account_id >= len(accounts):return jsonify({"error": {"message": f"无效的账号ID: {specified_account_id}", "type": "invalid_request_error"}}), 400account = accounts[specified_account_id]if not account.get('enabled', True):return jsonify({"error": {"message": f"账号 {specified_account_id} 已禁用", "type": "invalid_request_error"}}), 400# 检查是否在冷却中cooldown_until = account.get('cooldown_until', 0)if cooldown_until > time.time():return jsonify({"error": {"message": f"账号 {specified_account_id} 正在冷却中,请稍后重试", "type": "rate_limit_error"}}), 429account_idx = specified_account_idall_images = []last_error = Nonetry:session, jwt, team_id = ensure_session_for_account(account_idx, account)proxy = get_proxy()# 生成图片(可能需要多次调用以满足 n 参数)for i in range(n):chat_response = stream_chat_with_images(jwt, session, image_prompt, proxy, team_id, [])all_images.extend(chat_response.images)if len(all_images) >= n:break# 只返回前 n 张图片all_images = all_images[:n]except AccountRateLimitError as e:last_error = ept_wait = seconds_until_next_pt_midnight()cooldown_seconds = max(account_manager.rate_limit_cooldown, pt_wait)account_manager.mark_account_cooldown(account_idx, str(e), cooldown_seconds)except AccountAuthError as e:last_error = eaccount_manager.mark_account_cooldown(account_idx, str(e), account_manager.auth_error_cooldown)except AccountRequestError as e:last_error = eaccount_manager.mark_account_cooldown(account_idx, str(e), account_manager.generic_error_cooldown)except Exception as e:last_error = eelse:# 轮训获取账号available_accounts = account_manager.get_available_accounts()if not available_accounts:next_cd = account_manager.get_next_cooldown_info()wait_msg = ""if next_cd:wait_msg = f"(最近冷却账号 {next_cd['index']},约 {int(next_cd['cooldown_until']-time.time())} 秒后可重试)"return jsonify({"error": {"message": f"没有可用的账号{wait_msg}", "type": "rate_limit_error"}}), 429max_retries = len(available_accounts)last_error = Noneall_images = []account_idx = Nonefor retry_idx in range(max_retries):try:account_idx, account = account_manager.get_next_account()session, jwt, team_id = ensure_session_for_account(account_idx, account)proxy = get_proxy()# 生成图片(可能需��多次调用以满足 n 参数)for i in range(n):chat_response = stream_chat_with_images(jwt, session, image_prompt, proxy, team_id, [])all_images.extend(chat_response.images)if len(all_images) >= n:break# 只返回前 n 张图片all_images = all_images[:n]breakexcept AccountRateLimitError as e:last_error = eif account_idx is not None:pt_wait = seconds_until_next_pt_midnight()cooldown_seconds = max(account_manager.rate_limit_cooldown, pt_wait)account_manager.mark_account_cooldown(account_idx, str(e), cooldown_seconds)print(f"[图片生成] 第{retry_idx+1}次尝试失败(限额): {e}")continueexcept AccountAuthError as e:last_error = eif account_idx is not None:account_manager.mark_account_cooldown(account_idx, str(e), account_manager.auth_error_cooldown)print(f"[图片生成] 第{retry_idx+1}次尝试失败(凭证): {e}")continueexcept AccountRequestError as e:last_error = eif account_idx is not None:account_manager.mark_account_cooldown(account_idx, str(e), account_manager.generic_error_cooldown)print(f"[图片生成] 第{retry_idx+1}次尝试失败(请求异常): {e}")continueexcept Exception as e:last_error = eprint(f"[图片生成] 第{retry_idx+1}次尝试失败: {type(e).__name__}: {e}")if account_idx is None:breakcontinueif not all_images:error_message = last_error or "图片生成失败"status_code = 429 if isinstance(last_error, (AccountRateLimitError, NoAvailableAccount)) else 500err_type = "rate_limit_error" if status_code == 429 else "api_error"return jsonify({"error": {"message": error_message, "type": err_type}}), status_code# 构建 OpenAI 格式响应base_url = get_image_base_url(request.host_url)image_data_list = []for img in all_images:image_item = {}if response_format == 'b64_json':# 返回 base64 编码的图片if img.base64_data:image_item["b64_json"] = img.base64_dataelse:# 如果没有 base64 数据,尝试从文件读取if img.local_path and os.path.exists(img.local_path):with open(img.local_path, 'rb') as f:image_bytes = f.read()image_item["b64_json"] = base64.b64encode(image_bytes).decode('utf-8')else:# 降级为 URLif img.file_name:image_item["url"] = f"{base_url}image/{img.file_name}"else:# 返回 URL(默认)if img.file_name:image_item["url"] = f"{base_url}image/{img.file_name}"elif img.base64_data:# 如果没有文件名但有 base64,保存到缓存try:decoded = base64.b64decode(img.base64_data)filename = save_image_to_cache(decoded, img.mime_type or "image/png")image_item["url"] = f"{base_url}image/{filename}"except Exception:# 降级为 base64image_item["b64_json"] = img.base64_data# 可选:添加修订后的提示词if prompt:image_item["revised_prompt"] = promptimage_data_list.append(image_item)response = {"created": int(time.time()),"data": image_data_list}return jsonify(response)except Exception as e:import tracebacktraceback.print_exc()return jsonify({"error": {"message": str(e), "type": "api_error"}}), 500# ==================== 图片服务接口 ====================@app.route('/image/<path:filename>')595 unmodified linesif not account_manager.accounts:print("[!] 警告: 没有配置任何账号")app.run(host='0.0.0.0', port=7860, debug=False)1889 unmodified lines189018911892189318941895595 unmodified lines24912492249324941889 unmodified linesreturn result_text# ==================== 图片服务接口 ====================@app.route('/image/<path:filename>')595 unmodified linesif not account_manager.accounts:print("[!] 警告: 没有配置任何账号")app.run(host='0.0.0.0', port=7860, debug=False)
gemini.py+1-197