CoolFace
Apppublic

supportntest/ChatGPT4

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
app.py142 linesDownload Raw Back to root
1import gradio as gr2import os 3import json 4import requests5 6#Streaming endpoint 7API_URL = "https://api.openai.com/v1/chat/completions" #os.getenv("API_URL") + "/generate_stream"8 9#Testing with my Open AI Key 10OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") 11 12def predict(inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):  13 14    payload = {15    "model": "gpt-4",16    "messages": [{"role": "user", "content": f"{inputs}"}],17    "temperature" : 1.0,18    "top_p":1.0,19    "n" : 1,20    "stream": True,21    "presence_penalty":0,22    "frequency_penalty":0,23    }24 25    headers = {26    "Content-Type": "application/json",27    "Authorization": f"Bearer {OPENAI_API_KEY}"28    }29 30    print(f"chat_counter - {chat_counter}")31    if chat_counter != 0 :32        messages=[]33        for data in chatbot:34          temp1 = {}35          temp1["role"] = "user" 36          temp1["content"] = data[0] 37          temp2 = {}38          temp2["role"] = "assistant" 39          temp2["content"] = data[1]40          messages.append(temp1)41          messages.append(temp2)42        temp3 = {}43        temp3["role"] = "user" 44        temp3["content"] = inputs45        messages.append(temp3)46        #messages47        payload = {48        "model": "gpt-4",49        "messages": messages, #[{"role": "user", "content": f"{inputs}"}],50        "temperature" : temperature, #1.0,51        "top_p": top_p, #1.0,52        "n" : 1,53        "stream": True,54        "presence_penalty":0,55        "frequency_penalty":0,56        }57 58    chat_counter+=159 60    history.append(inputs)61    print(f"payload is - {payload}")62    # make a POST request to the API endpoint using the requests.post method, passing in stream=True63    response = requests.post(API_URL, headers=headers, json=payload, stream=True)64    print(f"response code - {response}")65    token_counter = 0 66    partial_words = "" 67 68    counter=069    for chunk in response.iter_lines():70        #Skipping first chunk71        if counter == 0:72          counter+=173          continue74        #counter+=175        # check whether each line is non-empty76        if chunk.decode() :77          chunk = chunk.decode()78          # decode each line as response data is in bytes79          if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:80              #if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:81              #  break82              partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"]83              if token_counter == 0:84                history.append(" " + partial_words)85              else:86                history[-1] = partial_words87              chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ]  # convert to tuples of list88              token_counter+=189              yield chat, history, chat_counter, response  # resembles {chatbot: chat, state: history}  90                   91 92def reset_textbox():93    return gr.update(value='')94 95title = """<h1 align="center">🔥GPT4 with ChatCompletions API +🚀Gradio-Streaming</h1>"""96description = """Language models can be conditioned to act like dialogue agents through a conversational prompt that typically takes the form:97```98User: <utterance>99Assistant: <utterance>100User: <utterance>101Assistant: <utterance>102...103```104In this app, you can explore the outputs of a gpt-4 LLM.105"""106 107theme = gr.themes.Default(primary_hue="green")                108 109with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}110                #chatbot {height: 520px; overflow: auto;}""",111              theme=theme) as demo:112    gr.HTML(title)113    gr.HTML("""<h3 align="center">🔥This Huggingface Gradio Demo provides you full access to GPT4 API (4096 token limit). 🎉🥳🎉You don't need any OPENAI API key🙌</h1>""")114    gr.HTML('''<center><a href="https://huggingface.co/spaces/ysharma/ChatGPT4?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate the Space and run securely with your OpenAI API Key</center>''')115    with gr.Column(elem_id = "col_container"):116        #GPT4 API Key is provided by Huggingface 117        #openai_api_key = gr.Textbox(type='password', label="Enter only your GPT4 OpenAI API key here")118        chatbot = gr.Chatbot(elem_id='chatbot') #c119        inputs = gr.Textbox(placeholder= "Hi there!", label= "Type an input and press Enter") #t120        state = gr.State([]) #s121        with gr.Row():122            with gr.Column(scale=7):123                b1 = gr.Button().style(full_width=True)124            with gr.Column(scale=3):125                server_status_code = gr.Textbox(label="Status code from OpenAI server", )126    127        #inputs, top_p, temperature, top_k, repetition_penalty128        with gr.Accordion("Parameters", open=False):129            top_p = gr.Slider( minimum=-0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p (nucleus sampling)",)130            temperature = gr.Slider( minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature",)131            #top_k = gr.Slider( minimum=1, maximum=50, value=4, step=1, interactive=True, label="Top-k",)132            #repetition_penalty = gr.Slider( minimum=0.1, maximum=3.0, value=1.03, step=0.01, interactive=True, label="Repetition Penalty", )133            chat_counter = gr.Number(value=0, visible=False, precision=0)134 135    inputs.submit( predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],)  #openai_api_key136    b1.click( predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],)  #openai_api_key137    b1.click(reset_textbox, [], [inputs])138    inputs.submit(reset_textbox, [], [inputs])139                    140    #gr.Markdown(description)141    demo.queue(max_size=20, concurrency_count=10).launch(debug=True)142