





import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
       import torch

if is_vision_available():
       from PIL import Image

       from transformers import MobileNetVaImageProcessor





class 			lowerCAmelCase__( unittest.TestCase    ):






       '''simple docstring'''


       def __init__(      self    ,						__lowerCamelCase    ,						__lowerCamelCase=7    ,						__lowerCamelCase=3    ,						__lowerCamelCase=1_8    ,						__lowerCamelCase=3_0    ,						__lowerCamelCase=4_0_0    ,						__lowerCamelCase=True    ,						__lowerCamelCase=None    ,						__lowerCamelCase=True    ,						__lowerCamelCase=None    ,						)			->     List[str]:
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							size if size is not None else {"shortest_edge": 2_0}
            _SCREAMING_SNAKE_CASE       :	Any						    =							crop_size if crop_size is not None else {"height": 1_8, "width": 1_8}
            _SCREAMING_SNAKE_CASE       :	str						    =							parent
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							batch_size
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							num_channels
            _SCREAMING_SNAKE_CASE       :	Any						    =							image_size
            _SCREAMING_SNAKE_CASE       :	Dict						    =							min_resolution
            _SCREAMING_SNAKE_CASE       :	Dict						    =							max_resolution
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							do_resize
            _SCREAMING_SNAKE_CASE       :	str						    =							size
            _SCREAMING_SNAKE_CASE       :	str						    =							do_center_crop
            _SCREAMING_SNAKE_CASE       :	str						    =							crop_size







       def     UpperCamelCase_			(      self							)			->     Dict:
            return {
                "do_resize": self.do_resize,
                "size": self.size,
                "do_center_crop": self.do_center_crop,
                "crop_size": self.crop_size,
            }





