mirror of
https://github.com/gradio-app/gradio.git
synced 2024-11-27 01:40:20 +08:00
Removes special characters from temporary filenames (#2480)
* remove special chars * tests * tests
This commit is contained in:
parent
dae17bb75a
commit
081f63c5c5
@ -68,6 +68,7 @@ gr.Interface(iteration,
|
||||
## Bug Fixes:
|
||||
* Add loading status tracker UI to HTML and Markdown components. [@pngwn](https://github.com/pngwn) in [PR 2474](https://github.com/gradio-app/gradio/pull/2474)
|
||||
* Fixed videos being mirrored in the front-end if source is not webcam by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 2475](https://github.com/gradio-app/gradio/pull/2475)
|
||||
* Removes special characters from temporary filenames so that the files can be served by components [@abidlabs](https://github.com/abidlabs) in [PR 2480](https://github.com/gradio-app/gradio/pull/2480)
|
||||
|
||||
## Documentation Changes:
|
||||
No changes to highlight.
|
||||
|
@ -329,6 +329,7 @@ def create_tmp_copy_of_file(file_path, dir=None):
|
||||
if "." in file_name:
|
||||
prefix = file_name[0 : file_name.index(".")]
|
||||
extension = file_name[file_name.index(".") + 1 :]
|
||||
prefix = utils.strip_invalid_filename_characters(prefix)
|
||||
if extension is None:
|
||||
file_obj = tempfile.NamedTemporaryFile(delete=False, prefix=prefix, dir=dir)
|
||||
else:
|
||||
|
@ -18,7 +18,7 @@ from gradio import media_data
|
||||
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
||||
|
||||
|
||||
class ImagePreprocessing(unittest.TestCase):
|
||||
class TestImagePreprocessing(unittest.TestCase):
|
||||
def test_decode_base64_to_image(self):
|
||||
output_image = gr.processing_utils.decode_base64_to_image(
|
||||
deepcopy(media_data.BASE64_IMAGE)
|
||||
@ -85,7 +85,7 @@ class ImagePreprocessing(unittest.TestCase):
|
||||
)
|
||||
|
||||
|
||||
class AudioPreprocessing(unittest.TestCase):
|
||||
class TestAudioPreprocessing(unittest.TestCase):
|
||||
def test_audio_from_file(self):
|
||||
audio = gr.processing_utils.audio_from_file("gradio/test_data/test_audio.wav")
|
||||
self.assertEqual(audio[0], 22050)
|
||||
@ -98,7 +98,7 @@ class AudioPreprocessing(unittest.TestCase):
|
||||
os.remove("test_audio_to_file")
|
||||
|
||||
|
||||
class OutputPreprocessing(unittest.TestCase):
|
||||
class TestOutputPreprocessing(unittest.TestCase):
|
||||
def test_decode_base64_to_binary(self):
|
||||
binary = gr.processing_utils.decode_base64_to_binary(
|
||||
deepcopy(media_data.BASE64_IMAGE)
|
||||
@ -116,6 +116,33 @@ class OutputPreprocessing(unittest.TestCase):
|
||||
temp_file = gr.processing_utils.create_tmp_copy_of_file(f.name)
|
||||
self.assertIsInstance(temp_file, tempfile._TemporaryFileWrapper)
|
||||
|
||||
@patch("shutil.copy2")
|
||||
def test_create_tmp_filenames(self, mock_copy2):
|
||||
filepath = "C:/gradio/test_image.png"
|
||||
file_obj = gr.processing_utils.create_tmp_copy_of_file(filepath)
|
||||
assert "test_image" in file_obj.name
|
||||
assert file_obj.name.endswith(".png")
|
||||
|
||||
filepath = "ABCabc123.csv"
|
||||
file_obj = gr.processing_utils.create_tmp_copy_of_file(filepath)
|
||||
assert "ABCabc123" in file_obj.name
|
||||
assert file_obj.name.endswith(".csv")
|
||||
|
||||
filepath = "lion#1.jpeg"
|
||||
file_obj = gr.processing_utils.create_tmp_copy_of_file(filepath)
|
||||
assert "lion1" in file_obj.name
|
||||
assert file_obj.name.endswith(".jpeg")
|
||||
|
||||
filepath = "%%lio|n#1.jpeg"
|
||||
file_obj = gr.processing_utils.create_tmp_copy_of_file(filepath)
|
||||
assert "lion1" in file_obj.name
|
||||
assert file_obj.name.endswith(".jpeg")
|
||||
|
||||
filepath = "/home/lion--_1.txt"
|
||||
file_obj = gr.processing_utils.create_tmp_copy_of_file(filepath)
|
||||
assert "lion--_1" in file_obj.name
|
||||
assert file_obj.name.endswith(".txt")
|
||||
|
||||
float_dtype_list = [
|
||||
float,
|
||||
float,
|
||||
@ -134,8 +161,8 @@ class OutputPreprocessing(unittest.TestCase):
|
||||
# Test all combinations of dtypes conversions
|
||||
dtype_combin = np.array(
|
||||
np.meshgrid(
|
||||
OutputPreprocessing.float_dtype_list,
|
||||
OutputPreprocessing.float_dtype_list,
|
||||
TestOutputPreprocessing.float_dtype_list,
|
||||
TestOutputPreprocessing.float_dtype_list,
|
||||
)
|
||||
).T.reshape(-1, 2)
|
||||
|
||||
@ -147,67 +174,67 @@ class OutputPreprocessing(unittest.TestCase):
|
||||
def test_subclass_conversion(self):
|
||||
"""Check subclass conversion behavior"""
|
||||
x = np.array([-1, 1])
|
||||
for dtype in OutputPreprocessing.float_dtype_list:
|
||||
for dtype in TestOutputPreprocessing.float_dtype_list:
|
||||
x = x.astype(dtype)
|
||||
y = gr.processing_utils._convert(x, np.floating)
|
||||
assert y.dtype == x.dtype
|
||||
|
||||
|
||||
def test_video_has_playable_codecs(test_file_dir):
|
||||
assert gr.processing_utils.video_is_playable(
|
||||
str(test_file_dir / "video_sample.mp4")
|
||||
)
|
||||
assert gr.processing_utils.video_is_playable(
|
||||
str(test_file_dir / "video_sample.ogg")
|
||||
)
|
||||
assert gr.processing_utils.video_is_playable(
|
||||
str(test_file_dir / "video_sample.webm")
|
||||
)
|
||||
assert not gr.processing_utils.video_is_playable(
|
||||
str(test_file_dir / "bad_video_sample.mp4")
|
||||
)
|
||||
class TestVideoProcessing:
|
||||
def test_video_has_playable_codecs(self, test_file_dir):
|
||||
assert gr.processing_utils.video_is_playable(
|
||||
str(test_file_dir / "video_sample.mp4")
|
||||
)
|
||||
assert gr.processing_utils.video_is_playable(
|
||||
str(test_file_dir / "video_sample.ogg")
|
||||
)
|
||||
assert gr.processing_utils.video_is_playable(
|
||||
str(test_file_dir / "video_sample.webm")
|
||||
)
|
||||
assert not gr.processing_utils.video_is_playable(
|
||||
str(test_file_dir / "bad_video_sample.mp4")
|
||||
)
|
||||
|
||||
def raise_ffmpy_runtime_exception(*args, **kwargs):
|
||||
raise ffmpy.FFRuntimeError("", "", "", "")
|
||||
|
||||
def raise_ffmpy_runtime_exception(*args, **kwargs):
|
||||
raise ffmpy.FFRuntimeError("", "", "", "")
|
||||
@pytest.mark.parametrize(
|
||||
"exception_to_raise", [raise_ffmpy_runtime_exception, KeyError(), IndexError()]
|
||||
)
|
||||
def test_video_has_playable_codecs_catches_exceptions(
|
||||
self, exception_to_raise, test_file_dir
|
||||
):
|
||||
with patch("ffmpy.FFprobe.run", side_effect=exception_to_raise):
|
||||
with tempfile.NamedTemporaryFile(suffix="out.avi") as tmp_not_playable_vid:
|
||||
shutil.copy(
|
||||
str(test_file_dir / "bad_video_sample.mp4"),
|
||||
tmp_not_playable_vid.name,
|
||||
)
|
||||
assert gr.processing_utils.video_is_playable(tmp_not_playable_vid.name)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"exception_to_raise", [raise_ffmpy_runtime_exception, KeyError(), IndexError()]
|
||||
)
|
||||
def test_video_has_playable_codecs_catches_exceptions(
|
||||
exception_to_raise, test_file_dir
|
||||
):
|
||||
with patch("ffmpy.FFprobe.run", side_effect=exception_to_raise):
|
||||
def test_convert_video_to_playable_mp4(self, test_file_dir):
|
||||
with tempfile.NamedTemporaryFile(suffix="out.avi") as tmp_not_playable_vid:
|
||||
shutil.copy(
|
||||
str(test_file_dir / "bad_video_sample.mp4"), tmp_not_playable_vid.name
|
||||
)
|
||||
assert gr.processing_utils.video_is_playable(tmp_not_playable_vid.name)
|
||||
playable_vid = gr.processing_utils.convert_video_to_playable_mp4(
|
||||
tmp_not_playable_vid.name
|
||||
)
|
||||
assert gr.processing_utils.video_is_playable(playable_vid)
|
||||
|
||||
|
||||
def test_convert_video_to_playable_mp4(test_file_dir):
|
||||
with tempfile.NamedTemporaryFile(suffix="out.avi") as tmp_not_playable_vid:
|
||||
shutil.copy(
|
||||
str(test_file_dir / "bad_video_sample.mp4"), tmp_not_playable_vid.name
|
||||
)
|
||||
playable_vid = gr.processing_utils.convert_video_to_playable_mp4(
|
||||
tmp_not_playable_vid.name
|
||||
)
|
||||
assert gr.processing_utils.video_is_playable(playable_vid)
|
||||
|
||||
|
||||
@patch("ffmpy.FFmpeg.run", side_effect=raise_ffmpy_runtime_exception)
|
||||
def test_video_conversion_returns_original_video_if_fails(mock_run, test_file_dir):
|
||||
with tempfile.NamedTemporaryFile(suffix="out.avi") as tmp_not_playable_vid:
|
||||
shutil.copy(
|
||||
str(test_file_dir / "bad_video_sample.mp4"), tmp_not_playable_vid.name
|
||||
)
|
||||
playable_vid = gr.processing_utils.convert_video_to_playable_mp4(
|
||||
tmp_not_playable_vid.name
|
||||
)
|
||||
# If the conversion succeeded it'd be .mp4
|
||||
assert pathlib.Path(playable_vid).suffix == ".avi"
|
||||
@patch("ffmpy.FFmpeg.run", side_effect=raise_ffmpy_runtime_exception)
|
||||
def test_video_conversion_returns_original_video_if_fails(
|
||||
self, mock_run, test_file_dir
|
||||
):
|
||||
with tempfile.NamedTemporaryFile(suffix="out.avi") as tmp_not_playable_vid:
|
||||
shutil.copy(
|
||||
str(test_file_dir / "bad_video_sample.mp4"), tmp_not_playable_vid.name
|
||||
)
|
||||
playable_vid = gr.processing_utils.convert_video_to_playable_mp4(
|
||||
tmp_not_playable_vid.name
|
||||
)
|
||||
# If the conversion succeeded it'd be .mp4
|
||||
assert pathlib.Path(playable_vid).suffix == ".avi"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Loading…
Reference in New Issue
Block a user