gradio icon indicating copy to clipboard operation
gradio copied to clipboard

Honour line breaks in HighlightedText

Open versae opened this issue 2 years ago • 7 comments

  • [x] I have searched to see if a similar issue already exists.

Is your feature request related to a problem? Please describe.
When using HighlightedText, line breaks are usually ignored. This is particularly annoying for showing GPT generated text, and even more so for poetry.

Describe the solution you'd like
An option to replace \n with <br> so the resulting HTML is rendered correctly. If I try to write the replace myself it gets escaped and it never renders the actual line break but the characters in the string <br>.

Additional context
Try with the next text

Caminante, son tus huellas
el camino y nada más;
Caminante, no hay camino,
se hace camino al andar.

In the demo here: https://huggingface.co/spaces/bertin-project/bertin-gpt-j-6B (press the Generar button)

versae avatar Jul 01 '22 12:07 versae

Thanks for raising the issue @versae! @aliabid94 is there a workaround for this already or do we need to add support for new lines specifically?

abidlabs avatar Jul 02 '22 03:07 abidlabs

Hi @versae, you can do this by passing in a little bit of css to your Interface or Blocks, like this:

import gradio as gr

def test(text1):
    return [(text1, "good")]

demo = gr.Interface(
    test,
    "text",
    gr.HighlightedText(elem_id="htext"),
    css="#htext span {white-space: pre}",
)
demo.launch()

See #1951 and #1839 for more info.

abidlabs avatar Aug 05 '22 01:08 abidlabs

Interesting. Does this work with the Blocks API as well?

versae avatar Aug 05 '22 13:08 versae

Yup, you can pass css into Blocks as well.

abidlabs avatar Aug 05 '22 16:08 abidlabs

Awesome!

versae avatar Aug 08 '22 09:08 versae

OK, I finally had the chance to try this but now the lines are not breaking at the length of their container. Any idea why? image

versae avatar Sep 14 '22 10:09 versae

hmm will take a look

aliabid94 avatar Sep 14 '22 20:09 aliabid94