Skip to content

Commit

Permalink
Fix: gemini
Browse files Browse the repository at this point in the history
  • Loading branch information
jianchang512 committed Jan 13, 2025
1 parent b7e9848 commit fcee2e8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
4 changes: 2 additions & 2 deletions videotrans/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-

VERSION = "v3.45"
VERSION_NUM = 120345
VERSION = "v3.46"
VERSION_NUM = 120346
2 changes: 1 addition & 1 deletion videotrans/recognition/_gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _exec(self):
error=str(e) if config.defaulelang !='zh' else '无法连接到Gemini,请尝试使用或更换代理'
raise requests.ConnectionError(error)
except google.api_core.exceptions.PermissionDenied:
raise Exception(f'您无权访问所请求的资源或模型' if config.defaulelang =='zh' else 'You don't have permission for the requested resource')
raise Exception(f'您无权访问所请求的资源或模型' if config.defaulelang =='zh' else 'You donot have permission for the requested resource')
except google.api_core.exceptions.ResourceExhausted:
raise Exception(f'您的配额已用尽。请稍等片刻,然后重试,若仍如此,请查看Google账号 ' if config.defaulelang =='zh' else 'Your quota is exhausted. Please wait a bit and try again')
except google.auth.exceptions.DefaultCredentialsError:
Expand Down
29 changes: 20 additions & 9 deletions videotrans/translator/_gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ def _item_task(self, data: Union[List[str], str]) -> str:
message = self.prompt.replace('<INPUT></INPUT>',f'<INPUT>{text}</INPUT>')
api_key=self.api_keys.pop(0)
self.api_keys.append(api_key)
config.logger.info(f'[Gemini]请求发送:{api_key=},{config.params["gemini_model"]=}')
genai.configure(api_key=api_key)

model = genai.GenerativeModel(model_name=config.params['gemini_model'],generation_config={"max_output_tokens": 8192},system_instruction="You are a translation assistant specializing in converting SRT subtitle content from one language to another while maintaining the original format and structure." if config.defaulelang != 'zh' else '您是一名翻译助理,专门负责将 SRT 字幕内容从一种语言转换为另一种语言,同时保持原始格式和结构。')
response = model.generate_content(
message,
safety_settings=safetySettings
)
config.logger.info(f'[Gemini]请求发送:{message=}')

result = response.text
config.logger.info(f'[Gemini]返回:{result=}')
Expand All @@ -75,7 +76,7 @@ def _item_task(self, data: Union[List[str], str]) -> str:
error=str(e) if config.defaulelang !='zh' else '无法连接到Gemini,请尝试使用或更换代理或切换模型'
raise requests.ConnectionError(error)
except google.api_core.exceptions.PermissionDenied:
raise Exception(f'您无权访问所请求的资源或模型' if config.defaulelang =='zh' else 'You don't have permission for the requested resource')
raise Exception('您无权访问所请求的资源或模型' if config.defaulelang =='zh' else 'You donot have permission for the requested resource')
except google.api_core.exceptions.ResourceExhausted:
raise Exception(f'您的配额已用尽。请稍等片刻,然后重试,若仍如此,请查看Google账号 ' if config.defaulelang =='zh' else 'Your quota is exhausted. Please wait a bit and try again')
except google.auth.exceptions.DefaultCredentialsError:
Expand Down Expand Up @@ -164,15 +165,25 @@ def _item_task_refine3(self, data: Union[List[str], str]) -> str:
except (ServerError,RetryError,socket.timeout) as e:
error=str(e) if config.defaulelang !='zh' else '无法连接到Gemini,请尝试使用或更换代理'
raise requests.ConnectionError(error)
except google.api_core.exceptions.PermissionDenied:
raise Exception('您无权访问所请求的资源或模型' if config.defaulelang =='zh' else 'You donot have permission for the requested resource')
except google.api_core.exceptions.ResourceExhausted:
raise Exception(f'您的配额已用尽。请稍等片刻,然后重试,若仍如此,请查看Google账号 ' if config.defaulelang =='zh' else 'Your quota is exhausted. Please wait a bit and try again')
except google.auth.exceptions.DefaultCredentialsError:
raise Exception(f'验证失败,可能 Gemini API Key 不正确 ' if config.defaulelang =='zh' else 'Authentication fails. Please double-check your API key and try again')
except google.api_core.exceptions.InvalidArgument:
raise Exception(f'文件过大或 Gemini API Key 不正确 ' if config.defaulelang =='zh' else 'Invalid argument. One example is the file is too large and exceeds the payload size limit. Another is providing an invalid API key')
except google.api_core.exceptions.RetryError:
raise Exception('无法连接到Gemini,请尝试使用或更换代理' if config.defaulelang=='zh' else 'Can be caused when using a proxy that does not support gRPC.')
except genai.types.BlockedPromptException as e:
raise Exception(self._get_error(e.args[0].finish_reason))
except genai.types.StopCandidateException as e:
if int(e.args[0].finish_reason>1):
raise Exception(self._get_error(e.args[0].finish_reason))

except Exception as e:
error = str(e)
config.logger.error(f'[Gemini]请求失败:{error=}')
if response and response.prompt_feedback.block_reason:
raise Exception(self._get_error(response.prompt_feedback.block_reason, "forbid"))

if error.find('User location is not supported') > -1 or error.find('time out') > -1:
if error.find('User location is not supported') > -1:
raise Exception("当前请求ip(或代理服务器)所在国家不在Gemini API允许范围")

if response and len(response.candidates) > 0 and response.candidates[0].finish_reason not in [0, 1]:
raise Exception(self._get_error(response.candidates[0].finish_reason))
raise
2 changes: 1 addition & 1 deletion videotrans/util/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1958,7 +1958,7 @@ def show_glossary_editor(parent):
layout = QVBoxLayout(dialog)

text_edit = QTextEdit()
text_edit.setPlaceholderText("请按照 术语=翻译 的格式,一行一组来填写,例如\n\n国家弹道导弹防御系统=NBMD\n首席执行官=CEO\n人工智能=AI\n\n在原文中如果遇到以上左侧文字,则翻译结果使用右侧文字" if config.defaulelang=='zh' else "Please fill in one line at a time, following the term on the left and the translation on the right, e.g. \nBallistic Missile Defense=BMD\nChief Executive Officer=CEO")
text_edit.setPlaceholderText("请按照 术语=翻译 的格式,一行一组来填写,例如\n\n首席执行官=CEO\n人工智能=AI\n\n在原文中如果遇到以上左侧文字,则翻译结果使用右侧文字" if config.defaulelang=='zh' else "Please fill in one line at a time, following the term on the left and the translation on the right, e.g. \nBallistic Missile Defense=BMD\nChief Executive Officer=CEO")
layout.addWidget(text_edit)

button_box = QDialogButtonBox(QDialogButtonBox.Save | QDialogButtonBox.Cancel)
Expand Down

0 comments on commit fcee2e8

Please sign in to comment.