


import textwrap

import pyarrow as pa
import pytest

from datasets import Features, Image
from datasets.packaged_modules.text.text import Text

from ..utils import require_pil







@pytest.fixture
def 	A (		A__			):
     UpperCAmelCase    :					Optional[Any]			=      tmp_path / "text.txt"
     UpperCAmelCase    :					Optional[int]			=      textwrap.dedent(
         "\\n        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n        Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\n        Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\n        Second paragraph:\n        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n        Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\n        Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n        "			)
     with open(A__		,		"w"		,		encoding="utf-8"			) as f:
          f.write(A__			)
     return str(A__			)







@pytest.fixture
def 	A (		A__		,		A__			):
     UpperCAmelCase    :					str			=      tmp_path / "text_with_image.txt"
     with open(A__		,		"w"		,		encoding="utf-8"			) as f:
          f.write(A__			)
     return str(A__			)







@pytest.mark.parametrize("keep_linebreaks"		,		[True, False]			)
def 	A (		A__		,		A__			):
     with open(A__		,		encoding="utf-8"			) as f:
          UpperCAmelCase    :					Union[str, Any]			=      f.read().splitlines(keepends=A__			)
     UpperCAmelCase    :					int			=      Text(keep_linebreaks=A__		,		encoding="utf-8"			)
     UpperCAmelCase    :					Tuple			=      text._generate_tables([[text_file]]			)
     UpperCAmelCase    :					Optional[Any]			=      pa.concat_tables([table for _, table in generator]			).to_pydict()["text"]
     assert generated_content == expected_content







@require_pil
def 	A (		A__			):
     with open(A__		,		encoding="utf-8"			) as f:
          UpperCAmelCase    :					str			=      f.read().splitlines()[0]
     UpperCAmelCase    :					List[str]			=      Text(encoding="utf-8"		,		features=Features({"image": Image()}			)			)
     UpperCAmelCase    :					Union[str, Any]			=      text._generate_tables([[text_file_with_image]]			)
     UpperCAmelCase    :					Union[str, Any]			=      pa.concat_tables([table for _, table in generator]			)
     assert pa_table.schema.field("image"			).type == Image()()
     UpperCAmelCase    :					Optional[Any]			=      pa_table.to_pydict()["image"]
     assert generated_content == [{"path": image_file, "bytes": None}]







@pytest.mark.parametrize("sample_by"		,		["line", "paragraph", "document"]			)
def 	A (		A__		,		A__			):
     with open(A__		,		encoding="utf-8"			) as f:
          UpperCAmelCase    :					Optional[Any]			=      f.read()
     if sample_by == "line":
          UpperCAmelCase    :					Optional[int]			=      expected_content.splitlines()
     elif sample_by == "paragraph":
          UpperCAmelCase    :					Any			=      expected_content.split("\n\n"			)
     elif sample_by == "document":
          UpperCAmelCase    :					List[str]			=      [expected_content]
     UpperCAmelCase    :					Any			=      Text(sample_by=A__		,		encoding="utf-8"		,		chunksize=1_0_0			)
     UpperCAmelCase    :					List[Any]			=      text._generate_tables([[text_file]]			)
     UpperCAmelCase    :					Dict			=      pa.concat_tables([table for _, table in generator]			).to_pydict()["text"]
     assert generated_content == expected_content







import subprocess
from argparse import ArgumentParser
from typing import List, Union

from huggingface_hub.hf_api import HfFolder, create_repo, whoami
from requests.exceptions import HTTPError

from . import BaseTransformersCLICommand




class  A_ (			a_  ):







 """simple docstring"""





 @staticmethod
 def 	_UpperCamelCase     (      _a       :			ArgumentParser					)     ->      str:
      UpperCAmelCase    :					str			=      parser.add_parser("login"							,     help="Log in using the same credentials as on huggingface.co"					)
      login_parser.set_defaults(func=lambda _a					: LoginCommand(_a					)					)
      UpperCAmelCase    :					str			=      parser.add_parser("whoami"							,     help="Find out which huggingface.co account you are logged in as."					)
      whoami_parser.set_defaults(func=lambda _a					: WhoamiCommand(_a					)					)
      UpperCAmelCase    :					List[Any]			=      parser.add_parser("logout"							,     help="Log out"					)
      logout_parser.set_defaults(func=lambda _a					: LogoutCommand(_a					)					)

      # new system: git-based repo system
      UpperCAmelCase    :					List[str]			=      parser.add_parser(
          "repo"							,     help="Deprecated: use `huggingface-cli` instead. Commands to interact with your huggingface.co repos."							,     )
      UpperCAmelCase    :					List[str]			=      repo_parser.add_subparsers(
          help="Deprecated: use `huggingface-cli` instead. huggingface.co repos related commands"					)
      UpperCAmelCase    :					Optional[Any]			=      repo_subparsers.add_parser(
          "create"							,     help="Deprecated: use `huggingface-cli` instead. Create a new repo on huggingface.co"					)
      repo_create_parser.add_argument(
          "name"							,     type=_a							,     help="Name for your model's repo. Will be namespaced under your username to build the model id."							,     )
      repo_create_parser.add_argument("--organization"							,     type=_a							,     help="Optional: organization namespace."					)
      repo_create_parser.add_argument("-y"							,     "--yes"							,     action="store_true"							,     help="Optional: answer Yes to the prompt"					)
      repo_create_parser.set_defaults(func=lambda _a					: RepoCreateCommand(_a					)					)




