Spaces:
Runtime error
Runtime error
yuntian-deng
commited on
Commit
•
2b2be0b
1
Parent(s):
bfcdf7c
Update app.py
Browse files
app.py
CHANGED
@@ -4,15 +4,11 @@ import sys
|
|
4 |
import json
|
5 |
import requests
|
6 |
|
7 |
-
#Streaming endpoint
|
8 |
MODEL = "gpt-3.5-turbo"
|
9 |
API_URL = os.getenv("API_URL")
|
10 |
DISABLED = os.getenv("DISABLED") == 'True'
|
11 |
-
|
12 |
-
#Testing with my Open AI Key
|
13 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
14 |
|
15 |
-
#Supress errors
|
16 |
def exception_handler(exception_type, exception, traceback):
|
17 |
print("%s: %s" % (exception_type.__name__, exception))
|
18 |
sys.excepthook = exception_handler
|
@@ -32,8 +28,7 @@ def parse_codeblock(text):
|
|
32 |
lines[i] = "<br/>" + line.replace("<", "<").replace(">", ">")
|
33 |
return "".join(lines)
|
34 |
|
35 |
-
def predict(inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
|
36 |
-
|
37 |
payload = {
|
38 |
"model": MODEL,
|
39 |
"messages": [{"role": "user", "content": f"{inputs}"}],
|
@@ -58,15 +53,15 @@ def predict(inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
|
|
58 |
role = 'user'
|
59 |
else:
|
60 |
role = 'assistant'
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
messages.append(
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
messages.append(
|
70 |
payload = {
|
71 |
"model": MODEL,
|
72 |
"messages": messages,
|
@@ -81,39 +76,44 @@ def predict(inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
|
|
81 |
chat_counter+=1
|
82 |
|
83 |
history.append(inputs)
|
84 |
-
# make a POST request to the API endpoint using the requests.post method, passing in stream=True
|
85 |
-
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
86 |
-
response_code = f"{response}"
|
87 |
-
if response_code.strip() != "<Response [200]>":
|
88 |
-
raise Exception(f"Sorry, hitting rate limit. Please try again later. {response}")
|
89 |
token_counter = 0
|
90 |
partial_words = ""
|
91 |
counter = 0
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
#
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
print(json.dumps({"chat_counter": chat_counter, "payload": payload, "partial_words": partial_words, "token_counter": token_counter, "counter": counter}))
|
113 |
|
114 |
|
115 |
def reset_textbox():
|
116 |
-
return gr.update(value='')
|
117 |
|
118 |
title = """<h1 align="center">GPT-3.5 Chatbot</h1>"""
|
119 |
if DISABLED:
|
@@ -177,11 +177,11 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
|
|
177 |
def enable_inputs():
|
178 |
return user_consent_block.update(visible=False), main_block.update(visible=True)
|
179 |
|
180 |
-
accept_button.click(fn=enable_inputs, inputs=[], outputs=[user_consent_block, main_block])
|
181 |
|
182 |
-
inputs.submit(
|
183 |
-
|
184 |
-
b1.click(reset_textbox, [], [inputs])
|
185 |
-
|
186 |
-
|
187 |
-
demo.queue(max_size=20, concurrency_count=10).launch()
|
|
|
4 |
import json
|
5 |
import requests
|
6 |
|
|
|
7 |
MODEL = "gpt-3.5-turbo"
|
8 |
API_URL = os.getenv("API_URL")
|
9 |
DISABLED = os.getenv("DISABLED") == 'True'
|
|
|
|
|
10 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
11 |
|
|
|
12 |
def exception_handler(exception_type, exception, traceback):
|
13 |
print("%s: %s" % (exception_type.__name__, exception))
|
14 |
sys.excepthook = exception_handler
|
|
|
28 |
lines[i] = "<br/>" + line.replace("<", "<").replace(">", ">")
|
29 |
return "".join(lines)
|
30 |
|
31 |
+
def predict(inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
|
|
|
32 |
payload = {
|
33 |
"model": MODEL,
|
34 |
"messages": [{"role": "user", "content": f"{inputs}"}],
|
|
|
53 |
role = 'user'
|
54 |
else:
|
55 |
role = 'assistant'
|
56 |
+
message = {}
|
57 |
+
message["role"] = role
|
58 |
+
message["content"] = data
|
59 |
+
messages.append(message)
|
60 |
|
61 |
+
message = {}
|
62 |
+
message["role"] = "user"
|
63 |
+
message["content"] = inputs
|
64 |
+
messages.append(message)
|
65 |
payload = {
|
66 |
"model": MODEL,
|
67 |
"messages": messages,
|
|
|
76 |
chat_counter+=1
|
77 |
|
78 |
history.append(inputs)
|
|
|
|
|
|
|
|
|
|
|
79 |
token_counter = 0
|
80 |
partial_words = ""
|
81 |
counter = 0
|
82 |
+
|
83 |
+
try:
|
84 |
+
# make a POST request to the API endpoint using the requests.post method, passing in stream=True
|
85 |
+
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
86 |
+
response_code = f"{response}"
|
87 |
+
#if response_code.strip() != "<Response [200]>":
|
88 |
+
# #print(f"response code - {response}")
|
89 |
+
# raise Exception(f"Sorry, hitting rate limit. Please try again later. {response}")
|
90 |
+
|
91 |
+
for chunk in response.iter_lines():
|
92 |
+
#Skipping first chunk
|
93 |
+
if counter == 0:
|
94 |
+
counter += 1
|
95 |
+
continue
|
96 |
+
#counter+=1
|
97 |
+
# check whether each line is non-empty
|
98 |
+
if chunk.decode() :
|
99 |
+
chunk = chunk.decode()
|
100 |
+
# decode each line as response data is in bytes
|
101 |
+
if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
|
102 |
+
partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"]
|
103 |
+
if token_counter == 0:
|
104 |
+
history.append(" " + partial_words)
|
105 |
+
else:
|
106 |
+
history[-1] = partial_words
|
107 |
+
token_counter += 1
|
108 |
+
yield [(parse_codeblock(history[i]), parse_codeblock(history[i + 1])) for i in range(0, len(history) - 1, 2) ], history, chat_counter, response, gr.update(interactive=False), gr.update(interactive=False) # resembles {chatbot: chat, state: history}
|
109 |
+
except Exception as e:
|
110 |
+
print (f'error found: {e}')
|
111 |
+
yield [(parse_codeblock(history[i]), parse_codeblock(history[i + 1])) for i in range(0, len(history) - 1, 2) ], history, chat_counter, response, gr.update(interactive=True), gr.update(interactive=True)
|
112 |
print(json.dumps({"chat_counter": chat_counter, "payload": payload, "partial_words": partial_words, "token_counter": token_counter, "counter": counter}))
|
113 |
|
114 |
|
115 |
def reset_textbox():
|
116 |
+
return gr.update(value='', interactive=False), gr.update(interactive=False)
|
117 |
|
118 |
title = """<h1 align="center">GPT-3.5 Chatbot</h1>"""
|
119 |
if DISABLED:
|
|
|
177 |
def enable_inputs():
|
178 |
return user_consent_block.update(visible=False), main_block.update(visible=True)
|
179 |
|
180 |
+
accept_button.click(fn=enable_inputs, inputs=[], outputs=[user_consent_block, main_block], queue=False)
|
181 |
|
182 |
+
inputs.submit(reset_textbox, [], [inputs, b1], queue=False)
|
183 |
+
inputs.submit(predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code, inputs, b1],) #openai_api_key
|
184 |
+
b1.click(reset_textbox, [], [inputs, b1], queue=False)
|
185 |
+
b1.click(predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code, inputs, b1],) #openai_api_key
|
186 |
+
|
187 |
+
demo.queue(max_size=20, concurrency_count=10, api_open=False).launch()
|