feat: change api base for #42 (#44)

* feat: change api base for #42

* fix: typo
This commit is contained in:
yihong 2023-03-05 15:47:37 +08:00 committed by GitHub
parent 1ad276808b
commit 7249540e98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 12 deletions

View File

@ -33,7 +33,10 @@ jobs:
run: pip install -r requirements.txt
- name: make test ebook
if: env.OPENAI_API_KEY != null
run: python3 make_book.py --book_name "test_books/${{ env.EPUB_FILENAME_BASE}}.epub" --no_limit --test --test_num 5 --language zh-hans
run: |
python3 make_book.py --book_name "test_books/${{ env.EPUB_FILENAME_BASE}}.epub" --no_limit --test --test_num 2 --language zh-hans
python3 make_book.py --book_name "test_books/${{ env.EPUB_FILENAME_BASE}}.epub" --no_limit --test --test_num 2 --language ja --model gpt3
- name: Rename and Upload ePub
if: env.OPENAI_API_KEY != null
uses: actions/upload-artifact@v2

View File

@ -24,6 +24,7 @@ Make bilingual epub books Using AI translate
Default target language is `"Simplified Chinese"`. Support language list please see the LANGUAGES at [utils.py](./utils.py).
7. 加了 `--proxy` 参数,方便中国大陆的用户在本地测试时使用代理,传入类似 `http://127.0.0.1:7890` 的字符串
8. 加入 `--resume` 命令,可以手动中断后,加入命令继续执行。
9. 如果你遇到了墙需要用 Cloudflare Workers 替换 api_base 请使用 `--api_base ${url}` 来替换
e.g.
```shell

View File

@ -26,6 +26,7 @@ Make bilingual epub books Using AI translate
Default target language is `"Simplified Chinese"`. Support language list please see the LANGUAGES at [utils.py](./utils.py).
7. Use the --proxy parameter to enable users in mainland China to use a proxy when testing locally. Enter a string such as http://127.0.0.1:7890.
8. Use the --resume command to manually resume the process after an interruption.
9. If you want to change api_base like using Cloudflare Workers Use --api_base ${url} to support it.
e.g.
```shell

View File

@ -6,13 +6,13 @@ from abc import abstractmethod
from copy import copy
from os import environ as env
from pathlib import Path
from tqdm import tqdm
import openai
import requests
from bs4 import BeautifulSoup as bs
from ebooklib import epub
from rich import print
from tqdm import tqdm
from utils import LANGUAGES, TO_LANGUAGE_CODE
@ -22,7 +22,7 @@ RESUME = False
class Base:
def __init__(self, key, language):
def __init__(self, key, language, api_base=None):
self.key = key
self.language = language
self.current_key_index = 0
@ -39,10 +39,13 @@ class Base:
class GPT3(Base):
def __init__(self, key, language):
def __init__(self, key, language, api_base=None):
super().__init__(key, language)
self.api_key = key
self.api_url = "https://api.openai.com/v1/completions"
if not api_base:
self.api_url = "https://api.openai.com/v1/completions"
else:
self.api_url = api_base + "v1/completions"
self.headers = {
"Content-Type": "application/json",
}
@ -70,18 +73,20 @@ class GPT3(Base):
class DeepL(Base):
def __init__(self, session, key):
super().__init__(session, key)
def __init__(self, session, key, api_base=None):
super().__init__(session, key, api_base=api_base)
def translate(self, text):
return super().translate(text)
class ChatGPT(Base):
def __init__(self, key, language):
super().__init__(key, language)
def __init__(self, key, language, api_base=None):
super().__init__(key, language, api_base=api_base)
self.key = key
self.language = language
if api_base:
openai.api_base = api_base
def translate(self, text):
print(text)
@ -135,10 +140,10 @@ class ChatGPT(Base):
class BEPUB:
def __init__(self, epub_name, model, key, resume, language):
def __init__(self, epub_name, model, key, resume, language, model_api_base=None):
self.epub_name = epub_name
self.new_epub = epub.EpubBook()
self.translate_model = model(key, language)
self.translate_model = model(key, language, model_api_base)
self.origin_book = epub.read_epub(self.epub_name)
self.p_to_save = []
self.resume = resume
@ -281,6 +286,13 @@ if __name__ == "__main__":
default="",
help="use proxy like http://127.0.0.1:7890",
)
# args to change api_base
parser.add_argument(
"--api_base",
dest="api_base",
type=str,
help="replace base url from openapi",
)
options = parser.parse_args()
NO_LIMIT = options.no_limit
@ -303,5 +315,14 @@ if __name__ == "__main__":
# use the value for prompt
language = LANGUAGES.get(language, language)
e = BEPUB(options.book_name, model, OPENAI_API_KEY, RESUME, language=language)
# change api_base for issue #42
model_api_base = options.api_base
e = BEPUB(
options.book_name,
model,
OPENAI_API_KEY,
RESUME,
language=language,
model_api_base=model_api_base,
)
e.make_bilingual_book()