File size: 1,435 Bytes
ff40816
a2c816b
ff40816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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 = json.loads(argsbox)
    messages = json.loads(messages)

    print(messages)
    print(argsbox)

    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)