gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
33.27k stars 2.51k forks source link

Gradio 5.0.1 - Streaming Chatbot removes new lines (and maybe more) #9679

Open skye0402 opened 1 week ago

skye0402 commented 1 week ago

Describe the bug

When using Gradio.Chatbot with "messages" I found the streaming has a flaw in it. It removes the "\n" from a stream which makes it formatting the message incorrect. Can you look into it?

You can use your own examples that you published for chatbot_simple and chatbot_streaming: https://www.gradio.app/docs/gradio/chatbot

You just have to add a \n into the sample response text. Simple works, Streaming doesn't. Please check if that is the only escaped character that has issues. I tried \\ and \r which seem to work.

Working: image

Not working: image

Have you searched existing issues? 🔎

Reproduction

import gradio as gr
import random
import time

with gr.Blocks() as demo:
    chatbot = gr.Chatbot(type="messages")
    msg = gr.Textbox()
    clear = gr.Button("Clear")

    def user(user_message, history: list):
        return "", history + [{"role": "user", "content": user_message}]

    def bot(history: list):
        bot_message = random.choice(["How are\n\n you?", "I\n\n love\n\n you", "I'm\n\n very\n\n hungry"])
        history.append({"role": "assistant", "content": ""})
        for character in bot_message:
            history[-1]['content'] += character
            time.sleep(0.05)
            yield history

    msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
        bot, chatbot, chatbot
    )
    clear.click(lambda: None, None, chatbot, queue=False)

if __name__ == "__main__":
    demo.launch()

Screenshot

See message above.

Logs

No errors.

System Info

Gradio Environment Information:
------------------------------
Operating System: Linux
gradio version: 5.0.1
gradio_client version: 1.4.0

------------------------------------------------
gradio dependencies in your environment:

aiofiles: 23.2.1
anyio: 4.4.0
fastapi: 0.112.2
ffmpy: 0.4.0
gradio-client==1.4.0 is not installed.
httpx: 0.27.2
huggingface-hub: 0.25.2
jinja2: 3.1.4
markupsafe: 2.1.5
numpy: 1.26.4
orjson: 3.10.7
packaging: 23.2
pandas: 2.2.2
pillow: 10.4.0
pydantic: 2.8.2
pydub: 0.25.1
python-multipart: 0.0.9
pyyaml: 6.0.2
ruff: 0.6.3
semantic-version: 2.10.0
tomlkit==0.12.0 is not installed.
typer: 0.12.5
typing-extensions: 4.12.2
urllib3: 2.2.2
uvicorn: 0.30.6
authlib; extra == 'oauth' is not installed.
itsdangerous; extra == 'oauth' is not installed.

gradio_client dependencies in your environment:

fsspec: 2024.6.1
httpx: 0.27.2
huggingface-hub: 0.25.2
packaging: 23.2
typing-extensions: 4.12.2
websockets: 12.0

Severity

Blocking usage of gradio

freddyaboulton commented 2 days ago

The issue is the call to inspect.cleandoc in post process. It removes all trailing white space. I think we can remove @dawoodkhan82 ?

dawoodkhan82 commented 1 day ago

@freddyaboulton I think we can remove. I'll check and make sure there's no regressions especially with markdown messages.

freddyaboulton commented 1 day ago

Sweet thanks @dawoodkhan82 !!

dawoodkhan82 commented 20 hours ago

@freddyaboulton Double checked and it would lead to breaking markdown messages. inspect.cleandoc maintains proper indentation, handles line endings and spacing that's necessary for the markdown messages. We can change the functionality to only use inspect.cleandoc when render_markdown is true. thoughts @freddyaboulton?

dawoodkhan82 commented 20 hours ago

Actually the better solution here is to probably implement a custom inspect.cleandoc function that preserves empty new lines, but handles all other cases the same.

freddyaboulton commented 18 hours ago

Yes we should keep the trailing whitelines and keep the rest the same. But jw how does it break the markdown? The frontend expects a specific format?