-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: class-based design #15
Changes from all commits
d7c126e
e1c1b4e
e3dd600
109cd02
bc7aab4
26fe9cf
e6be40e
3be24a6
d495f4f
5cc45c2
d61d287
cdbb239
d08be72
e01ca44
4f645c7
87fda3a
ff2aed2
c74e42f
252c99b
235a148
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import os | ||
|
||
import pytest | ||
|
||
from yuisub.translator import SubtitleTranslator | ||
|
||
from . import util | ||
|
||
|
||
@pytest.mark.skipif(os.environ.get("GITHUB_ACTIONS") == "true", reason="Skipping test when running on CI") | ||
async def test_translator_sub() -> None: | ||
translator = SubtitleTranslator( | ||
model=util.OPENAI_MODEL, | ||
api_key=util.OPENAI_API_KEY, | ||
base_url=util.OPENAI_BASE_URL, | ||
bangumi_url=util.BANGUMI_URL, | ||
bangumi_access_token=util.BANGUMI_ACCESS_TOKEN, | ||
) | ||
|
||
sub_zh, sub_bilingual = await translator.get_subtitles(sub=str(util.TEST_ENG_SRT)) | ||
Comment on lines
+11
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Test should verify the content of the translated subtitles The test only checks if the files are saved but doesn't verify the actual content of the translations. Consider adding assertions to check the translated text, timing, and format of both sub_zh and sub_bilingual. async def test_translator_sub() -> None:
translator = SubtitleTranslator(
model=util.OPENAI_MODEL,
api_key=util.OPENAI_API_KEY,
base_url=util.OPENAI_BASE_URL,
bangumi_url=util.BANGUMI_URL,
bangumi_access_token=util.BANGUMI_ACCESS_TOKEN,
)
sub_zh, sub_bilingual = await translator.get_subtitles(sub=str(util.TEST_ENG_SRT))
assert "你好" in str(sub_zh)
assert "Hello" in str(sub_bilingual) and "你好" in str(sub_bilingual) |
||
sub_zh.save(util.projectPATH / "assets" / "test.zh.translator.sub.ass") | ||
sub_bilingual.save(util.projectPATH / "assets" / "test.bilingual.translator.sub.ass") | ||
|
||
|
||
@pytest.mark.skipif(os.environ.get("GITHUB_ACTIONS") == "true", reason="Skipping test when running on CI") | ||
async def test_translator_audio() -> None: | ||
translator = SubtitleTranslator( | ||
torch_device=util.DEVICE, | ||
whisper_model=util.MODEL_NAME, | ||
model=util.OPENAI_MODEL, | ||
api_key=util.OPENAI_API_KEY, | ||
base_url=util.OPENAI_BASE_URL, | ||
bangumi_url=util.BANGUMI_URL, | ||
bangumi_access_token=util.BANGUMI_ACCESS_TOKEN, | ||
) | ||
Comment on lines
+26
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Add error case tests for the SubtitleTranslator The tests only cover the happy path. Consider adding tests for error cases such as invalid audio files, network errors, invalid API keys, and other edge cases that could occur during translation. async def test_translator_audio() -> None:
translator = SubtitleTranslator(
torch_device=util.DEVICE,
whisper_model=util.MODEL_NAME,
model=util.OPENAI_MODEL,
api_key=util.OPENAI_API_KEY,
base_url=util.OPENAI_BASE_URL,
bangumi_url=util.BANGUMI_URL,
bangumi_access_token=util.BANGUMI_ACCESS_TOKEN,
)
sub_zh, sub_bilingual = await translator.get_subtitles(audio=str(util.TEST_AUDIO))
sub_zh.save(util.projectPATH / "assets" / "test.zh.translator.audio.ass")
sub_bilingual.save(util.projectPATH / "assets" / "test.bilingual.translator.audio.ass")
with pytest.raises(FileNotFoundError):
await translator.get_subtitles(audio="nonexistent_file.mp3")
with pytest.raises(Exception):
invalid_translator = SubtitleTranslator(
torch_device=util.DEVICE,
whisper_model=util.MODEL_NAME,
model=util.OPENAI_MODEL,
api_key="invalid_key",
base_url=util.OPENAI_BASE_URL,
bangumi_url=util.BANGUMI_URL,
bangumi_access_token=util.BANGUMI_ACCESS_TOKEN,
)
await invalid_translator.get_subtitles(audio=str(util.TEST_AUDIO)) |
||
|
||
sub_zh, sub_bilingual = await translator.get_subtitles(audio=str(util.TEST_AUDIO)) | ||
sub_zh.save(util.projectPATH / "assets" / "test.zh.translator.audio.ass") | ||
sub_bilingual.save(util.projectPATH / "assets" / "test.bilingual.translator.audio.ass") |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,8 +112,8 @@ async def translate( | |
base_url=base_url, | ||
bangumi_info=bangumi_info, | ||
) | ||
print(summarizer.system_prompt) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Remove or replace debug print statement Consider using a proper logging system instead of print statements if this information is important for debugging. import logging
logging.debug(summarizer.system_prompt) |
||
|
||
print("Summarizing...") | ||
# get summary | ||
summary = await summarizer.ask(ORIGIN(origin="\n".join(trans_list))) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (testing): Consider using mocks for CI environment instead of skipping tests
Rather than skipping these tests in CI, consider mocking the external dependencies (Whisper model, OpenAI API) to allow these tests to run in all environments. This would provide better test coverage and catch potential issues earlier.