



# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re

from ..utils import cached_file


# docstyle-ignore
a_			=					"""
Human: <<task>>

Assistant: """


a_			=					"""huggingface-tools/default-prompts"""
a_			=					{"""chat""": """chat_prompt_template.txt""", """run""": """run_prompt_template.txt"""}





def 		a__			(		_UpperCamelCase			: Optional[Any] ,_UpperCamelCase			: Optional[int] ,_UpperCamelCase			: Any="run"							):
   if prompt_or_repo_id is None:
      __lowerCamelCase													=    DEFAULT_PROMPTS_REPO

   # prompt is considered a repo ID when it does not contain any kind of space
   if re.search('''\\s''' ,_UpperCamelCase							) is not None:
      return prompt_or_repo_id

   __lowerCamelCase													=    cached_file(
       _UpperCamelCase ,PROMPT_FILES[mode] ,repo_type='''dataset''' ,user_agent={'''agent''': agent_name}							)
   with open(_UpperCamelCase ,'''r''' ,encoding='''utf-8'''							) as f:
      return f.read()






import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


a_			=					False



class  __lowerCAmelCase     (	unittest.TestCase   ):
 pass



@nightly
@require_torch_gpu
class  __lowerCAmelCase     (	unittest.TestCase   ):




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    # clean up the VRAM after each test
    super().tearDown()
    gc.collect()
    torch.cuda.empty_cache()




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion'''	,				torch_dtype=torch.floataa				)
    pipe.to(__UpperCAmelCase				)
    pipe.set_progress_bar_config(disable=__UpperCAmelCase				)

    __lowerCamelCase													=    load_image(
        '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''				)

    __lowerCamelCase													=    torch.manual_seed(0				)
    __lowerCamelCase													=    pipe.dual_guided(
        prompt='''first prompt'''	,				image=__UpperCAmelCase	,				text_to_image_strength=0.75	,				generator=__UpperCAmelCase	,				guidance_scale=7.5	,				num_inference_steps=2	,				output_type='''numpy'''	,				).images

    with tempfile.TemporaryDirectory() as tmpdirname:
       pipe.save_pretrained(__UpperCAmelCase				)
       __lowerCamelCase													=    VersatileDiffusionPipeline.from_pretrained(__UpperCAmelCase	,				torch_dtype=torch.floataa				)
    pipe.to(__UpperCAmelCase				)
    pipe.set_progress_bar_config(disable=__UpperCAmelCase				)

    __lowerCamelCase													=    generator.manual_seed(0				)
    __lowerCamelCase													=    pipe.dual_guided(
        prompt='''first prompt'''	,				image=__UpperCAmelCase	,				text_to_image_strength=0.75	,				generator=__UpperCAmelCase	,				guidance_scale=7.5	,				num_inference_steps=2	,				output_type='''numpy'''	,				).images

    assert np.abs(image - new_image				).sum() < 1E-5, "Models don't have the same forward pass"




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    VersatileDiffusionPipeline.from_pretrained('''shi-labs/versatile-diffusion'''	,				torch_dtype=torch.floataa				)
    pipe.to(__UpperCAmelCase				)
    pipe.set_progress_bar_config(disable=__UpperCAmelCase				)

    __lowerCamelCase													=    '''cyberpunk 2077'''
    __lowerCamelCase													=    load_image(
        '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''				)
    __lowerCamelCase													=    torch.manual_seed(0				)
    __lowerCamelCase													=    pipe.dual_guided(
        prompt=__UpperCAmelCase	,				image=__UpperCAmelCase	,				text_to_image_strength=0.75	,				generator=__UpperCAmelCase	,				guidance_scale=7.5	,				num_inference_steps=50	,				output_type='''numpy'''	,				).images

    __lowerCamelCase													=    image[0, 253:256, 253:256, -1]

    assert image.shape == (1, 512, 512, 3)
    __lowerCamelCase													=    np.array([0.1_448, 0.1_619, 0.1_741, 0.1_086, 0.1_147, 0.1_128, 0.1_199, 0.1_165, 0.1_001]				)

    assert np.abs(image_slice.flatten() - expected_slice				).max() < 1E-1

    __lowerCamelCase													=    '''A painting of a squirrel eating a burger '''
    __lowerCamelCase													=    torch.manual_seed(0				)
    __lowerCamelCase													=    pipe.text_to_image(
        prompt=__UpperCAmelCase	,				generator=__UpperCAmelCase	,				guidance_scale=7.5	,				num_inference_steps=50	,				output_type='''numpy'''				).images

    __lowerCamelCase													=    image[0, 253:256, 253:256, -1]

    assert image.shape == (1, 512, 512, 3)
    __lowerCamelCase													=    np.array([0.3_367, 0.3_169, 0.2_656, 0.3_870, 0.4_790, 0.3_796, 0.4_009, 0.4_878, 0.4_778]				)

    assert np.abs(image_slice.flatten() - expected_slice				).max() < 1E-1

    __lowerCamelCase													=    pipe.image_variation(__UpperCAmelCase	,				generator=__UpperCAmelCase	,				output_type='''numpy'''				).images

    __lowerCamelCase													=    image[0, 253:256, 253:256, -1]

    assert image.shape == (1, 512, 512, 3)
    __lowerCamelCase													=    np.array([0.3_076, 0.3_123, 0.3_284, 0.3_782, 0.3_770, 0.3_894, 0.4_297, 0.4_331, 0.4_456]				)

    assert np.abs(image_slice.flatten() - expected_slice				).max() < 1E-1






