


import pytest
import requests

from datasets.utils.file_utils import http_head

from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline



@pytest.mark.integration
def 				UpperCamelCase_(      )    ->						Any:
    with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT ):
        with pytest.raises(lowerCamelCase_ ):
            requests.request('GET'							, 'https://huggingface.co' )
        with pytest.raises(requests.exceptions.ConnectTimeout ):
            requests.request('GET'							, 'https://huggingface.co'							, timeout=1.0 )



@pytest.mark.integration
def 				UpperCamelCase_(      )    ->						Any:
    with offline(OfflineSimulationMode.CONNECTION_FAILS ):
        with pytest.raises(requests.exceptions.ConnectionError ):
            requests.request('GET'							, 'https://huggingface.co' )



def 				UpperCamelCase_(      )    ->						Optional[Any]:
    with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1 ):
        with pytest.raises(lowerCamelCase_ ):
            http_head('https://huggingface.co' )






from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
 from PIL import Image

 from ..image_utils import load_image

if is_tf_available():
 import tensorflow as tf

 from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
 from ..tf_utils import stable_softmax

if is_torch_available():
 from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING

SCREAMING_SNAKE_CASE       :      List[str]												= logging.get_logger(__name__)






@add_end_docstrings(_a   )
class _lowerCamelCase(						_a   ):
       def __init__(					self,      *lowerCamelCase,      **lowerCamelCase)       ->   int:




           """simple docstring"""

           super().__init__(*lowerCamelCase,      **lowerCamelCase)
           requires_backends(self,      'vision')
           self.check_model_type(
               TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
               if self.framework == 'tf'
               else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING)
       def 			UpperCamelCase     (					self,      lowerCamelCase=None)       ->   int:




           """simple docstring"""

           _lowercase       :							Dict      		=		{}
           if top_k is not None:
               _lowercase       :							List[str]      		=		top_k
           return {}, {}, postprocess_params
       def __call__(					self,      lowerCamelCase,      **lowerCamelCase)       ->   Tuple:




           """simple docstring"""

           return super().__call__(lowerCamelCase,      **lowerCamelCase)
       def 			UpperCamelCase     (					self,      lowerCamelCase)       ->   str:




           """simple docstring"""

           _lowercase       :							Optional[Any]      		=		load_image(lowerCamelCase)
           _lowercase       :							List[str]      		=		self.image_processor(images=lowerCamelCase,      return_tensors=self.framework)
           return model_inputs
       def 			UpperCamelCase     (					self,      lowerCamelCase)       ->   List[str]:




           """simple docstring"""

           _lowercase       :							Optional[int]      		=		self.model(**lowerCamelCase)
           return model_outputs




       def 			UpperCamelCase     (					self,      lowerCamelCase,      lowerCamelCase=5)       ->   Dict:




           """simple docstring"""

           if top_k > self.model.config.num_labels:
               _lowercase       :							List[Any]      		=		self.model.config.num_labels

           if self.framework == "pt":
               _lowercase       :							int      		=		model_outputs.logits.softmax(-1)[0]
               _lowercase		,				_lowercase       :							Union[str, Any]      		=		probs.topk(lowerCamelCase)
           elif self.framework == "tf":
               _lowercase       :							int      		=		stable_softmax(model_outputs.logits,      axis=-1)[0]
               _lowercase       :							List[Any]      		=		tf.math.top_k(lowerCamelCase,      k=lowerCamelCase)
               _lowercase		,				_lowercase       :							Any      		=		topk.values.numpy(), topk.indices.numpy()
           else:
               raise ValueError(F'''Unsupported framework: {self.framework}''')

           _lowercase       :							str      		=		scores.tolist()
           _lowercase       :							str      		=		ids.tolist()
           return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(lowerCamelCase,      lowerCamelCase)]


