mirror of
https://github.com/gradio-app/gradio.git
synced 2024-11-21 01:01:05 +08:00
88e9c19c27
* remove preprocess_example * removing methods * added path support for images * fixes * video * formatting * fixing preprocess * fixes * removed from audio * fixed file * formatting * serialization * foramtting * formatting * removed save flag / restore flag * formatting * removed flagging * removed * load value * fixing typing * fixes, typing * fixes * file * handling images * formatting * fixed serializing for flagging * formatting * json * temp file * removed processing * changed processing * fixed temp FINALLY * flagging works * fix examples test * formatting * async examples * working on mix * comment out failing test * fixed interface problem * fix kitchen sink deprecation warning * gallery examples * fixes * fixes to serialization * fixing label serializing * fixed file serialization * kitchen sink restored * outbreak forecast updated * formatting * formatting and api mode * fix 1 test :/ * fixing tests * fixed components tests * remvoed test files * formatting * fixed examples * fixes * formatting * restored certain files * added encryption * fixed syntax mistake * formatting * fixed 1 test * clean up interface * formatting * fixed route tests * more fixes * formatting * formatting * fixing pipeline * format frontend * format backend * tweaks * fix * fix final test? * merged * Sanitize for CSV (#2017) * sanitize for csv * added sanitization logic * fixed examples * turn cache off * fixed example caching with optional inputs * fixed review problems * fixed Interface.load * updating the tests * updating the tests * fix * fixed seriailizing * testing * rewrite run prediction * formatting * await * fixes * formatting * finally fixed mix * fixed tests * formatting * formatting * deserialize fix * formatting * fixes * fixes * fix * fix tests * fixes Co-authored-by: Freddy Boulton <alfonsoboulton@gmail.com>
247 lines
10 KiB
Python
247 lines
10 KiB
Python
import json
|
|
import os
|
|
import pathlib
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
import transformers
|
|
|
|
import gradio as gr
|
|
from gradio.external import TooManyRequestsError
|
|
|
|
"""
|
|
WARNING: These tests have an external dependency: namely that Hugging Face's
|
|
Hub and Space APIs do not change, and they keep their most famous models up.
|
|
So if, e.g. Spaces is down, then these test will not pass.
|
|
|
|
These tests actually test gr.Interface.load() and gr.Blocks.load() but are
|
|
included in a separate file because of the above-mentioned dependency.
|
|
"""
|
|
|
|
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
|
|
|
# Mark the whole module as flaky
|
|
pytestmark = pytest.mark.flaky
|
|
|
|
|
|
class TestLoadInterface(unittest.TestCase):
|
|
def test_audio_to_audio(self):
|
|
model_type = "audio-to-audio"
|
|
interface = gr.Interface.load(
|
|
name="speechbrain/mtl-mimic-voicebank",
|
|
src="models",
|
|
alias=model_type,
|
|
)
|
|
self.assertEqual(interface.__name__, model_type)
|
|
self.assertIsInstance(interface.input_components[0], gr.components.Audio)
|
|
self.assertIsInstance(interface.output_components[0], gr.components.Audio)
|
|
|
|
def test_question_answering(self):
|
|
model_type = "image-classification"
|
|
interface = gr.Blocks.load(
|
|
name="lysandre/tiny-vit-random", src="models", alias=model_type
|
|
)
|
|
self.assertEqual(interface.__name__, model_type)
|
|
self.assertIsInstance(interface.input_components[0], gr.components.Image)
|
|
self.assertIsInstance(interface.output_components[0], gr.components.Label)
|
|
|
|
def test_text_generation(self):
|
|
model_type = "text_generation"
|
|
interface = gr.Interface.load("models/gpt2", alias=model_type)
|
|
self.assertEqual(interface.__name__, model_type)
|
|
self.assertIsInstance(interface.input_components[0], gr.components.Textbox)
|
|
self.assertIsInstance(interface.output_components[0], gr.components.Textbox)
|
|
|
|
def test_summarization(self):
|
|
model_type = "summarization"
|
|
interface = gr.Interface.load(
|
|
"models/facebook/bart-large-cnn", api_key=None, alias=model_type
|
|
)
|
|
self.assertEqual(interface.__name__, model_type)
|
|
self.assertIsInstance(interface.input_components[0], gr.components.Textbox)
|
|
self.assertIsInstance(interface.output_components[0], gr.components.Textbox)
|
|
|
|
def test_translation(self):
|
|
model_type = "translation"
|
|
interface = gr.Interface.load(
|
|
"models/facebook/bart-large-cnn", api_key=None, alias=model_type
|
|
)
|
|
self.assertEqual(interface.__name__, model_type)
|
|
self.assertIsInstance(interface.input_components[0], gr.components.Textbox)
|
|
self.assertIsInstance(interface.output_components[0], gr.components.Textbox)
|
|
|
|
def test_text2text_generation(self):
|
|
model_type = "text2text-generation"
|
|
interface = gr.Interface.load(
|
|
"models/sshleifer/tiny-mbart", api_key=None, alias=model_type
|
|
)
|
|
self.assertEqual(interface.__name__, model_type)
|
|
self.assertIsInstance(interface.input_components[0], gr.components.Textbox)
|
|
self.assertIsInstance(interface.output_components[0], gr.components.Textbox)
|
|
|
|
def test_text_classification(self):
|
|
model_type = "text-classification"
|
|
interface = gr.Interface.load(
|
|
"models/distilbert-base-uncased-finetuned-sst-2-english",
|
|
api_key=None,
|
|
alias=model_type,
|
|
)
|
|
self.assertEqual(interface.__name__, model_type)
|
|
self.assertIsInstance(interface.input_components[0], gr.components.Textbox)
|
|
self.assertIsInstance(interface.output_components[0], gr.components.Label)
|
|
|
|
def test_fill_mask(self):
|
|
model_type = "fill-mask"
|
|
interface = gr.Interface.load(
|
|
"models/bert-base-uncased", api_key=None, alias=model_type
|
|
)
|
|
self.assertEqual(interface.__name__, model_type)
|
|
self.assertIsInstance(interface.input_components[0], gr.components.Textbox)
|
|
self.assertIsInstance(interface.output_components[0], gr.components.Label)
|
|
|
|
def test_zero_shot_classification(self):
|
|
model_type = "zero-shot-classification"
|
|
interface = gr.Interface.load(
|
|
"models/facebook/bart-large-mnli", api_key=None, alias=model_type
|
|
)
|
|
self.assertEqual(interface.__name__, model_type)
|
|
self.assertIsInstance(interface.input_components[0], gr.components.Textbox)
|
|
self.assertIsInstance(interface.input_components[1], gr.components.Textbox)
|
|
self.assertIsInstance(interface.input_components[2], gr.components.Checkbox)
|
|
self.assertIsInstance(interface.output_components[0], gr.components.Label)
|
|
|
|
def test_automatic_speech_recognition(self):
|
|
model_type = "automatic-speech-recognition"
|
|
interface = gr.Interface.load(
|
|
"models/facebook/wav2vec2-base-960h", api_key=None, alias=model_type
|
|
)
|
|
self.assertEqual(interface.__name__, model_type)
|
|
self.assertIsInstance(interface.input_components[0], gr.components.Audio)
|
|
self.assertIsInstance(interface.output_components[0], gr.components.Textbox)
|
|
|
|
def test_image_classification(self):
|
|
model_type = "image-classification"
|
|
interface = gr.Interface.load(
|
|
"models/google/vit-base-patch16-224", api_key=None, alias=model_type
|
|
)
|
|
self.assertEqual(interface.__name__, model_type)
|
|
self.assertIsInstance(interface.input_components[0], gr.components.Image)
|
|
self.assertIsInstance(interface.output_components[0], gr.components.Label)
|
|
|
|
def test_feature_extraction(self):
|
|
model_type = "feature-extraction"
|
|
interface = gr.Interface.load(
|
|
"models/sentence-transformers/distilbert-base-nli-mean-tokens",
|
|
api_key=None,
|
|
alias=model_type,
|
|
)
|
|
self.assertEqual(interface.__name__, model_type)
|
|
self.assertIsInstance(interface.input_components[0], gr.components.Textbox)
|
|
self.assertIsInstance(interface.output_components[0], gr.components.Dataframe)
|
|
|
|
def test_sentence_similarity(self):
|
|
model_type = "text-to-speech"
|
|
interface = gr.Interface.load(
|
|
"models/julien-c/ljspeech_tts_train_tacotron2_raw_phn_tacotron_g2p_en_no_space_train",
|
|
api_key=None,
|
|
alias=model_type,
|
|
)
|
|
self.assertEqual(interface.__name__, model_type)
|
|
self.assertIsInstance(interface.input_components[0], gr.components.Textbox)
|
|
self.assertIsInstance(interface.output_components[0], gr.components.Audio)
|
|
|
|
def test_text_to_speech(self):
|
|
model_type = "text-to-speech"
|
|
interface = gr.Interface.load(
|
|
"models/julien-c/ljspeech_tts_train_tacotron2_raw_phn_tacotron_g2p_en_no_space_train",
|
|
api_key=None,
|
|
alias=model_type,
|
|
)
|
|
self.assertEqual(interface.__name__, model_type)
|
|
self.assertIsInstance(interface.input_components[0], gr.components.Textbox)
|
|
self.assertIsInstance(interface.output_components[0], gr.components.Audio)
|
|
|
|
def test_text_to_image(self):
|
|
model_type = "text-to-image"
|
|
interface = gr.Interface.load(
|
|
"models/osanseviero/BigGAN-deep-128", api_key=None, alias=model_type
|
|
)
|
|
self.assertEqual(interface.__name__, model_type)
|
|
self.assertIsInstance(interface.input_components[0], gr.components.Textbox)
|
|
self.assertIsInstance(interface.output_components[0], gr.components.Image)
|
|
|
|
def test_english_to_spanish(self):
|
|
interface = gr.Interface.load("spaces/abidlabs/english_to_spanish")
|
|
self.assertIsInstance(interface.input_components[0], gr.components.Textbox)
|
|
self.assertIsInstance(interface.output_components[0], gr.components.Textbox)
|
|
|
|
def test_sentiment_model(self):
|
|
io = gr.Interface.load("models/distilbert-base-uncased-finetuned-sst-2-english")
|
|
try:
|
|
output = io("I am happy, I love you")
|
|
assert json.load(open(output))["label"] == "POSITIVE"
|
|
except TooManyRequestsError:
|
|
pass
|
|
|
|
def test_image_classification_model(self):
|
|
io = gr.Blocks.load(name="models/google/vit-base-patch16-224")
|
|
try:
|
|
output = io("gradio/test_data/lion.jpg")
|
|
assert json.load(open(output))["label"] == "lion"
|
|
except TooManyRequestsError:
|
|
pass
|
|
|
|
def test_translation_model(self):
|
|
io = gr.Blocks.load(name="models/t5-base")
|
|
try:
|
|
output = io("My name is Sarah and I live in London")
|
|
self.assertEqual(output, "Mein Name ist Sarah und ich lebe in London")
|
|
except TooManyRequestsError:
|
|
pass
|
|
|
|
def test_numerical_to_label_space(self):
|
|
io = gr.Interface.load("spaces/abidlabs/titanic-survival")
|
|
try:
|
|
output = io("male", 77, 10)
|
|
assert json.load(open(output))["label"] == "Perishes"
|
|
except TooManyRequestsError:
|
|
pass
|
|
|
|
def test_speech_recognition_model(self):
|
|
io = gr.Interface.load("models/facebook/wav2vec2-base-960h")
|
|
try:
|
|
output = io("gradio/test_data/test_audio.wav")
|
|
self.assertIsNotNone(output)
|
|
except TooManyRequestsError:
|
|
pass
|
|
|
|
def test_text_to_image_model(self):
|
|
io = gr.Interface.load("models/osanseviero/BigGAN-deep-128")
|
|
try:
|
|
filename = io("chest")
|
|
self.assertTrue(filename.endswith(".jpg") or filename.endswith(".jpeg"))
|
|
except TooManyRequestsError:
|
|
pass
|
|
|
|
|
|
class TestLoadFromPipeline(unittest.TestCase):
|
|
def test_text_to_text_model_from_pipeline(self):
|
|
pipe = transformers.pipeline(model="sshleifer/bart-tiny-random")
|
|
output = pipe("My name is Sylvain and I work at Hugging Face in Brooklyn")
|
|
self.assertIsNotNone(output)
|
|
|
|
|
|
def test_interface_load_cache_examples(tmp_path):
|
|
test_file_dir = pathlib.Path(pathlib.Path(__file__).parent, "test_files")
|
|
with patch("gradio.examples.CACHED_FOLDER", tmp_path):
|
|
gr.Interface.load(
|
|
name="models/google/vit-base-patch16-224",
|
|
examples=[pathlib.Path(test_file_dir, "cheetah1.jpg")],
|
|
cache_examples=True,
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|