-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui_rag.py
719 lines (645 loc) · 23.7 KB
/
ui_rag.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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
import sys, re
import urllib3
urllib3.disable_warnings()
import gradio as gr
# from functools import partial
import concurrent.futures as cf
import threading
import time
import shutil
import os
import ast
from repolya.chat import chat_predict_openai
from repolya.rag.vdb_faiss import (
get_faiss_OpenAI,
get_faiss_HuggingFace,
merge_faiss_OpenAI,
)
from repolya.rag.qa_chain import (
qa_vdb_multi_query,
qa_docs_ensemble_query,
qa_docs_parent_query,
qa_summerize,
qa_with_context_as_mio,
)
from repolya.rag.doc_loader import (
get_docs_from_pdf,
clean_txt,
)
from repolya.rag.doc_splitter import split_pdf_docs_recursive
from repolya.rag.digest_dir import (
calculate_md5,
dir_to_faiss_OpenAI,
)
from repolya.autogen.wf_jd import (
generate_search_dict_for_event,
generate_event_context,
generate_event_plan,
clean_filename,
)
from repolya._const import LOG_ROOT, WORKSPACE_RAG, AUTOGEN_JD
from repolya._log import logger_rag
# from autogen import ChatCompletion
_log_ans1 = LOG_ROOT / '_ans1.txt'
_log_ref1 = LOG_ROOT / '_ref1.txt'
_log_ans2 = LOG_ROOT / '_ans2.txt'
_log_ref2 = LOG_ROOT / '_ref2.txt'
_log_ans3 = LOG_ROOT / '_ans3.txt'
_log_ref3 = LOG_ROOT / '_ref3.txt'
_log_ans_ml = LOG_ROOT / '_ans_ml.txt'
_log_ref_ml = LOG_ROOT / '_ref_ml.txt'
_log_ans_yj_context = LOG_ROOT / '_ans_yj_context.txt'
_log_ans_yj_plan = LOG_ROOT / '_ans_yj_plan.txt'
_log_ref_yj = LOG_ROOT / '_ref_yj.txt'
from repolya.toolset.tool_bshr import bshr_vdb
from repolya.autogen.workflow import (
create_jdml_task_list_zh,
create_rag_task_list_zh,
search_faiss_openai,
)
from ui_rag_JQ import JQ_openai_tagging
##### tagging
def read_file(file):
if file:
with open(file.name, encoding="utf-8") as f:
content = f.read()
return content
def chg_btn_color_if_file(file):
if file:
return gr.Button(variant="primary")
else:
return gr.Button(variant="secondary")
def llm_JQ(file_name):
import os
import re
_log = ""
_JQ_str = ""
_total_cost_str = ""
# print(f"file_name: {file_name}")
if os.path.exists(file_name):
left, right = os.path.splitext(os.path.basename(file_name))
global output_JQ_file
output_JQ_file = f"{left}_JQ.txt"
with open(file_name, encoding='utf-8') as rf:
_txt = rf.read()
txt_lines = clean_txt(_txt)
txt_lines = txt_lines.split('\n')
[_log, _JQ_str, _total_cost_str, _sentences] = JQ_openai_tagging(txt_lines)
with open(output_JQ_file, "w", encoding='utf-8') as wf:
wf.write(_JQ_str)
# _JQ = ast.literal_eval(_JQ_str)
# print(type(_JQ), _JQ)
return _log
def run_llm_JQ(file):
if file:
return llm_JQ(file.name)
else:
return ["错误: 请先上传一个TXT文件!"]
def show_JQ_file(text):
# print(f"text: {text}")
if text:
if output_JQ_file:
return gr.File(value=output_JQ_file, visible=True)
else:
if output_JQ_file:
return gr.File(value=output_JQ_file)
##### upload dir
_upload_dir = WORKSPACE_RAG / 'lj_rag_upload'
if not os.path.exists(_upload_dir):
os.makedirs(_upload_dir)
_db_name = str(WORKSPACE_RAG / 'lj_rag_openai')
_clean_txt_dir = str(WORKSPACE_RAG / 'lj_rag_clean_txt')
##### log
def write_log_ans(_log_ans, _txt, _status=None):
with open(_log_ans, 'w', encoding='utf-8') as wf:
if _status == "continue":
_txt += "\n\n计算中,请稍候..."
# elif _status == "done":
# _txt += "\n\n[完成]"
wf.write(_txt)
def write_log_ref(_log_ref, _txt):
with open(_log_ref, 'w', encoding='utf-8') as wf:
wf.write(_txt)
def rag_read_logs():
with open(_log_ans1, "r") as f:
_ans1 = f.read()
with open(_log_ref1, "r") as f:
_ref1 = f.read()
with open(_log_ans2, "r") as f:
_ans2 = f.read()
with open(_log_ref2, "r") as f:
_ref2 = f.read()
with open(_log_ans3, "r") as f:
_ans3 = f.read()
with open(_log_ref3, "r") as f:
_ref3 = f.read()
return [_ans1, _ref1, _ans2, _ref2, _ans3, _ref3]
def ml_read_logs():
with open(_log_ans_ml, "r") as f:
_ans_ml = f.read()
with open(_log_ref_ml, "r") as f:
_ref_ml = f.read()
return [_ans_ml, _ref_ml]
def yj_read_logs():
with open(_log_ref_yj, "r") as f:
_ref_yj = f.read()
with open(_log_ans_yj_context, "r") as f:
_ans_yj_context = f.read()
with open(_log_ans_yj_plan, "r") as f:
_ans_yj_plan = f.read()
return [_ref_yj, _ans_yj_context, _ans_yj_plan]
def rag_clean_logs():
write_log_ans(_log_ans1,'')
write_log_ref(_log_ref1,'')
write_log_ans(_log_ans2,'')
write_log_ref(_log_ref2,'')
write_log_ans(_log_ans3,'')
write_log_ref(_log_ref3,'')
def ml_clean_logs():
write_log_ans(_log_ans_ml,'')
write_log_ref(_log_ref_ml,'')
def yj_clean_logs():
write_log_ref(_log_ref_yj,'')
write_log_ans(_log_ans_yj_context,'')
write_log_ans(_log_ans_yj_plan,'')
rag_clean_logs()
ml_clean_logs()
yj_clean_logs()
def rag_clean_all():
rag_clean_logs()
print('rag_clean_logs()')
return [gr.Textbox(value=""), gr.Button(variant="secondary")]
def ml_clean_all():
ml_clean_logs()
print('ml_clean_logs()')
return [gr.Textbox(value=""), gr.Button(variant="secondary")]
def yj_clean_all():
yj_clean_logs()
print('yj_clean_logs()')
return [
gr.Textbox(value=""), # yj_query
gr.Button(variant="secondary"), # yj_start_btn
gr.Button(variant="secondary"), # yj_plan_btn
gr.Textbox(value=""), # yj_log
gr.Textbox(value=""), # yj_context
gr.Textbox(value=""), # yj_plan
]
##### btn, textbox
def chg_btn_color_if_input(_topic):
if _topic:
return gr.Button(variant="primary")
else:
return gr.Button(variant="secondary")
def chg_textbox_visible(_radio):
if _radio == '快速':
return {
rag_ans1: gr.Textbox(visible=True),
rag_log1: gr.Textbox(visible=True),
rag_ans2: gr.Textbox(visible=False),
rag_log2: gr.Textbox(visible=False),
rag_ans3: gr.Textbox(visible=False),
rag_log3: gr.Textbox(visible=False),
}
if _radio == '深思':
return {
rag_ans1: gr.Textbox(visible=False),
rag_log1: gr.Textbox(visible=False),
rag_ans2: gr.Textbox(visible=True),
rag_log2: gr.Textbox(visible=True),
rag_ans3: gr.Textbox(visible=False),
rag_log3: gr.Textbox(visible=False),
}
if _radio == '多智':
return {
rag_ans1: gr.Textbox(visible=False),
rag_log1: gr.Textbox(visible=False),
rag_ans2: gr.Textbox(visible=False),
rag_log2: gr.Textbox(visible=False),
rag_ans3: gr.Textbox(visible=True),
rag_log3: gr.Textbox(visible=True),
}
def is_dir_empty(dir_path):
"""
检查指定目录是否为空
"""
if not os.path.exists(dir_path):
return True
return len(os.listdir(dir_path)) == 0
def move_dir_content(src_dir, dst_dir):
"""
将 src_dir 下的所有内容移动到 dst_dir 下
"""
for item in os.listdir(src_dir):
src_path = os.path.join(src_dir, item)
dst_path = os.path.join(dst_dir, item)
if os.path.isfile(src_path):
shutil.move(src_path, dst_path)
elif os.path.isdir(src_path):
if not os.path.exists(dst_path):
os.makedirs(dst_path)
move_dir_content(src_path, dst_path)
##### RAG
def rag_handle_upload(_tmp_path):
_tmp_files = []
_out = []
for i in _tmp_path:
i_fp = i.name
_tmp_files.append(i_fp)
i_fn = os.path.basename(i_fp)
i_dir = os.path.dirname(i_fp)
# print(i_dir)
i_md5 = calculate_md5(i_fp)
# print(i_md5)
i_fn_new = f"{i_md5}" + os.path.splitext(os.path.basename(i_fp))[1]
i_fp_new = os.path.join(_upload_dir, i_fn_new)
i_db_name = os.path.join(_upload_dir, f"{i_md5}_openai")
# print(i_fp_new)
if not os.path.exists(i_fp_new):
logger_rag.info(f"upload {i_fn} to {i_fn_new}")
dir_to_faiss_OpenAI(i_dir, i_db_name, _clean_txt_dir)
shutil.move(i_fp, i_fp_new)
if is_dir_empty(_db_name):
print(i_db_name, _db_name)
move_dir_content(i_db_name, _db_name)
else:
merge_faiss_OpenAI(_db_name, i_db_name)
shutil.rmtree(i_db_name)
logger_rag.info(f"done upload process")
_out.append(f"upload {i_fn} to {i_fn_new}")
else:
logger_rag.info(f"{i_fn} ({i_fn_new}) exists")
_out.append(f"{i_fn} ({i_fn_new}) exists")
return "\n".join(_out)
def qa_faiss_openai(_query, _vdb):
start_time = time.time()
_ans, _step, _token_cost = qa_vdb_multi_query(_query, _vdb, 'stuff')
end_time = time.time()
execution_time = end_time - start_time
_time = f"Time: {execution_time:.1f} seconds"
logger_rag.info(f"{_time}")
return [_ans, _step, _token_cost, _time]
def sum_token_cost_from_text(text):
"""Extract and sum tokens, cost, and time from a given text."""
token_matches = re.findall(r"Tokens: (\d+)", text)
cost_matches = re.findall(r"Cost: \$([0-9.]+)", text)
time_matches = re.findall(r"Time: ([0-9.]+) seconds", text)
total_tokens = sum(int(token) for token in token_matches)
total_cost = sum(float(cost) for cost in cost_matches)
### 除了最后一步,前面几步都是并行的,所以只计算最后两个时间
total_time = sum(float(time) for time in time_matches[-2:])
_out = f"Tokens: {total_tokens}\nCost: ${format(total_cost, '.3f')}\nTime: {total_time:.1f} seconds"
return _out
def rag_helper_fast(_query, _radio):
_vdb = get_faiss_OpenAI(_db_name)
_ans, _ref = "", ""
write_log_ans(_log_ans1,'')
write_log_ref(_log_ref1,'')
if _radio == "快速":
with cf.ProcessPoolExecutor() as executor:
write_log_ans(_log_ans1, '', 'continue')
_ans, _step, _token_cost, _time = qa_faiss_openai(_query, _vdb)
_ref = f"{_token_cost}\n{_time}\n\n{_step}"
write_log_ans(_log_ans1, clean_txt(_ans), 'done')
write_log_ref(_log_ref1, _ref)
return
def rag_helper_advanced(_query, _radio):
_ans, _ref = "", ""
write_log_ans(_log_ans2,'')
write_log_ref(_log_ref2,'')
if _radio == "深思":
write_log_ans(_log_ans2, '', 'continue')
start_time = time.time()
_ans, _token_cost = bshr_vdb(_query, _db_name)
print(_ans)
end_time = time.time()
execution_time = end_time - start_time
_time = f"Time: {execution_time:.1f} seconds"
_ref = f"{_token_cost}\n{_time}"
write_log_ans(_log_ans2, clean_txt(_ans), 'done')
write_log_ref(_log_ref2, _ref)
return
def rag_helper_autogen(_query, _radio):
_vdb = get_faiss_OpenAI(_db_name)
_ans, _ref = "", ""
write_log_ans(_log_ans3,'')
write_log_ref(_log_ref3,'')
if _radio == "多智":
start_time = time.time()
write_log_ans(_log_ans3, '', 'continue')
# ChatCompletion.start_logging(reset_counter=True, compact=False)
### task list
_task_list, _token_cost = create_rag_task_list_zh(_query)
write_log_ans(_log_ans3, f"生成的子问题列表:\n\n{_task_list}", 'continue')
end_time = time.time()
execution_time = end_time - start_time
_time = f"Time: {execution_time:.1f} seconds"
write_log_ref(_log_ref3, f"\n\n{_time}")
# print(f"cost_usage: {cost_usage(ChatCompletion.logged_history)}")
### context
_context, _token_cost = search_faiss_openai(_task_list, _vdb)
write_log_ans(_log_ans3, f"生成的 QA 上下文:\n\n{_context}", 'continue')
end_time = time.time()
execution_time = end_time - start_time
_time = f"Time: {execution_time:.1f} seconds"
write_log_ref(_log_ref3, f"\n\n{_time}")
### qa
_qa, _tc = qa_with_context_as_mio(_query, _context)
write_log_ans(_log_ans3, f"生成的最终答案:\n\n{_qa}", 'done')
end_time = time.time()
execution_time = end_time - start_time
_time = f"Time: {execution_time:.1f} seconds"
write_log_ref(_log_ref3, f"\n\n{_time}\n\n{'='*40}\n\n{_context}")
return
def rag_helper(_query, _radio):
if _radio == "快速":
logger_rag.info("[快速]")
rag_helper_fast(_query, _radio)
if _radio == "深思":
logger_rag.info("[深思]")
rag_helper_advanced(_query, _radio)
if _radio == "多智":
logger_rag.info("[多智]")
rag_helper_autogen(_query, _radio)
##### ml
def ml_helper(_query):
_ans, _ref = "", ""
write_log_ans(_log_ans_ml,'')
write_log_ref(_log_ref_ml,'')
start_time = time.time()
write_log_ans(_log_ans_ml, '', 'continue')
# ChatCompletion.start_logging(reset_counter=True, compact=False)
### task list
_task_list = create_jdml_task_list_zh(_query)
write_log_ans(_log_ans_ml, f"{_task_list}", 'done')
end_time = time.time()
execution_time = end_time - start_time
_time = f"Time: {execution_time:.1f} seconds"
write_log_ref(_log_ref_ml, f"\n\n{_time}")
##### yj
yj_keyword = {
"基本情况_1": "发生发展时间线",
"基本情况_2": "灾害规模和强度",
"处置过程_1": "实施应急响应和救援措施的时间线",
"处置过程_2": "政府和非政府组织角色",#
"军民协作_1": "军队参与救灾行动的时间线",
"军民协作_2": "军队救援行动和协作细节",
"法规依据_1": "军队协助救灾的法律依据",
"法规依据_2": "应急救援军地联动机制",
"影响评估_1": "灾害对经济和社会的影响",
"影响评估_2": "受灾群体和地区的恢复进程",
"反思启示_1": "灾害管理和应对的有效性评估",
"反思启示_2": "灾害的经验教训和改进措施",
}
yj_section = [
"基本情况",
"处置过程",
"军民协作",
"法规依据",
"影响评估",
"反思启示",
]
def yj_sort_out_context(_event):
write_log_ans(_log_ans_yj_context, '')
start_time = time.time()
write_log_ans(_log_ans_yj_context, '', 'continue')
#####
_dict = generate_search_dict_for_event(_event, yj_keyword)
global _content_fp
_context, _content_fp = generate_event_context(_event, _dict, yj_keyword, yj_section)
write_log_ans(_log_ans_yj_context, _context, 'done')
#####
end_time = time.time()
execution_time = end_time - start_time
_time = f"'梳理脉络'耗时:{execution_time:.1f} seconds"
print(_time)
time.sleep(1)
return gr.Button(variant="primary")
def yj_write_plan(_event, _context):
write_log_ans(_log_ans_yj_plan, '')
start_time = time.time()
write_log_ans(_log_ans_yj_plan, '', 'continue')
#####
_plan = generate_event_plan(_event, _context)
write_log_ans(_log_ans_yj_plan, _plan, 'done')
#####
end_time = time.time()
execution_time = end_time - start_time
_time = f"'总结报告'耗时:{execution_time:.1f} seconds"
print(_time)
def show_YJ_context(text):
# print(f"text: {text}")
if text:
if _content_fp:
return gr.File(value=_content_fp, visible=True)
else:
if _content_fp:
return gr.File(value=_content_fp)
##### UI
_description = """
# 应急事件 / 报文问答 / 命令解析 / 标签提取
"""
chat_ask = gr.Textbox(label="", placeholder="...", lines=5, max_lines=5, interactive=True, visible=True, scale=9)
with gr.Blocks(title=_description) as demo:
dh_history = gr.State([])
dh_user_question = gr.State("")
gr.Markdown(_description)
with gr.Tab(label = "应急事件"):
with gr.Row():
with gr.Column(scale=1):
yj_query = gr.Textbox(label="专题", placeholder="...", lines=8, max_lines=8, interactive=True, visible=True)
yj_start_btn = gr.Button("开始梳理", variant="secondary", visible=True)
yj_clean_btn = gr.Button("清空", variant="secondary", visible=True)
with gr.Column(scale=1):
yj_log = gr.Textbox(label="日志", placeholder="...", lines=14, max_lines=14, interactive=False, visible=True)
yj_context = gr.Textbox(label="事件脉络", placeholder="...", lines=18, max_lines=18, interactive=False, visible=True)
yj_plan_btn = gr.Button("生成总结", variant="secondary", visible=False)
yj_plan = gr.Textbox(label="总结报告", placeholder="...", lines=15, max_lines=15, interactive=False, visible=False)
yj_download_context = gr.File(label="下载文件", file_count="single", type="file", file_types=['.md'], interactive=True, visible=False)
yj_query.change(
chg_btn_color_if_input,
[yj_query],
[yj_start_btn]
)
yj_start_btn.click(
yj_read_logs,
[],
[yj_log, yj_context, yj_plan],
every=1
)
yj_start_btn.click(
yj_sort_out_context,
[yj_query],
[yj_plan_btn]
)
yj_clean_btn.click(
yj_clean_all,
[],
[yj_query, yj_start_btn, yj_plan_btn, yj_log, yj_context, yj_plan]
)
yj_plan_btn.click(
yj_write_plan,
[yj_query, yj_context],
[]
)
yj_context.change(
show_YJ_context,
inputs=[yj_context],
outputs=[yj_download_context]
)
with gr.Tab(label = "报文问答"):
with gr.Row():
rag_upload = gr.File(label="上传报文", file_count="multiple", type="file", interactive=True, visible=True)
rag_tmp_files = gr.Textbox(label="上传日志", placeholder="...", lines=9, max_lines=9, interactive=False, visible=True)
rag_query = gr.Textbox(label="提问", placeholder="...", lines=10, max_lines=10, interactive=True, visible=True)
rag_radio = gr.Radio(
# ["快速", "多智", "深思"],
# label="快速(<半分钟), 多智(~2分钟), 深思(~4分钟)",
["快速", "多智"],
label="快速(<半分钟), 多智(~2分钟)",
info="",
type="value",
value="快速",
)
rag_start_btn = gr.Button("开始", variant="secondary", visible=True)
rag_clean_btn = gr.Button("清空", variant="secondary", visible=True)
with gr.Row():
rag_ans1 = gr.Textbox(label="回答 (快速)", placeholder="...", lines=15, max_lines=15, interactive=False, visible=True)
rag_ans2 = gr.Textbox(label="回答 (深思)", placeholder="...", lines=15, max_lines=15, interactive=False, visible=False)
rag_ans3 = gr.Textbox(label="回答 (多智)", placeholder="...", lines=15, max_lines=15, interactive=False, visible=False)
with gr.Row():
rag_log1 = gr.Textbox(label="日志 (快速)", placeholder="...", lines=15, max_lines=15, interactive=False, visible=True)
rag_log2 = gr.Textbox(label="日志 (深思)", placeholder="...", lines=15, max_lines=15, interactive=False, visible=False)
rag_log3 = gr.Textbox(label="日志 (多智)", placeholder="...", lines=15, max_lines=15, interactive=False, visible=False)
rag_upload.upload(
rag_handle_upload,
[rag_upload],
[rag_tmp_files]
)
rag_radio.change(
chg_textbox_visible,
[rag_radio],
[rag_ans1, rag_ans2, rag_log1, rag_log2, rag_ans3, rag_log3]
)
rag_query.change(
chg_btn_color_if_input,
[rag_query],
[rag_start_btn]
)
rag_start_btn.click(
rag_read_logs,
[],
[rag_ans1, rag_log1, rag_ans2, rag_log2, rag_ans3, rag_log3],
every=1
)
rag_start_btn.click(
rag_helper,
[rag_query, rag_radio],
[]
)
rag_clean_btn.click(
rag_clean_all,
[],
[rag_query, rag_start_btn]
)
with gr.Tab(label = "命令解析"):
ml_query = gr.Textbox(label="命令", placeholder="...", lines=10, max_lines=10, interactive=True, visible=True)
ml_start_btn = gr.Button("开始", variant="secondary", visible=True)
ml_clean_btn = gr.Button("清空", variant="secondary", visible=True)
ml_ans = gr.Textbox(label="解析", placeholder="...", lines=15, max_lines=15, interactive=False, visible=True)
ml_log = gr.Textbox(label="日志", placeholder="...", lines=15, max_lines=15, interactive=False, visible=True)
ml_query.change(
chg_btn_color_if_input,
[ml_query],
[ml_start_btn]
)
ml_start_btn.click(
ml_read_logs,
[],
[ml_ans, ml_log],
every=1
)
ml_start_btn.click(
ml_helper,
[ml_query],
[]
)
ml_clean_btn.click(
ml_clean_all,
[],
[ml_query, ml_start_btn]
)
with gr.Tab(label = "标签提取"):
with gr.Row():
upload_box = gr.File(label="上传单个TXT", file_count="single", type="file", file_types=['.txt'], interactive=True)
input_content = gr.Textbox(label="TXT文件内容", placeholder="...", lines=9, max_lines=9, interactive=False)
start_btn = gr.Button("开始分析", variant="secondary")
output_JQ = gr.Textbox(label="分析结果", placeholder="...", lines=10, interactive=False)
with gr.Row():
# output_log = gr.Textbox(label="日志", placeholder="日志", lines=12, interactive=False)
download_JQ = gr.File(label="下载分析", file_count="single", type="file", file_types=['.txt'], interactive=True, visible=False)
upload_box.change(
read_file,
inputs=[upload_box],
outputs=[input_content]
)
upload_box.change(
chg_btn_color_if_file,
inputs=[upload_box],
outputs=[start_btn]
)
start_btn.click(
run_llm_JQ,
inputs=[upload_box],
outputs=[output_JQ]
)
output_JQ.change(
show_JQ_file,
inputs=[output_JQ],
outputs=[download_JQ]
)
# with gr.Tab(label = "聊天"):
# gr.ChatInterface(
# fn=chat_predict_openai,
# textbox=chat_ask,
# submit_btn="提交",
# stop_btn="停止",
# retry_btn="🔄 重试",
# undo_btn="↩️ 撤消",
# clear_btn="🗑️ 清除",
# )
# 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__":
import sys
if len(sys.argv) > 1:
_port = int(sys.argv[1])
else:
_port = 7788
while True:
try:
demo.queue(concurrency_count=1).launch(
server_name="0.0.0.0",
server_port=_port,
share=False,
favicon_path="./asset/favicon_paper.png",
ssl_verify=False,
)
except Exception as e:
logger_rag.error(f"{e}")
continue
# 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
# )