class  A_ :







 """simple docstring"""





 _UpperCamelCase					:			str     	=       '''\u001b[1m'''
 _UpperCamelCase					:			Optional[Any]     	=       '''\u001b[31m'''
 _UpperCamelCase					:			List[Any]     	=       '''\u001b[90m'''
 _UpperCamelCase					:			Tuple     	=       '''\u001b[0m'''
 @classmethod
 def 	_UpperCamelCase     (      cls       :			Any							,     _a       :			Tuple					)     ->      Any:
      return f'{cls._bold}{s}{cls._reset}'
 @classmethod
 def 	_UpperCamelCase     (      cls       :			Optional[Any]							,     _a       :			List[Any]					)     ->      str:
      return f'{cls._bold}{cls._red}{s}{cls._reset}'

 @classmethod
 def 	_UpperCamelCase     (      cls       :			str							,     _a       :			Optional[Any]					)     ->      Tuple:
      return f'{cls._gray}{s}{cls._reset}'







def 	A (		A__		,		A__			):
     UpperCAmelCase    :					str			=      [max(len(str(A__			)			) for x in col			) for col in zip(*A__		,		A__			)]
     UpperCAmelCase    :					str			=      ("{{:{}}} " * len(A__			)).format(*A__			)
     UpperCAmelCase    :					Dict			=      []
     lines.append(row_format.format(*A__			)			)
     lines.append(row_format.format(*["-" * w for w in col_widths]			)			)
     for row in rows:
          lines.append(row_format.format(*A__			)			)
     return "\n".join(A__			)




class  A_ :







 """simple docstring"""





 def __init__(      self       :			List[str]							,     _a       :			str					)     ->      str:
      UpperCAmelCase    :					str			=      args




class  A_ (			a_  ):







 """simple docstring"""





 def 	_UpperCamelCase     (      self       :			Optional[int]					)     ->      Any:
      print(
          ANSI.red(
              "ERROR! `huggingface-cli login` uses an outdated login mechanism "
              "that is not compatible with the Hugging Face Hub backend anymore. "
              "Please use `huggingface-cli login instead."					)					)




class  A_ (			a_  ):







 """simple docstring"""





 def 	_UpperCamelCase     (      self       :			Tuple					)     ->      Tuple:
      print(
          ANSI.red(
              "WARNING! `transformers-cli whoami` is deprecated and will be removed in v5. Please use "
              "`huggingface-cli whoami` instead."					)					)
      UpperCAmelCase    :					Dict			=      HfFolder.get_token()
      if token is None:
           print("Not logged in"					)
           exit()
      try:
           UpperCAmelCase	,			UpperCAmelCase    :					Optional[int]			=      whoami(_a					)
           print(_a					)
           if orgs:
                print(ANSI.bold("orgs: "					)							,     ",".join(_a					)					)
      except HTTPError as e:
           print(_a					)
           print(ANSI.red(e.response.text					)					)
           exit(1					)




class  A_ (			a_  ):







 """simple docstring"""





 def 	_UpperCamelCase     (      self       :			List[Any]					)     ->      int:
      print(
          ANSI.red(
              "ERROR! `transformers-cli logout` uses an outdated logout mechanism "
              "that is not compatible with the Hugging Face Hub backend anymore. "
              "Please use `huggingface-cli logout instead."					)					)




class  A_ (			a_  ):







 """simple docstring"""





 def 	_UpperCamelCase     (      self       :			List[str]					)     ->      Union[str, Any]:
      print(
          ANSI.red(
              "WARNING! Managing repositories through transformers-cli is deprecated. "
              "Please use `huggingface-cli` instead."					)					)
      UpperCAmelCase    :					List[Any]			=      HfFolder.get_token()
      if token is None:
           print("Not logged in"					)
           exit(1					)
      try:
           UpperCAmelCase    :					List[str]			=      subprocess.check_output(["git", "--version"]					).decode("utf-8"					)
           print(ANSI.gray(stdout.strip()					)					)
      except FileNotFoundError:
           print("Looks like you do not have git installed, please install."					)

      try:
           UpperCAmelCase    :					Tuple			=      subprocess.check_output(["git-lfs", "--version"]					).decode("utf-8"					)
           print(ANSI.gray(stdout.strip()					)					)
      except FileNotFoundError:
           print(
               ANSI.red(
                   "Looks like you do not have git-lfs installed, please install."
                   " You can install from https://git-lfs.github.com/."
                   " Then run `git lfs install` (you only have to do this once)."					)					)
      print(""					)

      UpperCAmelCase	,			UpperCAmelCase    :					Tuple			=      whoami(_a					)
      UpperCAmelCase    :					Union[str, Any]			=      self.args.organization if self.args.organization is not None else user
      UpperCAmelCase    :					Dict			=      f'{namespace}/{self.args.name}'
      print(f'You are about to create {ANSI.bold(_a					)}'					)

      if not self.args.yes:
           UpperCAmelCase    :					Any			=      input("Proceed? [Y/n] "					).lower()
           if not (choice == "" or choice == "y" or choice == "yes"):
                print("Abort"					)
                exit()
      try:
           UpperCAmelCase    :					List[Any]			=      create_repo(_a							,     name=self.args.name							,     organization=self.args.organization					)
      except HTTPError as e:
           print(_a					)
           print(ANSI.red(e.response.text					)					)
           exit(1					)
      print("\nYour repo now lives at:"					)
      print(f'  {ANSI.bold(_a					)}'					)
      print("\nYou can clone it locally with the command below, and commit/push as usual."					)
      print(f'\n  git clone {url}'					)
      print(""					)



