Skip to content

Commit

Permalink
Merge pull request #24 from MinuraPunchihewa/development
Browse files Browse the repository at this point in the history
[PR] dev to main: made the lang_input and lang_output columns of the translation task optional
  • Loading branch information
MinuraPunchihewa authored Jul 20, 2023
2 parents 3351c5a + 9f1ca1e commit 2fa33ea
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion hugging_py_face/__about__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__title__ = 'hugging_py_face'
__package_name__ = 'hugging_py_face'
__version__ = '0.5.2'
__version__ = '0.5.3'
__description__ = "Hugging-Py-Face, the Python client for the Hugging Face Inference API."
__email__ = "[email protected]"
__author__ = 'Minura Punchihewa'
Expand Down
4 changes: 4 additions & 0 deletions hugging_py_face/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ class TaskModelMismatchException(Exception):


class APICallException(Exception):
pass


class InsufficientParametersException(Exception):
pass
18 changes: 11 additions & 7 deletions hugging_py_face/nlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Text, List, Dict, Optional, Union

from .base_api import BaseAPI
from .exceptions import HTTPServiceUnavailableException, APICallException
from .exceptions import HTTPServiceUnavailableException, APICallException, InsufficientParametersException


class NLP(BaseAPI):
Expand Down Expand Up @@ -369,36 +369,40 @@ def feature_extraction(self, text: Union[Text, List], options: Optional[Dict] =
"""
return self._query(text, options=options, model=model, task='feature-extraction')

def translation(self, text: Union[Text, List], lang_input: Text, lang_output: Text, options: Optional[Dict] = None, model: Optional[Text] = None) -> Union[Dict, List]:
def translation(self, text: Union[Text, List], lang_input: Text = None, lang_output: Text = None, options: Optional[Dict] = None, model: Optional[Text] = None) -> Union[Dict, List]:
"""
Translates text from one language to another.
:param text: a string or a list of strings to translate.
:param lang_input: the short code of the language of the input text.
:param lang_output: the short code of the language to translate the input text to.
:param lang_input: the short code of the language of the input text. This parameter is mandatory if the model is not provided.
:param lang_output: the short code of the language to translate the input text to. This parameter is mandatory if the model is not provided.
:param options: a dict of options. For more information, see the `detailed parameters for the translation task <https://huggingface.co/docs/api-inference/detailed_parameters#translation-task>`_.
:param model: the model to use for the translation task. If not provided, the recommended model from Hugging Face will be used.
:return: a dict or a list of dicts containing the translated text.
"""
if model is None:
if lang_input is None or lang_output is None:
InsufficientParametersException("lang_input and lang_output are required if model is not provided.")
model = f"{self.config['TASK_MODEL_MAP']['translation']}{lang_input}-{lang_output}"
return self._query(text, options=options, model=model, task='translation')
else:
return self._query(text, options=options, model=model, task='translation')

def translation_in_df(self, df: DataFrame, column: Text, lang_input: Text, lang_output: Text, options: Optional[Dict] = None, model: Optional[Text] = None) -> DataFrame:
def translation_in_df(self, df: DataFrame, column: Text, lang_input: Text = None, lang_output: Text = None, options: Optional[Dict] = None, model: Optional[Text] = None) -> DataFrame:
"""
Translates text from one language to another.
:param df: a pandas DataFrame containing the strings to be translated.
:param column: the column containing the strings to be translated.
:param lang_input: the short code of the language of the input text.
:param lang_output: the short code of the language to translate the input text to.
:param lang_input: the short code of the language of the input text. This parameter is mandatory if the model is not provided.
:param lang_output: the short code of the language to translate the input text to. This parameter is mandatory if the model is not provided.
:param options: a dict of options. For more information, see the `detailed parameters for the translation task <https://huggingface.co/docs/api-inference/detailed_parameters#translation-task>`_.
:param model: the model to use for the translation task. If not provided, the recommended model from Hugging Face will be used.
:return: a pandas DataFrame with the translations. The translations will be added as a new column called 'predictions' to the original DataFrame.
"""
if model is None:
if lang_input is None or lang_output is None:
InsufficientParametersException("lang_input and lang_output are required if model is not provided.")
model = f"{self.config['TASK_MODEL_MAP']['translation']}{lang_input}-{lang_output}"
predictions = self._query_in_df(df, column, options=options, model=model, task='translation')
else:
Expand Down

0 comments on commit 2fa33ea

Please sign in to comment.