-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
157 lines (123 loc) · 6.37 KB
/
Main.py
File metadata and controls
157 lines (123 loc) · 6.37 KB
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
# -*- coding: utf-8 -*-
import gradio as gr
from utils import Dictionary, GitUtil
try:
import Config_private as Config
except ImportError:
import Config
now_index = -1
now_tab = ''
with gr.Blocks(title='专有名词翻译汇总', analytics_enabled=True) as demo:
gr.Markdown('# 红石科技搬运组专有名词翻译汇总')
gr.Markdown('欢迎使用红石科技专有名词词库在线维护系统!')
with gr.Tab('新词条') as new_tab:
gr.Markdown('## 添加新词条')
gr.Markdown('此界面用于新增词条')
with gr.Row():
with gr.Column(scale=1, min_width=600):
with gr.Column(variant='panel'):
text_word = gr.Textbox(label="原词/词组")
text_trans = gr.Textbox(label="翻译")
text_tag = gr.Dropdown(label="Tag", choices=Dictionary.get_tag(), max_choices=5,
multiselect=True)
with gr.Accordion(label='添加Tag', open=False):
text_new_tag = gr.Textbox(label='Tag')
btn_tag = gr.Button('添加')
label_tag = gr.Markdown(value='')
with gr.Column(scale=2, min_width=600, variant='panel'):
text_desc = gr.TextArea(label="描述", lines=7)
text_example = gr.TextArea(label="示例", lines=5)
btn_add = gr.Button('添加')
label_word = gr.Markdown('')
def add_word(word, trans, tag, desc, example):
fi, index = Dictionary.add_word(word, trans, desc, example, tag)
if index == -1:
return '已存在该词条,请前往修改界面进行修改'
return f'添加了{fi}-{index}号词条'
def add_tag(tag):
result, tags = Dictionary.add_tag(tag)
print(tags)
# TODO 说是后续4.0版本会修复不更新的问题
if result == 1:
demo.update()
return 'success!', gr.Dropdown(label="Tag", choices=tags, max_choices=5, multiselect=True)
else:
return '已存在', gr.Dropdown(label="Tag", choices=tags, max_choices=5, multiselect=True)
btn_add.click(add_word, inputs=[text_word, text_trans, text_tag, text_desc, text_example], outputs=label_word)
btn_tag.click(add_tag, inputs=text_new_tag, outputs=[label_tag, text_tag])
with gr.Tab('删改词条') as modify_tab:
gr.Markdown('# 删改词条')
with gr.Row():
with gr.Column(scale=1):
search_input = gr.Textbox(label='词条')
with gr.Row():
clean_btn = gr.Button(value='clean')
search_btn = gr.Button(value='search', variant='primary')
submit_btn = gr.Button(value='submit', variant='primary')
del_btn = gr.Button(value='delete', variant='stop')
label_search = gr.Markdown('')
with gr.Column(scale=3):
with gr.Group():
with gr.Row():
text_modify_trans = gr.Textbox(label='翻译')
text_modify_tag = gr.Dropdown(label='Tag', choices=Dictionary.get_tag(), multiselect=True,
max_choices=5)
text_modify_description = gr.Textbox(label='描述', lines=7)
text_modify_example = gr.Textbox(label='示例', lines=5)
def clean_input():
global now_index
now_index = -1
return '', '', '', '', []
def search_word(word):
global now_index
index, result = Dictionary.search_word(word)
if index == -1:
return '该词条不存在', '', '', '', []
else:
now_index = index
return 'success', result['Translation'], result['Description'], result['Example'], result[
'Tag'].split('|')
def submit_modify(word, trans, desc, example, tag):
Dictionary.modify_word(now_index, word, trans, desc, example, tag)
return f'修改了{now_index}号词条'
def del_word(word, tag):
global now_index
index = now_index
now_index = -1
Dictionary.del_word(index, word, tag)
return f'删除了{word[0].upper()}-{index}号词条', '', '', '', []
clean_btn.click(clean_input,
outputs=[search_input, text_modify_trans, text_modify_description, text_modify_example,
text_modify_tag])
search_btn.click(search_word, inputs=search_input,
outputs=[label_search, text_modify_trans, text_modify_description, text_modify_example,
text_modify_tag])
submit_btn.click(submit_modify,
inputs=[search_input, text_modify_trans, text_modify_description, text_modify_example,
text_modify_tag], outputs=label_search)
del_btn.click(del_word, inputs=[search_input, text_modify_tag],
outputs=[label_search, text_modify_trans, text_modify_description, text_modify_example,
text_modify_tag])
with gr.Accordion(label='github 推送', open=True):
with gr.Row():
with gr.Column(scale=5):
text_commit = gr.Textbox(label='commit', lines=5)
with gr.Column(scale=1):
commit_btn = gr.Button(value='commit')
push_btn = gr.Button(value='push', variant='primary')
undo_btn = gr.Button(value='undo')
label_git = gr.Markdown('')
def commit(msg):
GitUtil.git_pull()
GitUtil.git_add()
GitUtil.git_commit(msg)
return 'done'
def undo():
result = GitUtil.git_checkout()
tags = Dictionary.get_tag()
return result, gr.Dropdown(label="Tag", choices=tags, max_choices=5, multiselect=True)
commit_btn.click(commit, inputs=text_commit, outputs=label_git)
push_btn.click(GitUtil.git_push, outputs=label_git)
undo_btn.click(undo, outputs=[label_git, text_tag])
if __name__ == '__main__':
demo.launch(auth=Config.AUTH, favicon_path='res/logo.ico', server_name='0.0.0.0', server_port=Config.PORT)