2021-10-17 15:25:04 +08:00
import unittest
2021-10-19 23:59:44 +08:00
import pathlib
2021-10-17 15:25:04 +08:00
import gradio as gr
2021-11-09 22:48:55 +08:00
import os
2021-12-21 06:04:37 +08:00
import transformers
2021-10-17 15:25:04 +08:00
2021-10-19 13:59:28 +08:00
"""
2021-10-22 18:55:42 +08:00
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.
2021-10-19 13:59:28 +08:00
"""
2021-11-13 14:33:59 +08:00
os . environ [ " GRADIO_ANALYTICS_ENABLED " ] = " False "
2021-11-09 22:48:55 +08:00
2021-10-19 23:59:44 +08:00
class TestHuggingFaceModelAPI ( unittest . TestCase ) :
2021-11-03 05:22:52 +08:00
def test_question_answering ( self ) :
model_type = " question-answering "
interface_info = gr . external . get_huggingface_interface (
" deepset/roberta-base-squad2 " , api_key = None , alias = model_type )
self . assertEqual ( interface_info [ " fn " ] . __name__ , model_type )
self . assertIsInstance ( interface_info [ " inputs " ] [ 0 ] , gr . inputs . Textbox )
self . assertIsInstance ( interface_info [ " inputs " ] [ 1 ] , gr . inputs . Textbox )
self . assertIsInstance ( interface_info [ " outputs " ] [ 0 ] , gr . outputs . Textbox )
self . assertIsInstance ( interface_info [ " outputs " ] [ 1 ] , gr . outputs . Label )
2021-10-26 06:27:08 +08:00
def test_text_generation ( self ) :
model_type = " text_generation "
2021-11-03 05:22:52 +08:00
interface_info = gr . external . get_huggingface_interface ( " gpt2 " ,
api_key = None ,
alias = model_type )
self . assertEqual ( interface_info [ " fn " ] . __name__ , model_type )
self . assertIsInstance ( interface_info [ " inputs " ] , gr . inputs . Textbox )
self . assertIsInstance ( interface_info [ " outputs " ] , gr . outputs . Textbox )
def test_summarization ( self ) :
model_type = " summarization "
interface_info = gr . external . get_huggingface_interface (
" facebook/bart-large-cnn " , api_key = None , alias = model_type )
self . assertEqual ( interface_info [ " fn " ] . __name__ , model_type )
self . assertIsInstance ( interface_info [ " inputs " ] , gr . inputs . Textbox )
self . assertIsInstance ( interface_info [ " outputs " ] , gr . outputs . Textbox )
def test_translation ( self ) :
model_type = " translation "
interface_info = gr . external . get_huggingface_interface (
" facebook/bart-large-cnn " , api_key = None , alias = model_type )
self . assertEqual ( interface_info [ " fn " ] . __name__ , model_type )
2021-10-17 15:25:04 +08:00
self . assertIsInstance ( interface_info [ " inputs " ] , gr . inputs . Textbox )
self . assertIsInstance ( interface_info [ " outputs " ] , gr . outputs . Textbox )
2021-10-26 06:27:08 +08:00
2021-11-03 05:22:52 +08:00
def test_text2text_generation ( self ) :
model_type = " text2text-generation "
2021-10-26 06:27:08 +08:00
interface_info = gr . external . get_huggingface_interface (
2021-11-03 05:22:52 +08:00
" sshleifer/tiny-mbart " , api_key = None , alias = model_type )
self . assertEqual ( interface_info [ " fn " ] . __name__ , model_type )
self . assertIsInstance ( interface_info [ " inputs " ] , gr . inputs . Textbox )
self . assertIsInstance ( interface_info [ " outputs " ] , gr . outputs . Textbox )
def test_text_classification ( self ) :
model_type = " text-classification "
interface_info = gr . external . get_huggingface_interface (
" distilbert-base-uncased-finetuned-sst-2-english " ,
api_key = None , alias = model_type )
self . assertEqual ( interface_info [ " fn " ] . __name__ , model_type )
self . assertIsInstance ( interface_info [ " inputs " ] , gr . inputs . Textbox )
self . assertIsInstance ( interface_info [ " outputs " ] , gr . outputs . Label )
def test_fill_mask ( self ) :
model_type = " fill-mask "
interface_info = gr . external . get_huggingface_interface (
" bert-base-uncased " ,
api_key = None , alias = model_type )
2021-10-26 06:27:08 +08:00
self . assertEqual ( interface_info [ " fn " ] . __name__ , model_type )
2021-10-17 15:25:04 +08:00
self . assertIsInstance ( interface_info [ " inputs " ] , gr . inputs . Textbox )
self . assertIsInstance ( interface_info [ " outputs " ] , gr . outputs . Label )
2021-11-03 05:22:52 +08:00
def test_zero_shot_classification ( self ) :
model_type = " zero-shot-classification "
interface_info = gr . external . get_huggingface_interface (
" facebook/bart-large-mnli " ,
api_key = None , alias = model_type )
self . assertEqual ( interface_info [ " fn " ] . __name__ , model_type )
self . assertIsInstance ( interface_info [ " inputs " ] [ 0 ] , gr . inputs . Textbox )
self . assertIsInstance ( interface_info [ " inputs " ] [ 1 ] , gr . inputs . Textbox )
self . assertIsInstance ( interface_info [ " inputs " ] [ 2 ] , gr . inputs . Checkbox )
self . assertIsInstance ( interface_info [ " outputs " ] , gr . outputs . Label )
def test_automatic_speech_recognition ( self ) :
model_type = " automatic-speech-recognition "
interface_info = gr . external . get_huggingface_interface (
" facebook/wav2vec2-base-960h " ,
api_key = None , alias = model_type )
self . assertEqual ( interface_info [ " fn " ] . __name__ , model_type )
self . assertIsInstance ( interface_info [ " inputs " ] , gr . inputs . Audio )
self . assertIsInstance ( interface_info [ " outputs " ] , gr . outputs . Textbox )
def test_image_classification ( self ) :
model_type = " image-classification "
interface_info = gr . external . get_huggingface_interface (
" google/vit-base-patch16-224 " ,
api_key = None , alias = model_type )
self . assertEqual ( interface_info [ " fn " ] . __name__ , model_type )
self . assertIsInstance ( interface_info [ " inputs " ] , gr . inputs . Image )
self . assertIsInstance ( interface_info [ " outputs " ] , gr . outputs . Label )
def test_feature_extraction ( self ) :
model_type = " feature-extraction "
interface_info = gr . external . get_huggingface_interface (
" sentence-transformers/distilbert-base-nli-mean-tokens " ,
api_key = None , alias = model_type )
self . assertEqual ( interface_info [ " fn " ] . __name__ , model_type )
self . assertIsInstance ( interface_info [ " inputs " ] , gr . inputs . Textbox )
self . assertIsInstance ( interface_info [ " outputs " ] , gr . outputs . Dataframe )
2021-10-26 06:27:08 +08:00
def test_sentence_similarity ( self ) :
model_type = " text-to-speech "
interface_info = gr . external . get_huggingface_interface (
" julien-c/ljspeech_tts_train_tacotron2_raw_phn_tacotron_g2p_en_no_space_train " ,
api_key = None , alias = model_type )
self . assertEqual ( interface_info [ " fn " ] . __name__ , model_type )
self . assertIsInstance ( interface_info [ " inputs " ] , gr . inputs . Textbox )
self . assertIsInstance ( interface_info [ " outputs " ] , gr . outputs . Audio )
def test_text_to_speech ( self ) :
model_type = " text-to-speech "
interface_info = gr . external . get_huggingface_interface (
" julien-c/ljspeech_tts_train_tacotron2_raw_phn_tacotron_g2p_en_no_space_train " ,
api_key = None , alias = model_type )
self . assertEqual ( interface_info [ " fn " ] . __name__ , model_type )
self . assertIsInstance ( interface_info [ " inputs " ] , gr . inputs . Textbox )
self . assertIsInstance ( interface_info [ " outputs " ] , gr . outputs . Audio )
def test_text_to_image ( self ) :
model_type = " text-to-image "
interface_info = gr . external . get_huggingface_interface (
" osanseviero/BigGAN-deep-128 " ,
api_key = None , alias = model_type )
self . assertEqual ( interface_info [ " fn " ] . __name__ , model_type )
self . assertIsInstance ( interface_info [ " inputs " ] , gr . inputs . Textbox )
self . assertIsInstance ( interface_info [ " outputs " ] , gr . outputs . Image )
2021-10-17 15:25:04 +08:00
def test_english_to_spanish ( self ) :
interface_info = gr . external . get_spaces_interface ( " abidlabs/english_to_spanish " , api_key = None , alias = None )
self . assertIsInstance ( interface_info [ " inputs " ] [ 0 ] , gr . inputs . Textbox )
self . assertIsInstance ( interface_info [ " outputs " ] [ 0 ] , gr . outputs . Textbox )
class TestLoadInterface ( unittest . TestCase ) :
def test_english_to_spanish ( self ) :
interface_info = gr . external . load_interface ( " spaces/abidlabs/english_to_spanish " )
self . assertIsInstance ( interface_info [ " inputs " ] [ 0 ] , gr . inputs . Textbox )
2021-11-03 05:22:52 +08:00
self . assertIsInstance ( interface_info [ " outputs " ] [ 0 ] , gr . outputs . Textbox )
2021-10-17 15:25:04 +08:00
2021-10-19 23:59:44 +08:00
def test_sentiment_model ( self ) :
interface_info = gr . external . load_interface ( " models/distilbert-base-uncased-finetuned-sst-2-english " , alias = " sentiment_classifier " )
2021-11-10 02:30:59 +08:00
io = gr . Interface ( * * interface_info )
2021-12-16 23:43:31 +08:00
io . api_mode = True
2021-10-19 23:59:44 +08:00
output = io ( " I am happy, I love you. " )
2021-12-21 07:59:07 +08:00
self . assertGreater ( output [ ' POSITIVE ' ] , 0.5 )
2021-10-19 23:59:44 +08:00
def test_image_classification_model ( self ) :
interface_info = gr . external . load_interface ( " models/google/vit-base-patch16-224 " )
2021-11-10 02:30:59 +08:00
io = gr . Interface ( * * interface_info )
2021-12-16 23:43:31 +08:00
io . api_mode = True
2021-11-09 04:37:32 +08:00
output = io ( " test/test_data/lion.jpg " )
2021-10-19 23:59:44 +08:00
self . assertGreater ( output [ ' lion ' ] , 0.5 )
def test_translation_model ( self ) :
interface_info = gr . external . load_interface ( " models/t5-base " )
2021-11-10 02:30:59 +08:00
io = gr . Interface ( * * interface_info )
2021-12-16 23:43:31 +08:00
io . api_mode = True
2021-10-19 23:59:44 +08:00
output = io ( " My name is Sarah and I live in London " )
self . assertEquals ( output , ' Mein Name ist Sarah und ich lebe in London ' )
2021-10-22 19:50:26 +08:00
def test_numerical_to_label_space ( self ) :
interface_info = gr . external . load_interface ( " spaces/abidlabs/titanic-survival " )
2021-11-10 02:30:59 +08:00
io = gr . Interface ( * * interface_info )
2021-12-16 23:43:31 +08:00
io . api_mode = True
2021-10-22 19:50:26 +08:00
output = io ( " male " , 77 , 10 )
self . assertLess ( output [ ' Survives ' ] , 0.5 )
2021-12-21 06:04:37 +08:00
def test_speech_recognition_model ( self ) :
interface_info = gr . external . load_interface ( " models/jonatasgrosman/wav2vec2-large-xlsr-53-english " )
io = gr . Interface ( * * interface_info )
io . api_mode = True
output = io ( " test/test_data/test_audio.wav " )
self . assertIsNotNone ( output )
2021-10-19 23:59:44 +08:00
def test_image_to_image_space ( self ) :
def assertIsFile ( path ) :
if not pathlib . Path ( path ) . resolve ( ) . is_file ( ) :
raise AssertionError ( " File does not exist: %s " % str ( path ) )
2021-10-17 15:25:04 +08:00
2021-10-22 19:50:26 +08:00
interface_info = gr . external . load_interface ( " spaces/abidlabs/image-identity " )
2021-11-10 02:30:59 +08:00
io = gr . Interface ( * * interface_info )
2021-12-16 23:43:31 +08:00
io . api_mode = True
2021-11-09 04:37:32 +08:00
output = io ( " test/test_data/lion.jpg " )
2021-10-19 23:59:44 +08:00
assertIsFile ( output )
2021-10-17 15:25:04 +08:00
2021-12-21 06:04:37 +08:00
class TestLoadFromPipeline ( unittest . TestCase ) :
def test_question_answering ( self ) :
p = transformers . pipeline ( " question-answering " )
io = gr . Interface . from_pipeline ( p )
output = io ( " My name is Sylvain and I work at Hugging Face in Brooklyn " , " Where do I work? " )
self . assertIsNotNone ( output )
2021-10-17 15:25:04 +08:00
if __name__ == ' __main__ ' :
unittest . main ( )