diff --git a/demo/diff_texts/run.py b/demo/diff_texts/run.py index bf66e71d83..e6127a50dd 100644 --- a/demo/diff_texts/run.py +++ b/demo/diff_texts/run.py @@ -25,7 +25,7 @@ demo = gr.Interface( value="The fast brown fox jumps over lazy dogs.", ), ], - gr.HighlightedText(label="Diff"), + gr.HighlightedText(label="Diff", combine_adjacent=True), ) if __name__ == "__main__": demo.launch() diff --git a/gradio.egg-info/PKG-INFO b/gradio.egg-info/PKG-INFO index 7b170030b5..34e0036d1d 100644 --- a/gradio.egg-info/PKG-INFO +++ b/gradio.egg-info/PKG-INFO @@ -1,6 +1,6 @@ Metadata-Version: 2.1 Name: gradio -Version: 2.9b25 +Version: 2.9b26 Summary: Python library for easily interacting with trained machine learning models Home-page: https://github.com/gradio-app/gradio-UI Author: Abubakar Abid, Ali Abid, Ali Abdalla, Dawood Khan, Ahsen Khaliq, Pete Allen, Ömer Faruk Özdemir diff --git a/gradio/components.py b/gradio/components.py index 419ed75a2c..d77df8fefb 100644 --- a/gradio/components.py +++ b/gradio/components.py @@ -2726,6 +2726,7 @@ class HighlightedText(Changeable, IOComponent): *, color_map: Dict[str, str] = None, show_legend: bool = False, + combine_adjacent: bool = False, label: Optional[str] = None, show_label: bool = True, css: Optional[Dict] = None, @@ -2745,6 +2746,7 @@ class HighlightedText(Changeable, IOComponent): self.value = value self.color_map = color_map self.show_legend = show_legend + self.combine_adjacent = combine_adjacent IOComponent.__init__( self, label=label, show_label=show_label, css=css, visible=visible, **kwargs ) @@ -2781,12 +2783,28 @@ class HighlightedText(Changeable, IOComponent): def postprocess(self, y): """ Parameters: - y (Union[Dict, List[Tuple[str, Union[str, int, float]]]]): dictionary or tuple list representing key value pairs + y (List[Tuple[str, Union[str, number, None]]]): List of (word, category) tuples Returns: - (List[Tuple[str, Union[str, number]]]): list of key value pairs - + (List[Tuple[str, Union[str, number, None]]]): List of (word, category) tuples """ - return y + if self.combine_adjacent: + output = [] + running_text, running_category = None, None + for text, category in y: + if running_text is None: + running_text = text + running_category = category + elif category == running_category: + running_text += text + else: + output.append((running_text, running_category)) + running_text = text + running_category = category + if running_text is not None: + output.append((running_text, running_category)) + return output + else: + return y def save_flagged(self, dir, label, data, encryption_key): return json.dumps(data) diff --git a/gradio/version.txt b/gradio/version.txt index 9ea98528a7..40a081ac5c 100644 --- a/gradio/version.txt +++ b/gradio/version.txt @@ -1 +1 @@ -2.9b25 \ No newline at end of file +2.9b26 \ No newline at end of file diff --git a/setup.py b/setup.py index 0e03d0f186..c2c89b84eb 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ long_description = (this_directory / "README.md").read_text() setup( name="gradio", - version="2.9b25", + version="2.9b26", include_package_data=True, description="Python library for easily interacting with trained machine learning models", long_description=long_description, diff --git a/ui/packages/highlighted-text/src/HighlightedText.svelte b/ui/packages/highlighted-text/src/HighlightedText.svelte index d729fbc91d..8b2322259d 100644 --- a/ui/packages/highlighted-text/src/HighlightedText.svelte +++ b/ui/packages/highlighted-text/src/HighlightedText.svelte @@ -104,22 +104,23 @@ {/if}