| import gradio as gr | |
| import time | |
| def generate_no_stream(): | |
| text = "Hello, this is an example without using streaming that emulates a model." | |
| time.sleep(1.2) | |
| return text | |
| def generate_streaming(): | |
| text = "Hello, this is an example using streaming that emulates a model ." | |
| results = "" | |
| for word in text.split(): | |
| time.sleep(0.1) | |
| results += word | |
| results += " " | |
| yield results | |
| with gr.Blocks() as demo: | |
| btn = gr.Button("Generate") | |
| out_non_stream = gr.Textbox(label="Non-streaming Generation") | |
| out_stream = gr.Textbox(label="Streaming Generation") | |
| def change_visibility(): | |
| return { | |
| out_non_stream: gr.update(visible=True), | |
| out_stream: gr.update(visible=True), | |
| } | |
| btn.click(fn=change_visibility, outputs=[out_non_stream, out_stream]) | |
| btn.click(fn=generate_streaming, outputs=out_stream) | |
| btn.click(fn=generate_no_stream, outputs=out_non_stream) | |
| demo.queue(2) | |
| demo.launch(debug=True) |