Spaces:
Runtime error
Runtime error
update streaming function
Browse filesremoved other chatbot components
app.py
CHANGED
|
@@ -15,84 +15,6 @@ from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
|
|
| 15 |
model_chtoen = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M")
|
| 16 |
tokenizer_chtoen = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M")
|
| 17 |
|
| 18 |
-
#Predict function for CHATGPT
|
| 19 |
-
def predict_chatgpt(inputs, top_p_chatgpt, temperature_chatgpt, openai_api_key, chat_counter_chatgpt, chatbot_chatgpt=[], history=[]):
|
| 20 |
-
#Define payload and header for chatgpt API
|
| 21 |
-
payload = {
|
| 22 |
-
"model": "gpt-3.5-turbo",
|
| 23 |
-
"messages": [{"role": "user", "content": f"{inputs}"}],
|
| 24 |
-
"temperature" : 1.0,
|
| 25 |
-
"top_p":1.0,
|
| 26 |
-
"n" : 1,
|
| 27 |
-
"stream": True,
|
| 28 |
-
"presence_penalty":0,
|
| 29 |
-
"frequency_penalty":0,
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
headers = {
|
| 33 |
-
"Content-Type": "application/json",
|
| 34 |
-
"Authorization": f"Bearer {openai_api_key}"
|
| 35 |
-
}
|
| 36 |
-
#debug
|
| 37 |
-
#print(f"chat_counter_chatgpt - {chat_counter_chatgpt}")
|
| 38 |
-
|
| 39 |
-
#Handling the different roles for ChatGPT
|
| 40 |
-
if chat_counter_chatgpt != 0 :
|
| 41 |
-
messages=[]
|
| 42 |
-
for data in chatbot_chatgpt:
|
| 43 |
-
temp1 = {}
|
| 44 |
-
temp1["role"] = "user"
|
| 45 |
-
temp1["content"] = data[0]
|
| 46 |
-
temp2 = {}
|
| 47 |
-
temp2["role"] = "assistant"
|
| 48 |
-
temp2["content"] = data[1]
|
| 49 |
-
messages.append(temp1)
|
| 50 |
-
messages.append(temp2)
|
| 51 |
-
temp3 = {}
|
| 52 |
-
temp3["role"] = "user"
|
| 53 |
-
temp3["content"] = inputs
|
| 54 |
-
messages.append(temp3)
|
| 55 |
-
payload = {
|
| 56 |
-
"model": "gpt-3.5-turbo",
|
| 57 |
-
"messages": messages, #[{"role": "user", "content": f"{inputs}"}],
|
| 58 |
-
"temperature" : temperature_chatgpt, #1.0,
|
| 59 |
-
"top_p": top_p_chatgpt, #1.0,
|
| 60 |
-
"n" : 1,
|
| 61 |
-
"stream": True,
|
| 62 |
-
"presence_penalty":0,
|
| 63 |
-
"frequency_penalty":0,
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
chat_counter_chatgpt+=1
|
| 67 |
-
|
| 68 |
-
history.append(inputs)
|
| 69 |
-
|
| 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 |
-
token_counter = 0
|
| 73 |
-
partial_words = ""
|
| 74 |
-
|
| 75 |
-
counter=0
|
| 76 |
-
for chunk in response.iter_lines():
|
| 77 |
-
#Skipping the first chunk
|
| 78 |
-
if counter == 0:
|
| 79 |
-
counter+=1
|
| 80 |
-
continue
|
| 81 |
-
# check whether each line is non-empty
|
| 82 |
-
if chunk.decode() :
|
| 83 |
-
chunk = chunk.decode()
|
| 84 |
-
# decode each line as response data is in bytes
|
| 85 |
-
if len(chunk) > 13 and "content" in json.loads(chunk[6:])['choices'][0]["delta"]:
|
| 86 |
-
partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"]
|
| 87 |
-
if token_counter == 0:
|
| 88 |
-
history.append(" " + partial_words)
|
| 89 |
-
else:
|
| 90 |
-
history[-1] = partial_words
|
| 91 |
-
chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
|
| 92 |
-
token_counter+=1
|
| 93 |
-
yield chat, history, chat_counter_chatgpt # this resembles {chatbot: chat, state: history}
|
| 94 |
-
|
| 95 |
-
|
| 96 |
# Define function to generate model predictions and update the history
|
| 97 |
def predict_glm(input, history=[]):
|
| 98 |
response, history = model_glm.chat(tokenizer_glm, input, history)
|
|
@@ -108,21 +30,18 @@ def translate_Chinese_English(chinese_text):
|
|
| 108 |
trans_eng_text = tokenizer_chtoen.batch_decode(generated_tokens, skip_special_tokens=True)
|
| 109 |
return trans_eng_text[0]
|
| 110 |
|
| 111 |
-
# Define
|
| 112 |
def predict_glm_stream(input, history=[]): #, top_p, temperature):
|
| 113 |
-
response, history = model_glm.chat(tokenizer_glm, input, history)
|
| 114 |
-
print(f"outside for loop resonse is ^^- {response}")
|
| 115 |
-
print(f"outside for loop history is ^^- {history}")
|
| 116 |
top_p = 1.0
|
| 117 |
temperature = 1.0
|
| 118 |
-
for response, history in
|
| 119 |
print(f"In for loop resonse is ^^- {response}")
|
| 120 |
print(f"In for loop history is ^^- {history}")
|
| 121 |
# translate Chinese to English
|
| 122 |
history = [(query, translate_Chinese_English(response)) for query, response in history]
|
| 123 |
print(f"In for loop translated history is ^^- {history}")
|
| 124 |
yield history, history #[history] + updates
|
| 125 |
-
|
| 126 |
|
| 127 |
"""
|
| 128 |
def predict(input, max_length, top_p, temperature, history=None):
|
|
@@ -162,76 +81,45 @@ Assistant: <utterance>
|
|
| 162 |
In this app, you can explore the outputs of multiple LLMs when prompted in similar ways.
|
| 163 |
"""
|
| 164 |
|
| 165 |
-
with gr.Blocks(css="""#col_container {
|
| 166 |
-
#chatgpt {height: 520px; overflow: auto;}
|
| 167 |
#chatglm {height: 520px; overflow: auto;} """ ) as demo:
|
| 168 |
-
#chattogether {height: 520px; overflow: auto;} """ ) as demo:
|
| 169 |
-
#clear {width: 100px; height:50px; font-size:12px}""") as demo:
|
| 170 |
gr.HTML(title)
|
| 171 |
-
with gr.Row():
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
openai_api_key = gr.Textbox(type='password', label="Enter your OpenAI API key here for ChatGPT")
|
| 177 |
inputs = gr.Textbox(placeholder="Hi there!", label="Type an input and press Enter ⤵️ " )
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
with gr.Row():
|
| 187 |
-
chatbot_chatgpt = gr.Chatbot(elem_id="chatgpt", label='ChatGPT API - OPENAI')
|
| 188 |
-
#chatbot_together = gr.Chatbot(elem_id="chattogether", label='OpenChatKit - Text Generation')
|
| 189 |
-
chatbot_glm = gr.Chatbot(elem_id="chatglm", label='THUDM-ChatGLM6B')
|
| 190 |
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
watermark = gr.Checkbox(value=True, label="Text watermarking", visible=False)
|
| 199 |
-
model = gr.CheckboxGroup(value="Rallio67/joi2_20B_instruct_alpha",
|
| 200 |
-
choices=["togethercomputer/GPT-NeoXT-Chat-Base-20B", "Rallio67/joi2_20B_instruct_alpha", "google/flan-t5-xxl", "google/flan-ul2", "bigscience/bloomz", "EleutherAI/gpt-neox-20b",],
|
| 201 |
-
label="Model",visible=False,)
|
| 202 |
-
temp_textbox_together = gr.Textbox(value=model.choices[0], visible=False)
|
| 203 |
-
|
| 204 |
-
with gr.Box():
|
| 205 |
-
gr.HTML("Parameters for OpenAI's ChatGPT")
|
| 206 |
-
top_p_chatgpt = gr.Slider( minimum=-0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p",)
|
| 207 |
-
temperature_chatgpt = gr.Slider( minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature",)
|
| 208 |
-
chat_counter_chatgpt = gr.Number(value=0, visible=False, precision=0)
|
| 209 |
|
| 210 |
inputs.submit(reset_textbox, [], [inputs])
|
| 211 |
|
| 212 |
-
|
| 213 |
-
[inputs, top_p_chatgpt, temperature_chatgpt, openai_api_key, chat_counter_chatgpt, chatbot_chatgpt, state_chatgpt],
|
| 214 |
-
[chatbot_chatgpt, state_chatgpt, chat_counter_chatgpt],)
|
| 215 |
-
#inputs.submit( predict_glm,
|
| 216 |
-
# [inputs, state_glm, ],
|
| 217 |
-
# [chatbot_glm, state_glm],)
|
| 218 |
-
#b1.click( predict_glm,
|
| 219 |
-
# [inputs, state_glm, ],
|
| 220 |
-
# [chatbot_glm, state_glm],)
|
| 221 |
inputs.submit( predict_glm_stream,
|
| 222 |
[inputs, state_glm, ],
|
| 223 |
[chatbot_glm, state_glm],)
|
| 224 |
b1.click( predict_glm_stream,
|
| 225 |
[inputs, state_glm, ],
|
| 226 |
[chatbot_glm, state_glm],)
|
| 227 |
-
b1.click( predict_chatgpt,
|
| 228 |
-
[inputs, top_p_chatgpt, temperature_chatgpt, openai_api_key, chat_counter_chatgpt, chatbot_chatgpt, state_chatgpt],
|
| 229 |
-
[chatbot_chatgpt, state_chatgpt, chat_counter_chatgpt],)
|
| 230 |
|
| 231 |
-
b2.click(reset_chat, [chatbot_chatgpt, state_chatgpt], [chatbot_chatgpt, state_chatgpt])
|
| 232 |
-
#b2.click(reset_chat, [chatbot_together, state_together], [chatbot_together, state_together])
|
| 233 |
b2.click(reset_chat, [chatbot_glm, state_glm], [chatbot_glm, state_glm])
|
| 234 |
|
| 235 |
gr.HTML('''<center><a href="https://huggingface.co/spaces/ysharma/OpenChatKit_ChatGPT_Comparison?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate the Space and run securely with your OpenAI API Key</center>''')
|
| 236 |
gr.Markdown(description)
|
| 237 |
-
demo.queue(concurrency_count=16).launch(height=
|
|
|
|
| 15 |
model_chtoen = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M")
|
| 16 |
tokenizer_chtoen = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M")
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Define function to generate model predictions and update the history
|
| 19 |
def predict_glm(input, history=[]):
|
| 20 |
response, history = model_glm.chat(tokenizer_glm, input, history)
|
|
|
|
| 30 |
trans_eng_text = tokenizer_chtoen.batch_decode(generated_tokens, skip_special_tokens=True)
|
| 31 |
return trans_eng_text[0]
|
| 32 |
|
| 33 |
+
# Define generator to stream model predictions
|
| 34 |
def predict_glm_stream(input, history=[]): #, top_p, temperature):
|
|
|
|
|
|
|
|
|
|
| 35 |
top_p = 1.0
|
| 36 |
temperature = 1.0
|
| 37 |
+
for response, history in model_glm.stream_chat(tokenizer_glm, input, history, top_p=1.0, temperature=1.0): #max_length=max_length,
|
| 38 |
print(f"In for loop resonse is ^^- {response}")
|
| 39 |
print(f"In for loop history is ^^- {history}")
|
| 40 |
# translate Chinese to English
|
| 41 |
history = [(query, translate_Chinese_English(response)) for query, response in history]
|
| 42 |
print(f"In for loop translated history is ^^- {history}")
|
| 43 |
yield history, history #[history] + updates
|
| 44 |
+
|
| 45 |
|
| 46 |
"""
|
| 47 |
def predict(input, max_length, top_p, temperature, history=None):
|
|
|
|
| 81 |
In this app, you can explore the outputs of multiple LLMs when prompted in similar ways.
|
| 82 |
"""
|
| 83 |
|
| 84 |
+
with gr.Blocks(css="""#col_container {margin-left: auto; margin-right: auto;}
|
|
|
|
| 85 |
#chatglm {height: 520px; overflow: auto;} """ ) as demo:
|
|
|
|
|
|
|
| 86 |
gr.HTML(title)
|
| 87 |
+
#with gr.Row():
|
| 88 |
+
with gr.Column(): #(scale=10):
|
| 89 |
+
with gr.Box():
|
| 90 |
+
with gr.Row():
|
| 91 |
+
with gr.Column(scale=8):
|
|
|
|
| 92 |
inputs = gr.Textbox(placeholder="Hi there!", label="Type an input and press Enter ⤵️ " )
|
| 93 |
+
with gr.Column(scale=1):
|
| 94 |
+
b1 = gr.Button('🏃Run', elem_id = 'run').style(full_width=True)
|
| 95 |
+
with gr.Column(scale=1):
|
| 96 |
+
b2 = gr.Button('🔄Clear up Chatbots!', elem_id = 'clear').style(full_width=True)
|
| 97 |
+
state_glm = gr.State([])
|
| 98 |
+
|
| 99 |
+
with gr.Box():
|
| 100 |
+
chatbot_glm = gr.Chatbot(elem_id="chatglm", label='THUDM-ChatGLM6B')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
#with gr.Column(): #(scale=2, elem_id='parameters'):
|
| 103 |
+
with gr.Box():
|
| 104 |
+
gr.HTML("Parameters for ChatGLM-6B", visible=True)
|
| 105 |
+
top_p = gr.Slider(minimum=-0, maximum=1.0,value=0.25, step=0.05,interactive=True, label="Top-p", visible=False)
|
| 106 |
+
temperature = gr.Slider(minimum=-0, maximum=5.0, value=0.6, step=0.1, interactive=True, label="Temperature", visible=False)
|
| 107 |
+
#top_k = gr.Slider( minimum=1, maximum=50, value=50, step=1, interactive=True, label="Top-k", visible=False)
|
| 108 |
+
#repetition_penalty = gr.Slider( minimum=0.1, maximum=3.0, value=1.01, step=0.01, interactive=True, label="Repetition Penalty", visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
inputs.submit(reset_textbox, [], [inputs])
|
| 111 |
|
| 112 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
inputs.submit( predict_glm_stream,
|
| 114 |
[inputs, state_glm, ],
|
| 115 |
[chatbot_glm, state_glm],)
|
| 116 |
b1.click( predict_glm_stream,
|
| 117 |
[inputs, state_glm, ],
|
| 118 |
[chatbot_glm, state_glm],)
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
+
#b2.click(reset_chat, [chatbot_chatgpt, state_chatgpt], [chatbot_chatgpt, state_chatgpt])
|
|
|
|
| 121 |
b2.click(reset_chat, [chatbot_glm, state_glm], [chatbot_glm, state_glm])
|
| 122 |
|
| 123 |
gr.HTML('''<center><a href="https://huggingface.co/spaces/ysharma/OpenChatKit_ChatGPT_Comparison?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate the Space and run securely with your OpenAI API Key</center>''')
|
| 124 |
gr.Markdown(description)
|
| 125 |
+
demo.queue(concurrency_count=16).launch(height= 800, debug=True)
|