Upload 3 files
Browse files- app.py +65 -0
- requirements.txt +3 -0
- test.py +19 -0
    	
        app.py
    ADDED
    
    | @@ -0,0 +1,65 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 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 = eval(argsbox)
         | 
| 18 | 
            +
                messages = eval(messages)
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                print(messages)
         | 
| 21 | 
            +
                print(argsbox)
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                # api_key = args["api_key"]
         | 
| 24 | 
            +
                # base_url = args["base_url"]
         | 
| 25 | 
            +
                # model = args["model"]
         | 
| 26 | 
            +
                # temperature = args["temperature"]
         | 
| 27 | 
            +
                # max_tokens = args["max_tokens"]
         | 
| 28 | 
            +
                #
         | 
| 29 | 
            +
                # headers = {
         | 
| 30 | 
            +
                #     "Content-Type": "application/json",
         | 
| 31 | 
            +
                #     "Authorization": f"Bearer {}"  # Users will provide their own OPENAI_API_KEY
         | 
| 32 | 
            +
                # }
         | 
| 33 | 
            +
             | 
| 34 | 
            +
             | 
| 35 | 
            +
                client = OpenAI(api_key=args["api_key"], base_url = args["base_url"])
         | 
| 36 | 
            +
                # n = 0
         | 
| 37 | 
            +
                # while True:
         | 
| 38 | 
            +
                    # try:
         | 
| 39 | 
            +
                chat_completion = client.chat.completions.create(
         | 
| 40 | 
            +
                    messages=messages,
         | 
| 41 | 
            +
                    model=args["model"], #"gpt-3.5-turbo-16k",  # "gpt-3.5-turbo", # gpt-4-1106-preview
         | 
| 42 | 
            +
                    temperature=float(args["temperature"]),
         | 
| 43 | 
            +
                    max_tokens=int(args["max_tokens"])
         | 
| 44 | 
            +
                )
         | 
| 45 | 
            +
                    #     break
         | 
| 46 | 
            +
                    # except Exception as e:
         | 
| 47 | 
            +
                    #     continue
         | 
| 48 | 
            +
                print(chat_completion)
         | 
| 49 | 
            +
                return chat_completion.choices[0].message.content
         | 
| 50 | 
            +
             | 
| 51 | 
            +
             | 
| 52 | 
            +
             | 
| 53 | 
            +
            with gr.Blocks() as demo:
         | 
| 54 | 
            +
                gr.Markdown("# vLLM text completion demo\n")
         | 
| 55 | 
            +
                inputbox = gr.Textbox(label="Input",
         | 
| 56 | 
            +
                                      placeholder="Enter text and press ENTER")
         | 
| 57 | 
            +
                argsbox = gr.Textbox(label="Args", placeholder="a dict of {api_key, base_url, model, temperature, max_tokens}")
         | 
| 58 | 
            +
                outputbox = gr.Textbox(label="Output",
         | 
| 59 | 
            +
                                       placeholder="Generated result from the model")
         | 
| 60 | 
            +
                submit = gr.Button("Submit")
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                submit.click(http_bot, [inputbox, argsbox], [outputbox], api_name="submit")
         | 
| 63 | 
            +
             | 
| 64 | 
            +
            demo.launch(share=True)
         | 
| 65 | 
            +
             | 
    	
        requirements.txt
    ADDED
    
    | @@ -0,0 +1,3 @@ | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            gradio==4.11.0
         | 
| 2 | 
            +
            openai==1.6.0
         | 
| 3 | 
            +
            Requests==2.31.0
         | 
    	
        test.py
    ADDED
    
    | @@ -0,0 +1,19 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            from gradio_client import Client
         | 
| 2 | 
            +
            import json
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            messages = json.dumps([{"role":"user","content":"hi(from macbook)"}])
         | 
| 5 | 
            +
            #
         | 
| 6 | 
            +
            args = json.dumps({"api_key":"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",
         | 
| 7 | 
            +
                               "base_url":"https://api.openai.com/v1",
         | 
| 8 | 
            +
                               "model":"gpt-3.5-turbo",
         | 
| 9 | 
            +
                               "temperature":"1.0",
         | 
| 10 | 
            +
                               "max_tokens":"1024" })
         | 
| 11 | 
            +
             | 
| 12 | 
            +
             | 
| 13 | 
            +
            client = Client("https://f2569cc8b84a4386fa.gradio.live/")
         | 
| 14 | 
            +
            result = client.predict(
         | 
| 15 | 
            +
            		messages,	# str  in 'Input' Textbox component
         | 
| 16 | 
            +
            		args,	# str  in 'Args' Textbox component
         | 
| 17 | 
            +
            		api_name="/submit"
         | 
| 18 | 
            +
            )
         | 
| 19 | 
            +
            print(result)
         |