Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

sereepap2029
Copy link

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

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??)
@sereepap2029
Copy link
Author

I have resolved conflict with the main branch

Comment on lines +12 to +44
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
Copy link

@santosderek santosderek Mar 17, 2023

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):

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):
Copy link

@santosderek santosderek Mar 17, 2023

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):

https://peps.python.org/pep-0008/#descriptive-naming-styles

resp_txt = self.interpreter.translate_text(self.text, source_lang=self.source, target_lang=self.target)
return resp_txt

def translate_google_web(self):
Copy link

@santosderek santosderek Mar 17, 2023

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):

https://peps.python.org/pep-0008/#descriptive-naming-styles

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"):
Copy link

@santosderek santosderek Mar 17, 2023

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):

https://peps.python.org/pep-0008/#descriptive-naming-styles

@santosderek
Copy link

The above comments are just suggestions from experience in the field 👍 No need to take action if not comfortable.

Comment on lines +6 to +7
except ValueError:
return False
Copy link

@santosderek santosderek Mar 17, 2023

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?

Copy link

@santosderek santosderek Mar 17, 2023

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."

Comment on lines +13 to +14
except ValueError:
return False

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants