gradio_webserver.py 2.2 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4
5
6
"""Example for starting a Gradio Webserver
Start vLLM API server:
    python -m vllm.entrypoints.api_server \
        --model meta-llama/Llama-2-7b-chat-hf
7

8
9
10
11
12
13
14
15
16
17
18
19
20
Start Webserver:
    python examples/online_serving/gradio_webserver.py

Note that `pip install --upgrade gradio` is needed to run this example.
More details: https://github.com/gradio-app/gradio

If your antivirus software blocks the download of frpc for gradio,
you can install it manually by following these steps:

1. Download this file: https://cdn-media.huggingface.co/frpc-gradio-0.3/frpc_linux_amd64
2. Rename the downloaded file to: frpc_linux_amd64_v0.3
3. Move the file to this location: /home/user/.cache/huggingface/gradio/frpc
"""
21

22
23
24
25
26
27
28
29
import argparse
import json

import gradio as gr
import requests


def http_bot(prompt):
Woosuk Kwon's avatar
Woosuk Kwon committed
30
    headers = {"User-Agent": "vLLM Client"}
31
32
    pload = {
        "prompt": prompt,
33
        "stream": True,
Woosuk Kwon's avatar
Woosuk Kwon committed
34
        "max_tokens": 128,
35
    }
36
37
38
39
40
    response = requests.post(args.model_url, headers=headers, json=pload, stream=True)

    for chunk in response.iter_lines(
        chunk_size=8192, decode_unicode=False, delimiter=b"\n"
    ):
41
42
43
44
45
46
47
48
        if chunk:
            data = json.loads(chunk.decode("utf-8"))
            output = data["text"][0]
            yield output


def build_demo():
    with gr.Blocks() as demo:
49
        gr.Markdown("# vLLM text completion demo\n")
50
51
52
53
        inputbox = gr.Textbox(label="Input", placeholder="Enter text and press ENTER")
        outputbox = gr.Textbox(
            label="Output", placeholder="Generated result from the model"
        )
54
55
56
57
        inputbox.submit(http_bot, [inputbox], [outputbox])
    return demo


58
def parse_args():
59
    parser = argparse.ArgumentParser()
60
    parser.add_argument("--host", type=str, default=None)
61
    parser.add_argument("--port", type=int, default=8001)
62
63
64
    parser.add_argument(
        "--model-url", type=str, default="http://localhost:8000/generate"
    )
65
    return parser.parse_args()
66

67
68

def main(args):
69
    demo = build_demo()
70
    demo.queue().launch(server_name=args.host, server_port=args.port, share=True)
71
72
73
74
75


if __name__ == "__main__":
    args = parse_args()
    main(args)