Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# encoding = "utf-8"
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
'''
|
| 5 |
+
This is a mediator: a gradio server for OpenAI APIs
|
| 6 |
+
'''
|
| 7 |
+
|
| 8 |
+
import os
|
| 9 |
+
import json
|
| 10 |
+
import argparse
|
| 11 |
+
import gradio as gr
|
| 12 |
+
import requests
|
| 13 |
+
from openai import OpenAI
|
| 14 |
+
|
| 15 |
+
def http_bot(messages, argsbox):
|
| 16 |
+
|
| 17 |
+
args = json.loads(argsbox)
|
| 18 |
+
messages = json.loads(messages)
|
| 19 |
+
|
| 20 |
+
print(messages)
|
| 21 |
+
print(argsbox)
|
| 22 |
+
|
| 23 |
+
client = OpenAI(api_key=args["api_key"], base_url = args["base_url"])
|
| 24 |
+
# n = 0
|
| 25 |
+
# while True:
|
| 26 |
+
# try:
|
| 27 |
+
chat_completion = client.chat.completions.create(
|
| 28 |
+
messages=messages,
|
| 29 |
+
model=args["model"], #"gpt-3.5-turbo-16k", # "gpt-3.5-turbo", # gpt-4-1106-preview
|
| 30 |
+
temperature=float(args["temperature"]),
|
| 31 |
+
max_tokens=int(args["max_tokens"])
|
| 32 |
+
)
|
| 33 |
+
# break
|
| 34 |
+
# except Exception as e:
|
| 35 |
+
# continue
|
| 36 |
+
print(chat_completion)
|
| 37 |
+
return chat_completion.choices[0].message.content
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("# vLLM text completion demo\n")
|
| 43 |
+
inputbox = gr.Textbox(label="Input",
|
| 44 |
+
placeholder="Enter text and press ENTER")
|
| 45 |
+
argsbox = gr.Textbox(label="Args", placeholder="a dict of {api_key, base_url, model, temperature, max_tokens}")
|
| 46 |
+
outputbox = gr.Textbox(label="Output",
|
| 47 |
+
placeholder="Generated result from the model")
|
| 48 |
+
submit = gr.Button("Submit")
|
| 49 |
+
|
| 50 |
+
submit.click(http_bot, [inputbox, argsbox], [outputbox], api_name="submit")
|
| 51 |
+
|
| 52 |
+
demo.launch(share=True)
|
| 53 |
+
|