2022-01-26 13:44:41 +08:00
|
|
|
import os
|
2021-11-02 00:40:51 +08:00
|
|
|
import tempfile
|
2022-01-26 13:44:41 +08:00
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
|
|
|
import huggingface_hub
|
2022-12-21 05:27:14 +08:00
|
|
|
import pytest
|
2021-11-02 00:40:51 +08:00
|
|
|
|
2022-01-21 21:44:12 +08:00
|
|
|
import gradio as gr
|
|
|
|
from gradio import flagging
|
|
|
|
|
2022-08-26 04:23:28 +08:00
|
|
|
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
|
|
|
|
2022-03-26 02:14:42 +08:00
|
|
|
|
2022-11-08 08:37:55 +08:00
|
|
|
class TestDefaultFlagging:
|
2022-03-26 02:12:45 +08:00
|
|
|
def test_default_flagging_callback(self):
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
|
|
io = gr.Interface(lambda x: x, "text", "text", flagging_dir=tmpdirname)
|
|
|
|
io.launch(prevent_thread_lock=True)
|
|
|
|
row_count = io.flagging_callback.flag(["test", "test"])
|
2022-11-08 08:37:55 +08:00
|
|
|
assert row_count == 1 # 2 rows written including header
|
2022-03-26 02:12:45 +08:00
|
|
|
row_count = io.flagging_callback.flag(["test", "test"])
|
2022-11-08 08:37:55 +08:00
|
|
|
assert row_count == 2 # 3 rows written including header
|
2022-03-26 02:12:45 +08:00
|
|
|
io.close()
|
2021-11-02 00:40:51 +08:00
|
|
|
|
2022-01-24 12:54:48 +08:00
|
|
|
|
2022-11-08 08:37:55 +08:00
|
|
|
class TestSimpleFlagging:
|
2022-03-26 02:12:45 +08:00
|
|
|
def test_simple_csv_flagging_callback(self):
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
|
|
io = gr.Interface(
|
|
|
|
lambda x: x,
|
|
|
|
"text",
|
|
|
|
"text",
|
|
|
|
flagging_dir=tmpdirname,
|
|
|
|
flagging_callback=flagging.SimpleCSVLogger(),
|
|
|
|
)
|
|
|
|
io.launch(prevent_thread_lock=True)
|
|
|
|
row_count = io.flagging_callback.flag(["test", "test"])
|
2022-11-08 08:37:55 +08:00
|
|
|
assert row_count == 0 # no header in SimpleCSVLogger
|
2022-03-26 02:12:45 +08:00
|
|
|
row_count = io.flagging_callback.flag(["test", "test"])
|
2022-11-08 08:37:55 +08:00
|
|
|
assert row_count == 1 # no header in SimpleCSVLogger
|
2022-03-26 02:12:45 +08:00
|
|
|
io.close()
|
2021-11-02 00:40:51 +08:00
|
|
|
|
2022-02-09 02:56:13 +08:00
|
|
|
|
2022-11-08 08:37:55 +08:00
|
|
|
class TestHuggingFaceDatasetSaver:
|
2022-03-26 02:12:45 +08:00
|
|
|
def test_saver_setup(self):
|
2023-01-21 12:15:45 +08:00
|
|
|
huggingface_hub.get_full_repo_name = MagicMock(return_value="test/test")
|
2022-03-26 02:12:45 +08:00
|
|
|
huggingface_hub.create_repo = MagicMock()
|
|
|
|
huggingface_hub.Repository = MagicMock()
|
|
|
|
flagger = flagging.HuggingFaceDatasetSaver("test", "test")
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
|
|
flagger.setup([gr.Audio, gr.Textbox], tmpdirname)
|
|
|
|
huggingface_hub.create_repo.assert_called_once()
|
2022-02-09 02:56:13 +08:00
|
|
|
|
2022-03-26 02:12:45 +08:00
|
|
|
def test_saver_flag(self):
|
2023-01-21 12:15:45 +08:00
|
|
|
huggingface_hub.get_full_repo_name = MagicMock(return_value="test/test")
|
2022-03-26 02:12:45 +08:00
|
|
|
huggingface_hub.create_repo = MagicMock()
|
|
|
|
huggingface_hub.Repository = MagicMock()
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
|
|
io = gr.Interface(
|
|
|
|
lambda x: x,
|
|
|
|
"text",
|
|
|
|
"text",
|
|
|
|
flagging_dir=tmpdirname,
|
|
|
|
flagging_callback=flagging.HuggingFaceDatasetSaver("test", "test"),
|
|
|
|
)
|
|
|
|
os.mkdir(os.path.join(tmpdirname, "test"))
|
|
|
|
io.launch(prevent_thread_lock=True)
|
2023-03-01 02:29:34 +08:00
|
|
|
row_count = io.flagging_callback.flag(["test", "test"], "")
|
2022-11-08 08:37:55 +08:00
|
|
|
assert row_count == 1 # 2 rows written including header
|
2022-03-26 02:12:45 +08:00
|
|
|
row_count = io.flagging_callback.flag(["test", "test"])
|
2022-11-08 08:37:55 +08:00
|
|
|
assert row_count == 2 # 3 rows written including header
|
2022-02-09 02:56:13 +08:00
|
|
|
|
|
|
|
|
2022-11-08 08:37:55 +08:00
|
|
|
class TestHuggingFaceDatasetJSONSaver:
|
2022-08-24 07:01:37 +08:00
|
|
|
def test_saver_setup(self):
|
2023-01-21 12:15:45 +08:00
|
|
|
huggingface_hub.get_full_repo_name = MagicMock(return_value="test/test")
|
2022-08-24 07:01:37 +08:00
|
|
|
huggingface_hub.create_repo = MagicMock()
|
|
|
|
huggingface_hub.Repository = MagicMock()
|
|
|
|
flagger = flagging.HuggingFaceDatasetJSONSaver("test", "test")
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
|
|
flagger.setup([gr.Audio, gr.Textbox], tmpdirname)
|
|
|
|
huggingface_hub.create_repo.assert_called_once()
|
|
|
|
|
|
|
|
def test_saver_flag(self):
|
2023-01-21 12:15:45 +08:00
|
|
|
huggingface_hub.get_full_repo_name = MagicMock(return_value="test/test")
|
2022-08-24 07:01:37 +08:00
|
|
|
huggingface_hub.create_repo = MagicMock()
|
|
|
|
huggingface_hub.Repository = MagicMock()
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
|
|
io = gr.Interface(
|
|
|
|
lambda x: x,
|
|
|
|
"text",
|
|
|
|
"text",
|
|
|
|
flagging_dir=tmpdirname,
|
|
|
|
flagging_callback=flagging.HuggingFaceDatasetJSONSaver("test", "test"),
|
|
|
|
)
|
|
|
|
test_dir = os.path.join(tmpdirname, "test")
|
|
|
|
os.mkdir(test_dir)
|
|
|
|
io.launch(prevent_thread_lock=True)
|
|
|
|
row_unique_name = io.flagging_callback.flag(["test", "test"])
|
|
|
|
# Test existence of metadata.jsonl file for that example
|
2022-11-08 08:37:55 +08:00
|
|
|
assert os.path.isfile(
|
|
|
|
os.path.join(os.path.join(test_dir, row_unique_name), "metadata.jsonl")
|
2022-08-24 07:01:37 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-11-08 08:37:55 +08:00
|
|
|
class TestDisableFlagging:
|
2022-02-15 18:14:47 +08:00
|
|
|
def test_flagging_no_permission_error_with_flagging_disabled(self):
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
|
|
os.chmod(tmpdirname, 0o444) # Make directory read-only
|
2022-02-16 08:32:42 +08:00
|
|
|
nonwritable_path = os.path.join(tmpdirname, "flagging_dir")
|
|
|
|
|
2022-02-15 18:14:47 +08:00
|
|
|
io = gr.Interface(
|
|
|
|
lambda x: x,
|
|
|
|
"text",
|
|
|
|
"text",
|
|
|
|
allow_flagging="never",
|
2022-02-16 08:32:42 +08:00
|
|
|
flagging_dir=nonwritable_path,
|
2022-02-15 18:14:47 +08:00
|
|
|
)
|
|
|
|
try:
|
|
|
|
io.launch(prevent_thread_lock=True)
|
|
|
|
except PermissionError:
|
|
|
|
self.fail("launch() raised a PermissionError unexpectedly")
|
2022-02-16 08:32:42 +08:00
|
|
|
|
2022-02-15 18:14:47 +08:00
|
|
|
io.close()
|
2022-12-21 05:27:14 +08:00
|
|
|
|
|
|
|
|
2023-03-01 02:29:34 +08:00
|
|
|
class TestInterfaceSetsUpFlagging:
|
2022-12-21 05:27:14 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"allow_flagging, called",
|
|
|
|
[
|
|
|
|
("manual", True),
|
|
|
|
("auto", True),
|
|
|
|
("never", False),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_flag_method_init_called(self, allow_flagging, called):
|
|
|
|
flagging.FlagMethod.__init__ = MagicMock()
|
|
|
|
flagging.FlagMethod.__init__.return_value = None
|
|
|
|
gr.Interface(lambda x: x, "text", "text", allow_flagging=allow_flagging)
|
|
|
|
assert flagging.FlagMethod.__init__.called == called
|
2023-03-01 02:29:34 +08:00
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"options, processed_options",
|
|
|
|
[
|
|
|
|
(None, [("Flag", "")]),
|
|
|
|
(["yes", "no"], [("Flag as yes", "yes"), ("Flag as no", "no")]),
|
|
|
|
([("abc", "de"), ("123", "45")], [("abc", "de"), ("123", "45")]),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_flagging_options_processed_correctly(self, options, processed_options):
|
|
|
|
io = gr.Interface(lambda x: x, "text", "text", flagging_options=options)
|
|
|
|
assert io.flagging_options == processed_options
|