feat: 支持从环境变量 GEMINI_MODEL 获取模型名

This commit is contained in:
ivanberry 2026-03-16 19:01:41 +08:00
parent 1ee8188ecd
commit be889623bd
1 changed files with 15 additions and 2 deletions

View File

@ -20,6 +20,10 @@ from dataclasses import dataclass, field
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import sys
sys.path.insert(0, str(Path(__file__).parent))
from load_env import load_env
load_env() # 加载 .env 文件
try: try:
import google.generativeai as genai # type: ignore import google.generativeai as genai # type: ignore
except ImportError as exc: except ImportError as exc:
@ -96,6 +100,11 @@ def get_api_key() -> str:
) )
def get_model_name() -> str:
import os
return os.getenv("GEMINI_MODEL", "gemini-2.0-flash-lite")
def extract_chinese_content( def extract_chinese_content(
input_path: Path, input_path: Path,
columns: list[str] | None = None, columns: list[str] | None = None,
@ -160,9 +169,11 @@ def extract_chinese_content(
def translate_entries( def translate_entries(
entries: list[TranslationEntry], entries: list[TranslationEntry],
model_name: str = "gemini-2.0-flash-lite", model_name: str | None = None,
api_key: str | None = None, api_key: str | None = None,
) -> list[TranslationEntry]: ) -> list[TranslationEntry]:
if model_name is None:
model_name = get_model_name()
if not entries: if not entries:
print("✓ 没有需要翻译的内容") print("✓ 没有需要翻译的内容")
return entries return entries
@ -263,10 +274,12 @@ def translate_excel_file(
output_path: Path | None = None, output_path: Path | None = None,
columns: list[str] | None = None, columns: list[str] | None = None,
sheet_name: str | None = None, sheet_name: str | None = None,
model_name: str = "gemini-2.0-flash-lite", model_name: str | None = None,
api_key: str | None = None, api_key: str | None = None,
dry_run: bool = False, dry_run: bool = False,
) -> Path: ) -> Path:
if model_name is None:
model_name = get_model_name()
if not output_path: if not output_path:
output_path = input_path.parent / f"{input_path.stem}_en{input_path.suffix}" output_path = input_path.parent / f"{input_path.stem}_en{input_path.suffix}"