File size: 1,760 Bytes
d2f74fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# encoding = "utf-8"


'''
This is a mediator: a gradio server for OpenAI APIs
'''

import os
import json
import argparse
import gradio as gr
import requests
from openai import OpenAI

def http_bot(messages, argsbox):

    args = eval(argsbox)
    messages = eval(messages)

    print(messages)
    print(argsbox)

    # api_key = args["api_key"]
    # base_url = args["base_url"]
    # model = args["model"]
    # temperature = args["temperature"]
    # max_tokens = args["max_tokens"]
    #
    # headers = {
    #     "Content-Type": "application/json",
    #     "Authorization": f"Bearer {}"  # Users will provide their own OPENAI_API_KEY
    # }


    client = OpenAI(api_key=args["api_key"], base_url = args["base_url"])
    # n = 0
    # while True:
        # try:
    chat_completion = client.chat.completions.create(
        messages=messages,
        model=args["model"], #"gpt-3.5-turbo-16k",  # "gpt-3.5-turbo", # gpt-4-1106-preview
        temperature=float(args["temperature"]),
        max_tokens=int(args["max_tokens"])
    )
        #     break
        # except Exception as e:
        #     continue
    print(chat_completion)
    return chat_completion.choices[0].message.content



with gr.Blocks() as demo:
    gr.Markdown("# vLLM text completion demo\n")
    inputbox = gr.Textbox(label="Input",
                          placeholder="Enter text and press ENTER")
    argsbox = gr.Textbox(label="Args", placeholder="a dict of {api_key, base_url, model, temperature, max_tokens}")
    outputbox = gr.Textbox(label="Output",
                           placeholder="Generated result from the model")
    submit = gr.Button("Submit")

    submit.click(http_bot, [inputbox, argsbox], [outputbox], api_name="submit")

demo.launch(share=True)