Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,134 +1,55 @@
|
|
| 1 |
-
import
|
| 2 |
-
import os
|
| 3 |
-
import json
|
| 4 |
-
import requests
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
MODELS = [
|
| 13 |
'gpt-4o',
|
| 14 |
'gpt-4o-mini',
|
| 15 |
-
'gpt-4-turbo',
|
| 16 |
'gpt-4',
|
|
|
|
| 17 |
'gpt-3.5-turbo',
|
| 18 |
]
|
| 19 |
|
| 20 |
-
def predict(model_name, inputs, top_p, temperature, openai_api_key, chat_counter, chatbot=[], history=[]): #repetition_penalty, top_k
|
| 21 |
-
|
| 22 |
-
payload = {
|
| 23 |
-
"model": "gpt-3.5-turbo",
|
| 24 |
-
"messages": [{"role": "user", "content": f"{inputs}"}],
|
| 25 |
-
"temperature" : 1.0,
|
| 26 |
-
"top_p":1.0,
|
| 27 |
-
"n" : 1,
|
| 28 |
-
"stream": True,
|
| 29 |
-
"presence_penalty":0,
|
| 30 |
-
"frequency_penalty":0,
|
| 31 |
-
}
|
| 32 |
-
|
| 33 |
-
headers = {
|
| 34 |
-
"Content-Type": "application/json",
|
| 35 |
-
"Authorization": f"Bearer {openai_api_key}"
|
| 36 |
-
}
|
| 37 |
-
|
| 38 |
-
print(f"chat_counter - {chat_counter}")
|
| 39 |
-
if chat_counter != 0 :
|
| 40 |
-
messages=[]
|
| 41 |
-
for data in chatbot:
|
| 42 |
-
temp1 = {}
|
| 43 |
-
temp1["role"] = "user"
|
| 44 |
-
temp1["content"] = data[0]
|
| 45 |
-
temp2 = {}
|
| 46 |
-
temp2["role"] = "assistant"
|
| 47 |
-
temp2["content"] = data[1]
|
| 48 |
-
messages.append(temp1)
|
| 49 |
-
messages.append(temp2)
|
| 50 |
-
temp3 = {}
|
| 51 |
-
temp3["role"] = "user"
|
| 52 |
-
temp3["content"] = inputs
|
| 53 |
-
messages.append(temp3)
|
| 54 |
-
#messages
|
| 55 |
-
payload = {
|
| 56 |
-
"model": model_name,
|
| 57 |
-
"messages": messages, #[{"role": "user", "content": f"{inputs}"}],
|
| 58 |
-
"temperature" : temperature, #1.0,
|
| 59 |
-
"top_p": top_p, #1.0,
|
| 60 |
-
"n" : 1,
|
| 61 |
-
"stream": True,
|
| 62 |
-
"presence_penalty":0,
|
| 63 |
-
"frequency_penalty":0,
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
chat_counter+=1
|
| 67 |
-
|
| 68 |
-
history.append(inputs)
|
| 69 |
-
print(f"payload is - {payload}")
|
| 70 |
-
# make a POST request to the API endpoint using the requests.post method, passing in stream=True
|
| 71 |
-
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
| 72 |
-
#response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
| 73 |
-
token_counter = 0
|
| 74 |
-
partial_words = ""
|
| 75 |
-
|
| 76 |
-
counter=0
|
| 77 |
-
for chunk in response.iter_lines():
|
| 78 |
-
#Skipping first chunk
|
| 79 |
-
if counter == 0:
|
| 80 |
-
counter+=1
|
| 81 |
-
continue
|
| 82 |
-
#counter+=1
|
| 83 |
-
# check whether each line is non-empty
|
| 84 |
-
if chunk.decode() :
|
| 85 |
-
chunk = chunk.decode()
|
| 86 |
-
# decode each line as response data is in bytes
|
| 87 |
-
if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
|
| 88 |
-
#if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:
|
| 89 |
-
# break
|
| 90 |
-
partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"]
|
| 91 |
-
if token_counter == 0:
|
| 92 |
-
history.append(" " + partial_words)
|
| 93 |
-
else:
|
| 94 |
-
history[-1] = partial_words
|
| 95 |
-
chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
|
| 96 |
-
token_counter+=1
|
| 97 |
-
yield chat, history, chat_counter # resembles {chatbot: chat, state: history}
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
def reset_textbox():
|
| 101 |
-
return gr.update(value='')
|
| 102 |
-
|
| 103 |
-
title = """<h1 align="center">Private ChatGPT</h1>"""
|
| 104 |
-
description = """Chat with OpenAI models using their official API. OpenAI <a href="https://platform.openai.com/docs/concepts">promises</a> not to train on input or output of API calls.
|
| 105 |
-
"""
|
| 106 |
-
|
| 107 |
-
with gr.Blocks(css = """#col_container {width: 1000px; margin-left: auto; margin-right: auto;}
|
| 108 |
-
#chatbot {height: 520px; overflow: auto;}""") as demo:
|
| 109 |
-
gr.HTML(title)
|
| 110 |
-
gr.HTML(description)
|
| 111 |
-
with gr.Column(elem_id = "col_container"):
|
| 112 |
-
openai_api_key = gr.Textbox(type='password', label="OpenAI API key (this space does not store it)", value=OPENAI_API_KEY)
|
| 113 |
-
model_name = gr.Dropdown(label='model', choices=MODELS, value=MODELS[0], allow_custom_value=True)
|
| 114 |
-
chatbot = gr.Chatbot(elem_id='chatbot') #c
|
| 115 |
-
inputs = gr.Textbox(placeholder= "Type here!", label= "Type an input and press Enter") #t
|
| 116 |
-
state = gr.State([]) #s
|
| 117 |
-
b1 = gr.Button()
|
| 118 |
-
|
| 119 |
-
#inputs, top_p, temperature, top_k, repetition_penalty
|
| 120 |
-
with gr.Accordion("Parameters", open=False):
|
| 121 |
-
top_p = gr.Slider( minimum=-0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p (nucleus sampling)",)
|
| 122 |
-
temperature = gr.Slider( minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature",)
|
| 123 |
-
#top_k = gr.Slider( minimum=1, maximum=50, value=4, step=1, interactive=True, label="Top-k",)
|
| 124 |
-
#repetition_penalty = gr.Slider( minimum=0.1, maximum=3.0, value=1.03, step=0.01, interactive=True, label="Repetition Penalty", )
|
| 125 |
-
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
| 126 |
-
|
| 127 |
-
inputs.submit( predict, [model_name, inputs, top_p, temperature, openai_api_key, chat_counter, chatbot, state], [chatbot, state, chat_counter],)
|
| 128 |
-
b1.click( predict, [model_name, inputs, top_p, temperature, openai_api_key, chat_counter, chatbot, state], [chatbot, state, chat_counter],)
|
| 129 |
-
b1.click(reset_textbox, [], [inputs])
|
| 130 |
-
inputs.submit(reset_textbox, [], [inputs])
|
| 131 |
-
|
| 132 |
-
#gr.Markdown(description)
|
| 133 |
-
demo.queue().launch(debug=True)
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
import gradio as gr
|
| 5 |
|
| 6 |
+
api_key = os.environ.get('OPENAI_API_KEY')
|
| 7 |
+
client = OpenAI(api_key=api_key)
|
| 8 |
|
| 9 |
MODELS = [
|
| 10 |
'gpt-4o',
|
| 11 |
'gpt-4o-mini',
|
|
|
|
| 12 |
'gpt-4',
|
| 13 |
+
'gpt-4-turbo',
|
| 14 |
'gpt-3.5-turbo',
|
| 15 |
]
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
def generate(message, history, model, temperature=1.0):
|
| 19 |
+
history_openai_format = []
|
| 20 |
+
for human, assistant in history:
|
| 21 |
+
history_openai_format.append({"role": "user", "content": human})
|
| 22 |
+
history_openai_format.append({"role": "assistant", "content": assistant})
|
| 23 |
+
history_openai_format.append({"role": "user", "content": message})
|
| 24 |
+
|
| 25 |
+
response = client.chat.completions.create(model=model,
|
| 26 |
+
messages=history_openai_format,
|
| 27 |
+
temperature=temperature,
|
| 28 |
+
stream=True)
|
| 29 |
+
|
| 30 |
+
partial_message = ""
|
| 31 |
+
for chunk in response:
|
| 32 |
+
if chunk.choices[0].delta.content is not None:
|
| 33 |
+
partial_message = partial_message + chunk.choices[0].delta.content
|
| 34 |
+
yield partial_message
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
chat_interface = gr.ChatInterface(
|
| 38 |
+
title='Private ChatGPT',
|
| 39 |
+
description='Chat with OpenAI models using their official API. OpenAI <a href="https://platform.openai.com/docs/concepts">promises</a> not to train on input or output of API calls.',
|
| 40 |
+
fn=generate,
|
| 41 |
+
additional_inputs=[
|
| 42 |
+
gr.Dropdown(label='model',
|
| 43 |
+
choices=MODELS,
|
| 44 |
+
value=MODELS[0],
|
| 45 |
+
allow_custom_value=True),
|
| 46 |
+
gr.Slider(label="Temperature",
|
| 47 |
+
minimum=0.,
|
| 48 |
+
maximum=1.2,
|
| 49 |
+
step=0.05,
|
| 50 |
+
value=1.0),
|
| 51 |
+
],
|
| 52 |
+
analytics_enabled=False,
|
| 53 |
+
show_progress='full',
|
| 54 |
+
)
|
| 55 |
+
chat_interface.launch()
|