-
Notifications
You must be signed in to change notification settings - Fork 165
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
create translator class for support multiple text translation api #67
base: main
Are you sure you want to change the base?
Conversation
can specify source language to speak for whisper input other than english, add try catch sounddevice for better error to user for debug , better type casting (or may be worst??)
I have resolved conflict with the main branch |
class Translator: | ||
def __init__(self, text="Hello, world!",source="en",target="ja"): | ||
self.mode = TRANSLATOR | ||
self.text = text | ||
self.source = source | ||
self.target = target | ||
self.interpreter = None | ||
self.google_project_id=GOOGLE_PROJECT_ID | ||
self.deepl_auth_key=DEEPL_AUTH_KEY | ||
|
||
def get_source(self): | ||
return self.source | ||
|
||
def get_target(self): | ||
return self.target | ||
|
||
def get_text(self): | ||
return self.text | ||
|
||
def get_mode(self): | ||
return self.mode | ||
|
||
def set_text(self, text): | ||
self.text = text | ||
|
||
def set_source(self, source): | ||
self.source = source | ||
|
||
def set_target(self, target): | ||
self.target = target | ||
|
||
def set_mode(self, mode): | ||
self.mode = mode |
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.
Seems this could be converted into a dataclass
and would preform the same actions.
https://docs.python.org/3/library/dataclasses.html
Instead of all these setters and getters, you could do:
from dataclasses import dataclass
@dataclass
class Translator:
text: str = "Hello, world!"
source: str = "en"
target: str = "ja"
mode: str = "TRANSLATOR"
interpreter: str = None
google_project_id: str = GOOGLE_PROJECT_ID
deepl_auth_key: str = DEEPL_AUTH_KEY
Then you can access these values the same way as any other, and break apart functionality into their own classes in future revisions as well.
def some_func(self):
if self.source == self.target:
raise ValueError("Source and Target is the same")
return self.translate()
Just as an example
def set_mode(self, mode): | ||
self.mode = mode | ||
|
||
def translate(self): |
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.
It may be best to have the ability to pass in the text here, and decouple the need to have 'text' as a class variable.
print("TRANSLATOR ENV is incorrect") | ||
return False | ||
|
||
def translate_deepl(self): |
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.
While skimming through the PR, it seems this function is only used within the class.
We can express this to be a "private function" by using _
at the beginning of the function.
It may be appropriate to do this here.
def _translate_deepl(self):
resp_txt = self.interpreter.translate_text(self.text, source_lang=self.source, target_lang=self.target) | ||
return resp_txt | ||
|
||
def translate_google_web(self): |
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.
While skimming through the PR, it seems this function is only used within the class.
We can express this to be a "private function" by using _
at the beginning of the function.
It may be appropriate to do this here.
def _translate_google_web(self):
resp_txt = self.interpreter.translate(self.text,src=self.source, dest=self.target).text | ||
return resp_txt | ||
|
||
def translate_google_api(self,project_id="project_id"): |
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.
While skimming through the PR, it seems this function is only used within the class.
We can express this to be a "private function" by using _
at the beginning of the function.
It may be appropriate to do this here.
def _translate_google_api(self):
The above comments are just suggestions from experience in the field 👍 No need to take action if not comfortable. |
except ValueError: | ||
return False |
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.
Returning False and dismissing their mistake for both of these lines below seems like a bad idea.
Maybe catch the ValueError and raise a more descriptive error instead? Or log some debug out to the user and exit(1)
early?
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.
Assertions are also valid to raise
assert val.isnumeric(), "Sorry the value for XXXX field is non-numerical. Please enter a valid number."
except ValueError: | ||
return False |
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.
Same as above comment
can specify source language to speak for whisper input other than english, add try catch sounddevice for better error to user for debug , better type casting (or may be worst??)
I cannot use deepL because i'm not in service area
by use google translate ajax call from https://translate.google.com lead to sometime you will get your IP blacklist by google and cannot use google service most of people might not get blacklist but I have.... and need to do captcha for days so I add an official Google Translation API to not risk it
I'm not sure that DeepL API call is still working require testing and I not in service area so.. I cannot
I'm not touch subtitler.py and its module because I'm not using it .....so some work might need to be done if you want to integrate my translator class to it