Gradio 的月活用户达到 100 万的历程!
阅读更多Gradio 的月活用户达到 100 万的历程!
阅读更多Gradio 支持传递批量函数的功能。批量函数只是接受输入列表并返回预测列表的函数。
例如,这是一个批量函数,它接受两个输入列表(单词列表和整数列表),并返回修剪后的单词列表作为输出
import time
def trim_words(words, lens):
trimmed_words = []
time.sleep(5)
for w, l in zip(words, lens):
trimmed_words.append(w[:int(l)])
return [trimmed_words]
使用批量函数的优势在于,如果您启用队列,Gradio 服务器可以自动批量处理传入的请求并并行处理它们,从而可能加快您的演示速度。这是 Gradio 代码的样子(注意 batch=True
和 max_batch_size=16
)
使用 gr.Interface
类
demo = gr.Interface(
fn=trim_words,
inputs=["textbox", "number"],
outputs=["output"],
batch=True,
max_batch_size=16
)
demo.launch()
使用 gr.Blocks
类
import gradio as gr
with gr.Blocks() as demo:
with gr.Row():
word = gr.Textbox(label="word")
leng = gr.Number(label="leng")
output = gr.Textbox(label="Output")
with gr.Row():
run = gr.Button()
event = run.click(trim_words, [word, leng], output, batch=True, max_batch_size=16)
demo.launch()
在上面的示例中,可以并行处理 16 个请求(总推理时间为 5 秒),而不是单独处理每个请求(总推理时间为 80 秒)。许多 Hugging Face transformers
和 diffusers
模型与 Gradio 的批量模式非常自然地配合使用:这是一个使用 diffusers 批量生成图像的示例演示