diff --git a/gradio/external_utils.py b/gradio/external_utils.py index 8b69721aa9..bab3ea8de3 100644 --- a/gradio/external_utils.py +++ b/gradio/external_utils.py @@ -120,7 +120,12 @@ def zero_shot_classification_inner(input: str, labels: str, multi_label: bool): def sentence_similarity_wrapper(client: InferenceClient): def sentence_similarity_inner(input: str, sentences: str): - return client.sentence_similarity(input, sentences.split("\n")) + # Avoid unnecessary work if 'sentences' is empty: + if not sentences: + return client.sentence_similarity(input, []) + # Using splitlines is usually faster than split("\n"), and avoids trailing empty strings for trailing newlines + sentence_list = sentences.splitlines() + return client.sentence_similarity(input, sentence_list) return sentence_similarity_inner