-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui_pa_cn.py
548 lines (494 loc) · 20.6 KB
/
ui_pa_cn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# coding=utf-8
from dotenv import load_dotenv
load_dotenv()
import urllib3
urllib3.disable_warnings()
import gradio as gr
from functools import partial
##### Auto-task
from module.auto_task import run_babyagi
from module.auto_task import run_autogpt
from module.auto_task import run_metaprompt
from module.auto_task import run_camel
from module.auto_task import run_debate
def auto_selected_agent(_task, _radio):
_ans, _steps = "", ""
if _radio == "babyagi":
_ans, _steps = run_babyagi(_task)
elif _radio == "autogpt":
_ans, _steps = run_autogpt(_task)
elif _radio == "metaprompt":
_ans, _steps = run_metaprompt(_task)
elif _radio == "camel":
_ans, _steps = run_camel(_task)
elif _radio == "debate":
_ans, _steps = run_debate(_task)
else:
_ans = f"ERROR: not supported agent: {_radio}"
return [_ans, _steps]
##### Azure Rules/Code
from module.azure_related import chat_azure_rules
from module.azure_related import llm_azure_code
##### Azure VM +
from module.query_vdb import qa_faiss_multi_query
from module.agents import agent_react_zeroshot
from module.agents import agent_openai_multifunc
from module.agents import agent_selfask_search
from module.agents import agent_react_docstore
from module.agents import agent_plan_execute
from module.tools import tools_faiss_azure_googleserp_math
from module.tools import tools_faiss_azure_googleserp
from module.tools import tools_selfask_azure
from module.tools import tools_react_docstore_azure_googleserp
# def azure_vm_selected_agent_retriever(_query, _radio):
# _ans, _steps = "", ""
# if _radio == "react_zeroshot":
# _ans, _steps = agent_react_zeroshot(
# tools_faiss_azure_googleserp_math,
# _query
# )
# elif _radio == "openai_multifunc":
# _ans, _steps = agent_openai_multifunc(
# tools_faiss_azure_googleserp,
# _query
# )
# elif _radio == "selfask_search":
# _ans, _steps = agent_selfask_search(
# tools_selfask_azure,
# _query
# )
# elif _radio == "react_docstore":
# _ans, _steps = agent_react_docstore(
# tools_react_docstore_azure_googleserp,
# _query
# )
# elif _radio == "plan_execute":
# _ans, _steps = agent_plan_execute(
# tools_faiss_azure_googleserp,
# _query
# )
# elif _radio == "qa_multiquery":
# from pathlib import Path
# _pwd = Path(__file__).absolute()
# _pa_path = _pwd.parent
# _db_name = str(_pa_path / "vdb" / "azure_sql+")
# _ans, _steps = qa_faiss_multi_query(_query, _db_name)
# else:
# _ans = f"ERROR: not supported agent or retriever: {_radio}"
# return [_ans, _steps]
##### Azure Doc
from module.query_vdb import qa_faiss_multi_query
def azure_selected_vdb(_query, _radio):
_ans, _steps = "", ""
from pathlib import Path
_pwd = Path(__file__).absolute()
_pa_path = _pwd.parent
if _radio == "vm_disk":
_db_name = str(_pa_path / "vdb" / "azure_vm")
_ans, _steps = qa_faiss_multi_query(_query, _db_name)
elif _radio == "sql_db":
_db_name = str(_pa_path / "vdb" / "azure_sql_db")
_ans, _steps = qa_faiss_multi_query(_query, _db_name)
elif _radio == "sql_mi":
_db_name = str(_pa_path / "vdb" / "azure_sql_mi")
_ans, _steps = qa_faiss_multi_query(_query, _db_name)
elif _radio == "web_apps":
_db_name = str(_pa_path / "vdb" / "azure_webapps")
_ans, _steps = qa_faiss_multi_query(_query, _db_name)
else:
_ans = f"ERROR: not supported agent or retriever: {_radio}"
return [_ans, _steps]
# def azure_sql_selected_agent_retriever(_query, _radio):
# _ans, _steps = "", ""
# if _radio == "qa_multiquery":
# from pathlib import Path
# _pwd = Path(__file__).absolute()
# _pa_path = _pwd.parent
# _db_name = str(_pa_path / "vdb" / "azure_sql+")
# _ans, _steps = qa_faiss_multi_query(_query, _db_name)
# else:
# _ans = f"ERROR: not supported agent or retriever: {_radio}"
# return [_ans, _steps]
##### Search
from module.tools import tools_googleserp
from module.tools import tools_selfask_googleserp
from module.tools import tools_react_docstore_wiki
def search_selected_agent_retriever(_query, _radio):
_ans, _steps = "", ""
if _radio == "react_zeroshot":
_ans, _steps = agent_react_zeroshot(
tools_googleserp,
_query
)
elif _radio == "openai_multifunc":
_ans, _steps = agent_openai_multifunc(
tools_googleserp,
_query
)
elif _radio == "selfask_search":
_ans, _steps = agent_selfask_search(
tools_selfask_googleserp,
_query
)
elif _radio == "react_docstore_wiki":
_ans, _steps = agent_react_docstore(
tools_react_docstore_wiki,
_query
)
elif _radio == "plan_execute":
_ans, _steps = agent_plan_execute(
tools_googleserp,
_query
)
else:
_ans = f"ERROR: not supported agent or retriever: {_radio}"
return [_ans, _steps]
##### Auto-Programming
from module.auto_programming import auto_py
##### 对话
from module.voice import audio_transcribe
from module.gradio_func import chg_btn_color_if_input
from module.gradio_func import clear_audio_microphone_if_transcribe
from module.gradio_func import transfer_input
from module.gradio_func import reset_status
from module.gradio_func import reset_textbox
from module.gradio_func import cancel_output
from module.gradio_func import delete_last_conversation
from module.chatbot import predict
from module.chatbot import retry_bot
from module.chatbot import create_chatopenai
chatagent_openai = create_chatopenai()
##### 语音
from module.voice import txt_to_mp3
##### UI
_description = """
# Assistant
"""
with gr.Blocks(title=_description) as demo:
dh_history = gr.State([])
dh_user_question = gr.State("")
gr.Markdown(_description)
# with gr.Tab(label = "Auto-task"):
# ao_query = gr.Textbox(label="Task", placeholder="Task", lines=5, max_lines=5, interactive=True, visible=True)
# ao_radio = gr.Radio(
# ["babyagi", "autogpt", "metaprompt", "camel", "debate"],
# label="Autonomous agents",
# info="What agent to use?",
# type="value",
# value="babyagi"
# )
# ao_start_btn = gr.Button("Start", variant="secondary", visible=True)
# ao_ans = gr.Textbox(label="Ans", placeholder="...", lines=15, max_lines=15, interactive=False, visible=True)
# ao_steps = gr.Textbox(label="Steps", placeholder="...", lines=15, max_lines=15, interactive=False, visible=True)
# ao_query.change(
# chg_btn_color_if_input,
# [ao_query],
# [ao_start_btn]
# )
# ao_start_btn.click(
# auto_selected_agent,
# [ao_query, ao_radio],
# [ao_ans, ao_steps]
# )
# with gr.Tab(label = "Azure Rules/Code"):
# ru_text = gr.Textbox(label="Text", placeholder="Text", lines=10, max_lines=10, interactive=True, visible=True)
# ru_rules_btn = gr.Button("Generate Rules", variant="secondary", visible=True)
# ru_rules = gr.Textbox(label="Rules", placeholder="...", lines=10, max_lines=10, interactive=True, visible=True)
# ru_rules_steps = gr.Textbox(label="Info", placeholder="...", lines=5, max_lines=5, interactive=False, visible=True)
# ru_code_btn = gr.Button("Generate Code", variant="secondary", visible=True)
# ru_code = gr.Textbox(label="Code", placeholder="...", lines=15, max_lines=15, interactive=False, visible=True)
# ru_code_steps = gr.Textbox(label="Info", placeholder="...", lines=10, max_lines=10, interactive=False, visible=True)
# ru_text.change(
# chg_btn_color_if_input,
# [ru_text],
# [ru_rules_btn]
# )
# ru_rules_btn.click(
# chat_azure_rules,
# [ru_text],
# [ru_rules, ru_rules_steps]
# )
# ru_rules.change(
# chg_btn_color_if_input,
# [ru_rules],
# [ru_code_btn]
# )
# ru_code_btn.click(
# llm_azure_code,
# [ru_rules],
# [ru_code, ru_code_steps]
# )
# with gr.Tab(label = "Azure VM +"):
# az_query = gr.Textbox(label="Query", placeholder="Query", lines=10, max_lines=10, interactive=True, visible=True)
# az_radio = gr.Radio(
# ["react_zeroshot", "openai_multifunc", "selfask_search", "react_docstore", "plan_execute", "qa_multiquery"],
# label="Agent & Retriever",
# info="What agent or retriever to use?",
# type="value",
# value="qa_multiquery"
# )
# az_start_btn = gr.Button("Start", variant="secondary", visible=True)
# az_ans = gr.Textbox(label="Ans", placeholder="...", lines=15, max_lines=15, interactive=False, visible=True)
# az_steps = gr.Textbox(label="Steps", placeholder="...", lines=15, max_lines=15, interactive=False, visible=True)
# az_query.change(
# chg_btn_color_if_input,
# [az_query],
# [az_start_btn]
# )
# az_start_btn.click(
# azure_vm_selected_agent_retriever,
# [az_query, az_radio],
# [az_ans, az_steps]
# )
with gr.Tab(label = "Azure Doc"):
az_query = gr.Textbox(label="Query", placeholder="Query", lines=10, max_lines=10, interactive=True, visible=True)
az_radio = gr.Radio(
["vm_disk", "sql_db", "sql_mi", "web_apps"],
label="Which Azure cloud service do you want to know about?",
info="",
type="value",
value="vm_disk"
)
az_start_btn = gr.Button("Start", variant="secondary", visible=True)
az_ans = gr.Textbox(label="Ans", placeholder="...", lines=15, max_lines=15, interactive=False, visible=True)
az_steps = gr.Textbox(label="Steps", placeholder="...", lines=15, max_lines=15, interactive=False, visible=True)
az_query.change(
chg_btn_color_if_input,
[az_query],
[az_start_btn]
)
az_start_btn.click(
azure_selected_vdb,
[az_query, az_radio],
[az_ans, az_steps]
)
with gr.Tab(label = "Search"):
sh_query = gr.Textbox(label="Query", placeholder="Query", lines=10, max_lines=10, interactive=True, visible=True)
sh_radio = gr.Radio(
["react_zeroshot", "openai_multifunc", "selfask_search", "react_docstore_wiki", "plan_execute"],
label="Agent & Retriever",
info="What agent or retriever to use?",
type="value",
value="react_zeroshot"
)
sh_start_btn = gr.Button("Start", variant="secondary", visible=True)
sh_ans = gr.Textbox(label="Ans", placeholder="...", lines=15, max_lines=15, interactive=False, visible=True)
sh_steps = gr.Textbox(label="Steps", placeholder="...", lines=15, max_lines=15, interactive=False, visible=True)
sh_query.change(
chg_btn_color_if_input,
[sh_query],
[sh_start_btn]
)
sh_start_btn.click(
search_selected_agent_retriever,
[sh_query, sh_radio],
[sh_ans, sh_steps]
)
with gr.Tab(label = "Auto-Programming"):
# with gr.Row():
# openai_api_key = gr.Textbox(label="OpenAI API Key", placeholder="sk-**********, will much better if use gpt-4", lines=1, visible=True)
# with gr.Row(equal_height=True):
# with gr.Column(scale=4):
# with gr.Row():
# ap_task = gr.Textbox(label="Task", placeholder="Task", lines=3, max_lines=3, interactive=True, visible=True)
# # with gr.Row():
# # ap_ans = gr.Textbox(label="Ans", placeholder="...", lines=3, max_lines=3, interactive=False, visible=True)
# with gr.Row():
# ap_generated = gr.File(label="Generated Files", file_count="multiple", type="file", interactive=False, visible=True)
# ap_start_btn = gr.Button("Start", variant="secondary", visible=True)
# with gr.Column(scale=4):
# ap_steps = gr.Textbox(label="Steps", placeholder="...", lines=15, max_lines=16, interactive=False, visible=True)
ap_task = gr.Textbox(label="Task", placeholder="Task", lines=10, max_lines=10, interactive=True, visible=True)
ap_start_btn = gr.Button("Start", variant="secondary", visible=True)
ap_steps = gr.Textbox(label="Steps", placeholder="...", lines=15, max_lines=15, interactive=False, visible=True)
ap_generated = gr.File(label="Generated Files", file_count="multiple", type="file", interactive=False, visible=True)
ap_task.change(
chg_btn_color_if_input,
[ap_task],
[ap_start_btn]
)
ap_start_btn.click(
auto_py,
[ap_task],
[ap_generated, ap_steps]
)
with gr.Tab(label = "对话"):
dh_chatbot = gr.Chatbot([], elem_id="chatbot", height=300)
with gr.Row(equal_height=True):
with gr.Column(scale=9):
dh_user_input = gr.Textbox(show_label=False, placeholder="输入", lines=1, max_lines=1, container=False)
dh_submit_btn = gr.Button("提问", visible=True)
with gr.Row(equal_height=True):
with gr.Column(scale=9):
dh_audio_microphone = gr.Audio(source="microphone", type="filepath", label="录音", format="mp3")
dh_transcribe_btn = gr.Button("转录", visible=True)
with gr.Row(equal_height=True):
dh_cancel_btn = gr.Button("🛑 停止", visible=False)
dh_retry_btn = gr.Button("🔃 重答")
dh_delLast_btn = gr.Button("🔙 回退", visible=False)
dh_empty_btn = gr.Button("🧹 重启")
with gr.Row(equal_height=True):
status_display = gr.Markdown("🚩 Status", elem_id="status_display")
dh_audio_microphone.change(
chg_btn_color_if_input,
[dh_audio_microphone],
[dh_transcribe_btn]
)
dh_transcribe_btn.click(
audio_transcribe,
[dh_audio_microphone],
[dh_user_input],
show_progress=True
).then(
clear_audio_microphone_if_transcribe,
[],
[dh_audio_microphone],
).then(
chg_btn_color_if_input,
[dh_audio_microphone],
[dh_transcribe_btn]
)
dh_user_input.change(
chg_btn_color_if_input,
[dh_user_input],
[dh_submit_btn]
)
predict_event_1 = dh_submit_btn.click(
transfer_input,
[dh_user_input],
[dh_user_question, dh_user_input],
show_progress=True
).then(
partial(predict, chatagent_openai),
[dh_user_question, dh_chatbot, dh_history],
[dh_chatbot, dh_history, status_display],
show_progress=True
)
predict_event_2 = dh_retry_btn.click(
partial(retry_bot, chatagent_openai),
[dh_user_input, dh_chatbot, dh_history],
[dh_chatbot, dh_history, status_display],
show_progress=True
)
dh_delLast_btn.click(
delete_last_conversation,
[dh_chatbot, dh_history],
[dh_chatbot, dh_history, status_display],
show_progress=True
)
dh_empty_btn.click(
reset_textbox,
[],
[dh_user_input, status_display]
)
dh_empty_btn.click(
reset_status,
[],
[dh_chatbot, dh_history, status_display],
show_progress=True
)
dh_cancel_btn.click(
cancel_output,
[],
[status_display],
cancels=[predict_event_1, predict_event_2]
)
# with gr.Tab(label = "搜索"):
# with gr.Row(equal_height=True):
# with gr.Column(scale=5):
# with gr.Row():
# gzl_search_ask = gr.Textbox(label="提问", placeholder="提问", lines=3, max_lines=3, interactive=True, visible=True)
# with gr.Row():
# gzl_search_ans = gr.Textbox(label="回答", placeholder="...", lines=3, max_lines=3, interactive=False, visible=True)
# gzl_search_zero_btn = gr.Button("搜索 zero", variant="secondary", visible=True)
# gzl_search_wiki_btn = gr.Button("搜索 wiki", variant="secondary", visible=True)
# # gzl_search_serp_btn = gr.Button("搜索 web", variant="secondary", visible=True)
# with gr.Column(scale=3):
# gzl_search_steps = gr.Textbox(label="中间步骤", placeholder="...", lines=10, max_lines=10, interactive=False, visible=True)
# gzl_search_ask.change(
# chg_btn_color_if_input,
# [gzl_search_ask],
# [gzl_search_zero_btn]
# )
# # gzl_search_ask.change(
# # chg_btn_color_if_input,
# # [gzl_search_ask],
# # [gzl_search_serp_btn]
# # )
# gzl_search_ask.change(
# chg_btn_color_if_input,
# [gzl_search_ask],
# [gzl_search_wiki_btn]
# )
# gzl_search_zero_btn.click(
# partial(agent_react_zeroshot, tools_faiss_azure_langchain_googleserp_math),
# [gzl_search_ask],
# [gzl_search_ans, gzl_search_steps]
# )
# # gzl_search_serp_btn.click(
# # partial(agent_selfask_search, tools_selfask_search),
# # [gzl_search_ask],
# # [gzl_search_ans, gzl_search_steps]
# # )
# gzl_search_wiki_btn.click(
# partial(agent_react_docstore, tools_react_docstore_wiki),
# [gzl_search_ask],
# [gzl_search_ans, gzl_search_steps]
# )
with gr.Tab(label = "语音"):
with gr.Row(equal_height=True):
with gr.Column(scale=6):
yy_txt = gr.Textbox(label="输入文字", placeholder="input text", lines=5, max_lines=5, interactive=True, visible=True)
yy_btn = gr.Button("转换", variant="secondary", visible=True)
with gr.Column(scale=3):
yy_mp3 = gr.Audio(label="生成语音", visible=True)
yy_txt.change(
chg_btn_color_if_input,
[yy_txt],
[yy_btn]
)
yy_btn.click(
txt_to_mp3,
[yy_txt],
[yy_mp3]
)
# with gr.Tab(label = "工具集"):
# with gr.Row():
# openai_api_key = gr.Textbox(label="OpenAI API Key", placeholder="sk-**********", lines=1, visible=True)
# with gr.Tab(label = "设置"):
# with gr.Row():
# openai_api_key = gr.Textbox(label="OpenAI API Key", placeholder="sk-**********", lines=1, visible=True)
# with gr.Row():
# _api_key = gr.Textbox(label="API Key", placeholder="**********", lines=1, visible=True)
# from fastapi import FastAPI, Response
# import json
# app = FastAPI()
# @app.get("/health")
# def index():
# return {"message": "active"}
# app = gr.mount_gradio_app(app, demo.queue(), path="/")
## uvicorn ui_cn_pa:app --reload
if __name__ == "__main__":
demo.queue(concurrency_count=1).launch(
server_name="0.0.0.0",
server_port=7788,
share=False,
favicon_path="./asset/favicon_pa.png",
# auth = ('sz','1123'),
# auth_message= "欢迎回来!",
ssl_verify=False,
# ssl_keyfile="./localhost+2-key.pem",
# ssl_certfile="./localhost+2.pem",
ssl_keyfile="./ssl/key.pem",
ssl_certfile="./ssl/cert.pem",
)
# import uvicorn
# uvicorn.run(
# app,
# host="0.0.0.0",
# port=7788,
# ssl_keyfile="./localhost+2-key.pem",
# ssl_certfile="./localhost+2.pem",
# reload=True,
# debug=True
# )