Spaces:
Runtime error
Runtime error
yizhangliu
commited on
Commit
·
01868df
1
Parent(s):
2548fd4
Update app.py
Browse files
app.py
CHANGED
@@ -16,16 +16,27 @@ def getTextTrans(text, source='zh', target='en'):
|
|
16 |
session_token = os.environ.get('SessionToken')
|
17 |
# logger.info(f"session_token_: {session_token}")
|
18 |
|
19 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
try:
|
21 |
-
api = ChatGPT(session_token)
|
22 |
resp = api.send_message(text)
|
23 |
-
api.refresh_auth()
|
24 |
-
api.reset_conversation()
|
25 |
response = resp['message']
|
|
|
|
|
26 |
# logger.info(f"response_: {response}")
|
|
|
27 |
except:
|
28 |
-
response = "Sorry, I'm busy. Try again later."
|
29 |
return response
|
30 |
|
31 |
model_ids = {
|
@@ -50,19 +61,19 @@ for model_id in model_ids.keys():
|
|
50 |
except:
|
51 |
logger.info(f"load_fail__{model_id}_")
|
52 |
|
53 |
-
def chat(input0, input1, chat_radio, chat_history):
|
54 |
out_chat = []
|
55 |
if chat_history != '':
|
56 |
out_chat = json.loads(chat_history)
|
57 |
logger.info(f"out_chat_: {len(out_chat)} / {chat_radio}")
|
58 |
if chat_radio == "Talk to chatGPT":
|
59 |
-
response = get_response_from_chatbot(input0)
|
60 |
out_chat.append((input0, response))
|
61 |
chat_history = json.dumps(out_chat)
|
62 |
-
return out_chat, input1, chat_history
|
63 |
else:
|
64 |
prompt_en = getTextTrans(input0, source='zh', target='en') + f',{random.randint(0,sys.maxsize)}'
|
65 |
-
return out_chat, prompt_en, chat_history
|
66 |
|
67 |
|
68 |
start_work = """async() => {
|
@@ -136,8 +147,8 @@ start_work = """async() => {
|
|
136 |
window['gradioEl'].querySelectorAll('#chat_radio')[0].style.flex = 'auto';
|
137 |
window['gradioEl'].querySelectorAll('#chat_radio')[0].style.width = '100%';
|
138 |
prompt_row.children[0].setAttribute('style','flex-direction: inherit; flex: 1 1 auto; width: 100%;border-color: green;border-width: 1px !important;')
|
139 |
-
window['chat_bot1'].children[1].setAttribute('style', 'border-bottom-right-radius:0;top:unset;bottom:0;padding-left:0.1rem
|
140 |
-
|
141 |
window['prevPrompt'] = '';
|
142 |
window['doCheckPrompt'] = 0;
|
143 |
window['prevImgSrc'] = '';
|
@@ -243,9 +254,10 @@ with gr.Blocks(title='Talk to chatGPT') as demo:
|
|
243 |
rounded=(True, True, True, True),
|
244 |
width=100
|
245 |
)
|
|
|
246 |
submit_btn.click(fn=chat,
|
247 |
-
inputs=[prompt_input0, prompt_input1, chat_radio, chat_history],
|
248 |
-
outputs=[chatbot, prompt_input1, chat_history],
|
249 |
)
|
250 |
with gr.Row(elem_id='tab_img', visible=False).style(height=5):
|
251 |
tab_img = gr.TabbedInterface(tab_actions, tab_titles)
|
|
|
16 |
session_token = os.environ.get('SessionToken')
|
17 |
# logger.info(f"session_token_: {session_token}")
|
18 |
|
19 |
+
def get_api():
|
20 |
+
try:
|
21 |
+
api = ChatGPT(session_token)
|
22 |
+
api.refresh_auth()
|
23 |
+
except:
|
24 |
+
api = None
|
25 |
+
return api
|
26 |
+
|
27 |
+
def get_response_from_chatbot(api, text):
|
28 |
+
if api is None:
|
29 |
+
return "Sorry, I'm busy. Try again later.(1)"
|
30 |
try:
|
|
|
31 |
resp = api.send_message(text)
|
32 |
+
api.refresh_auth()
|
|
|
33 |
response = resp['message']
|
34 |
+
conversation_id = resp['conversation_id']
|
35 |
+
parent_id = resp['parent_id']
|
36 |
# logger.info(f"response_: {response}")
|
37 |
+
# logger.info(f"conversation_id_: [{conversation_id}] / parent_id: [{parent_id}]")
|
38 |
except:
|
39 |
+
response = "Sorry, I'm busy. Try again later.(2)"
|
40 |
return response
|
41 |
|
42 |
model_ids = {
|
|
|
61 |
except:
|
62 |
logger.info(f"load_fail__{model_id}_")
|
63 |
|
64 |
+
def chat(api, input0, input1, chat_radio, chat_history):
|
65 |
out_chat = []
|
66 |
if chat_history != '':
|
67 |
out_chat = json.loads(chat_history)
|
68 |
logger.info(f"out_chat_: {len(out_chat)} / {chat_radio}")
|
69 |
if chat_radio == "Talk to chatGPT":
|
70 |
+
response = get_response_from_chatbot(api, input0)
|
71 |
out_chat.append((input0, response))
|
72 |
chat_history = json.dumps(out_chat)
|
73 |
+
return api, out_chat, input1, chat_history
|
74 |
else:
|
75 |
prompt_en = getTextTrans(input0, source='zh', target='en') + f',{random.randint(0,sys.maxsize)}'
|
76 |
+
return api, out_chat, prompt_en, chat_history
|
77 |
|
78 |
|
79 |
start_work = """async() => {
|
|
|
147 |
window['gradioEl'].querySelectorAll('#chat_radio')[0].style.flex = 'auto';
|
148 |
window['gradioEl'].querySelectorAll('#chat_radio')[0].style.width = '100%';
|
149 |
prompt_row.children[0].setAttribute('style','flex-direction: inherit; flex: 1 1 auto; width: 100%;border-color: green;border-width: 1px !important;')
|
150 |
+
window['chat_bot1'].children[1].setAttribute('style', 'border-bottom-right-radius:0;top:unset;bottom:0;padding-left:0.1rem');
|
151 |
+
|
152 |
window['prevPrompt'] = '';
|
153 |
window['doCheckPrompt'] = 0;
|
154 |
window['prevImgSrc'] = '';
|
|
|
254 |
rounded=(True, True, True, True),
|
255 |
width=100
|
256 |
)
|
257 |
+
api = gr.State(value=get_api())
|
258 |
submit_btn.click(fn=chat,
|
259 |
+
inputs=[api, prompt_input0, prompt_input1, chat_radio, chat_history],
|
260 |
+
outputs=[api, chatbot, prompt_input1, chat_history],
|
261 |
)
|
262 |
with gr.Row(elem_id='tab_img', visible=False).style(height=5):
|
263 |
tab_img = gr.TabbedInterface(tab_actions, tab_titles)
|