@require_torch
@require_vision
class 			lowerCAmelCase__( __lowercase  ,     unittest.TestCase    ):






       '''simple docstring'''


       __snake_case 			=    MobileNetVaImageProcessor if is_vision_available() else None




       def     UpperCamelCase_			(      self							)			->     Tuple:
            _SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							MobileNetVaImageProcessingTester(self							)




       @property
       def     UpperCamelCase_			(      self							)			->     int:
            return self.image_processor_tester.prepare_image_processor_dict()




       def     UpperCamelCase_			(      self							)			->     Optional[Any]:
            _SCREAMING_SNAKE_CASE       :	Any						    =							self.image_processing_class(**self.image_processor_dict							)
            self.assertTrue(hasattr(__lowerCamelCase    ,						"do_resize"							)							)
            self.assertTrue(hasattr(__lowerCamelCase    ,						"size"							)							)
            self.assertTrue(hasattr(__lowerCamelCase    ,						"do_center_crop"							)							)
            self.assertTrue(hasattr(__lowerCamelCase    ,						"crop_size"							)							)




       def     UpperCamelCase_			(      self							)			->     Optional[Any]:
            _SCREAMING_SNAKE_CASE       :	List[str]						    =							self.image_processing_class.from_dict(self.image_processor_dict							)
            self.assertEqual(image_processor.size    ,						{"shortest_edge": 2_0}							)
            self.assertEqual(image_processor.crop_size    ,						{"height": 1_8, "width": 1_8}							)

            _SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							self.image_processing_class.from_dict(self.image_processor_dict    ,						size=4_2    ,						crop_size=8_4							)
            self.assertEqual(image_processor.size    ,						{"shortest_edge": 4_2}							)
            self.assertEqual(image_processor.crop_size    ,						{"height": 8_4, "width": 8_4}							)




       def     UpperCamelCase_			(      self							)			->     List[str]:
            pass




       def     UpperCamelCase_			(      self							)			->     Optional[int]:
            # Initialize image_processing
            _SCREAMING_SNAKE_CASE       :	int						    =							self.image_processing_class(**self.image_processor_dict							)
            # create random PIL images
            _SCREAMING_SNAKE_CASE       :	str						    =							prepare_image_inputs(self.image_processor_tester    ,						equal_resolution=__lowerCamelCase							)
            for image in image_inputs:
                 self.assertIsInstance(__lowerCamelCase    ,						Image.Image							)

            # Test not batched input
            _SCREAMING_SNAKE_CASE       :	Any						    =							image_processing(image_inputs[0]    ,						return_tensors="pt"							).pixel_values
            self.assertEqual(
                encoded_images.shape    ,						(
                    1,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.crop_size["height"],
                    self.image_processor_tester.crop_size["width"],
                )    ,						)

            # Test batched
            _SCREAMING_SNAKE_CASE       :	Tuple						    =							image_processing(__lowerCamelCase    ,						return_tensors="pt"							).pixel_values
            self.assertEqual(
                encoded_images.shape    ,						(
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.crop_size["height"],
                    self.image_processor_tester.crop_size["width"],
                )    ,						)




       def     UpperCamelCase_			(      self							)			->     Any:
            # Initialize image_processing
            _SCREAMING_SNAKE_CASE       :	List[str]						    =							self.image_processing_class(**self.image_processor_dict							)
            # create random numpy tensors
            _SCREAMING_SNAKE_CASE       :	Tuple						    =							prepare_image_inputs(self.image_processor_tester    ,						equal_resolution=__lowerCamelCase    ,						numpify=__lowerCamelCase							)
            for image in image_inputs:
                 self.assertIsInstance(__lowerCamelCase    ,						np.ndarray							)

            # Test not batched input
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							image_processing(image_inputs[0]    ,						return_tensors="pt"							).pixel_values
            self.assertEqual(
                encoded_images.shape    ,						(
                    1,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.crop_size["height"],
                    self.image_processor_tester.crop_size["width"],
                )    ,						)

            # Test batched
            _SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							image_processing(__lowerCamelCase    ,						return_tensors="pt"							).pixel_values
            self.assertEqual(
                encoded_images.shape    ,						(
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.crop_size["height"],
                    self.image_processor_tester.crop_size["width"],
                )    ,						)







       def     UpperCamelCase_			(      self							)			->     Union[str, Any]:
            # Initialize image_processing
            _SCREAMING_SNAKE_CASE       :	Any						    =							self.image_processing_class(**self.image_processor_dict							)
            # create random PyTorch tensors
            _SCREAMING_SNAKE_CASE       :	List[str]						    =							prepare_image_inputs(self.image_processor_tester    ,						equal_resolution=__lowerCamelCase    ,						torchify=__lowerCamelCase							)
            for image in image_inputs:
                 self.assertIsInstance(__lowerCamelCase    ,						torch.Tensor							)

            # Test not batched input
            _SCREAMING_SNAKE_CASE       :	str						    =							image_processing(image_inputs[0]    ,						return_tensors="pt"							).pixel_values
            self.assertEqual(
                encoded_images.shape    ,						(
                    1,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.crop_size["height"],
                    self.image_processor_tester.crop_size["width"],
                )    ,						)

            # Test batched
            _SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							image_processing(__lowerCamelCase    ,						return_tensors="pt"							).pixel_values
            self.assertEqual(
                encoded_images.shape    ,						(
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    self.image_processor_tester.crop_size["height"],
                    self.image_processor_tester.crop_size["width"],
                )    ,						)






from __future__ import annotations

import requests






def     lowerCamelCase__			(__lowerCamelCase	):
     _SCREAMING_SNAKE_CASE       :	Dict						    =							f"""https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty"""
     return requests.get(__lowerCamelCase	).json()






def     lowerCamelCase__			(__lowerCamelCase = 10	):
     _SCREAMING_SNAKE_CASE       :	List[Any]						    =							"https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty"
     _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							requests.get(__lowerCamelCase	).json()[:max_stories]
     return [get_hackernews_story(__lowerCamelCase	) for story_id in story_ids]






def     lowerCamelCase__			(__lowerCamelCase = 10	):
     _SCREAMING_SNAKE_CASE       :	List[str]						    =							hackernews_top_stories(__lowerCamelCase	)
     return "\n".join("* [{title}]({url})".format(**__lowerCamelCase	) for story in stories	)


if __name__ == "__main__":
       print(hackernews_top_stories_as_markdown())