Skip to content

Commit

Permalink
feat: Supported custom endpoint for Claude.
Browse files Browse the repository at this point in the history
  • Loading branch information
bookfere committed Mar 17, 2024
1 parent 0358aa0 commit bc36961
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 20 deletions.
3 changes: 2 additions & 1 deletion advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .lib.translation import get_engine_class, get_translator, get_translation
from .lib.element import get_element_handler, Extraction
from .lib.conversion import extract_item
from .engines.custom import CustomTranslate

from . import EbookTranslator
from .components import (
Expand Down Expand Up @@ -613,7 +614,7 @@ def refresh_languages():
source_lang.refresh.emit(
self.current_engine.lang_codes.get('source'),
self.current_engine.config.get('source_lang'),
not self.current_engine.is_custom())
not isinstance(self.current_engine, CustomTranslate))
lang = self.current_engine.config.get('target_lang')
if target_lang.findText(self.ebook.target_lang):
lang = self.ebook.target_lang
Expand Down
3 changes: 2 additions & 1 deletion batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from .lib.config import get_config
from .lib.translation import get_engine_class
from .engines.custom import CustomTranslate
from .components import (
layout_info, AlertMessage, SourceLang, TargetLang, InputFormat,
OutputFormat)
Expand Down Expand Up @@ -104,7 +105,7 @@ def change_source_lang(lang, row=row):
source_lang.refresh.emit(
translation_engine.lang_codes.get('source'),
translation_engine.config.get('source_lang'),
not translation_engine.is_custom())
not issubclass(translation_engine, CustomTranslate))

target_lang = TargetLang()
table.setCellWidget(row, 4, target_lang)
Expand Down
8 changes: 5 additions & 3 deletions components/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from ..lib.utils import sorted_mixed_keys
from ..lib.config import get_config
from ..engines.custom import create_engine_template, load_engine_data
from ..engines.custom import (
create_engine_template, load_engine_data, CustomTranslate)
from ..engines import builtin_engines, GoogleFreeTranslate

from .lang import SourceLang, TargetLang
Expand Down Expand Up @@ -115,8 +116,9 @@ def layout(self):
layout.addWidget(target, 1, 0, 1, 3)

source_lang = SourceLang()
source_lang.set_codes(self.translator.lang_codes.get('source'),
not self.translator.is_custom())
source_lang.set_codes(
self.translator.lang_codes.get('source'),
not isinstance(self.translator, CustomTranslate))
layout.addWidget(source_lang, 2, 0)

def change_source_lang(lang):
Expand Down
8 changes: 0 additions & 8 deletions engines/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,6 @@ def api_key_error_message(cls):
return _('A correct key format "{}" is required.') \
.format(cls.api_key_hint)

@classmethod
def is_chatgpt(cls):
return 'chatgpt' in cls.__name__.lower()

@classmethod
def is_custom(cls):
return cls.__name__ == 'CustomTranslate'

def change_api_key(self):
"""Change the API key if the previous one cannot be used."""
if self.api_key not in self.bad_api_keys:
Expand Down
11 changes: 4 additions & 7 deletions setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from .engines import (
builtin_engines, GeminiPro, ChatgptTranslate, ClaudeTranslate)
from .engines.custom import CustomTranslate
from .components import (
layout_info, AlertMessage, TargetLang, SourceLang, EngineList,
EngineTester, ManageCustomEngine, InputFormat, OutputFormat)
Expand Down Expand Up @@ -562,10 +563,8 @@ def show_chatgpt_preferences():
# Endpoint
self.chatgpt_endpoint.setPlaceholderText(
self.current_engine.endpoint)
chatgpt_layout.setRowVisible(self.chatgpt_endpoint, is_chatgpt)
self.chatgpt_endpoint.setText(
config.get('endpoint', self.current_engine.endpoint)
if is_chatgpt else '')
config.get('endpoint', self.current_engine.endpoint))
self.chatgpt_endpoint.setCursorPosition(0)
# Model
if self.current_engine.model is not None:
Expand Down Expand Up @@ -641,7 +640,7 @@ def choose_default_engine(index):
source_lang = self.current_engine.config.get('source_lang')
self.source_lang.refresh.emit(
self.current_engine.lang_codes.get('source'), source_lang,
not self.current_engine.is_custom())
not issubclass(self.current_engine, CustomTranslate))
target_lang = self.current_engine.config.get('target_lang')
self.target_lang.refresh.emit(
self.current_engine.lang_codes.get('target'), target_lang)
Expand Down Expand Up @@ -1171,12 +1170,10 @@ def get_engine_config(self):
config.update(api_keys=api_keys)
self.set_api_keys()

# ChatGPT preference
if issubclass(self.current_engine, ChatgptTranslate) or \
issubclass(self.current_engine, ClaudeTranslate):
self.update_prompt(self.chatgpt_prompt, config)

# ChatGPT preference
if issubclass(self.current_engine, ChatgptTranslate):
endpoint = self.chatgpt_endpoint.text().strip()
if 'endpoint' in config:
del config['endpoint']
Expand Down

0 comments on commit bc36961

Please sign in to comment.