curry tang
commited on
Commit
·
ca737e5
1
Parent(s):
4797738
update
Browse files
app.py
CHANGED
@@ -109,6 +109,50 @@ def function_gen(_code_type: str, _code: str, _chat):
|
|
109 |
yield response_message
|
110 |
|
111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
with gr.Blocks() as app:
|
113 |
with gr.Tab('聊天'):
|
114 |
chat_engine = gr.State(value=None)
|
@@ -121,7 +165,7 @@ with gr.Blocks() as app:
|
|
121 |
textbox=gr.MultimodalTextbox(lines=1),
|
122 |
additional_inputs=[chat_engine]
|
123 |
)
|
124 |
-
with gr.Column(scale=1, min_width=300):
|
125 |
with gr.Accordion('模型参数设置', open=True):
|
126 |
with gr.Column():
|
127 |
provider = gr.Dropdown(
|
@@ -179,25 +223,53 @@ with gr.Blocks() as app:
|
|
179 |
with gr.Tab('代码优化'):
|
180 |
chat_engine = gr.State(value=None)
|
181 |
with gr.Row():
|
182 |
-
with gr.Column(scale=
|
183 |
with gr.Row(variant="panel"):
|
184 |
code_result = gr.Markdown(label='解释结果', value=None)
|
185 |
with gr.Column(scale=1):
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
|
|
197 |
explain_code_btn.click(fn=explain_code, inputs=[code_type, code, chat_engine], outputs=[code_result])
|
198 |
optimize_code_btn.click(fn=optimize_code, inputs=[code_type, code, chat_engine], outputs=[code_result])
|
199 |
debug_code_btn.click(fn=debug_code, inputs=[code_type, code, chat_engine], outputs=[code_result])
|
200 |
function_gen_btn.click(fn=function_gen, inputs=[code_type, code, chat_engine], outputs=[code_result])
|
201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
|
203 |
app.launch(debug=settings.debug, show_api=False)
|
|
|
109 |
yield response_message
|
110 |
|
111 |
|
112 |
+
def translate_doc(_language_input, _language_output, _doc, _chat):
|
113 |
+
prompt = f'''
|
114 |
+
你是一位精通{_language_output}的专业翻译,尤其擅长将专业学术论文翻译成浅显易懂的科普文章。我希望你能帮我将以下{_language_input}论文段落翻译成{_language_output},风格与科普杂志的{_language_output}版相似。
|
115 |
+
|
116 |
+
规则:
|
117 |
+
1. 翻译时要准确传达原文的事实和背景。
|
118 |
+
2. 即使上意译也要保留原始段落格式,以及保留术语,例如 FLAC,JPEG 等。保留公司缩写,例如 Microsoft, Amazon 等。
|
119 |
+
3. 同时要保留引用的论文,例如 [20] 这样的引用。
|
120 |
+
4. 对于 Figure 和 Table,翻译的同时保留原有格式,例如:“Figure 1:” 翻译为 “图 1: ”,“Table 1: ” 翻译为:“表 1: ”。
|
121 |
+
5. 全角括号换成半角括号,并在左括号前面加半角空格,右括号后面加半角空格。
|
122 |
+
6. 输入格式为 Markdown 格式,输出格式也必须保留原始 Markdown 格式
|
123 |
+
7. 以下是常见的 AI 相关术语词汇对应表:
|
124 |
+
Transformer -> Transformer
|
125 |
+
LLM/Large Language Model -> 大语言模型
|
126 |
+
Generative AI -> 生成式 AI
|
127 |
+
|
128 |
+
策略:
|
129 |
+
分成两次翻译,并且打印每一次结果:
|
130 |
+
1. 第一次,根据{_language_input}内容直译为{_language_output},保持原有格式,不要遗漏任何信息,并且打印直译结果
|
131 |
+
2. 第二次,根据第一次直译的结果重新意译,遵守原意的前提下让内容更通俗易懂、符合{_language_output}表达习惯,但要保留原有格式不变
|
132 |
+
|
133 |
+
返回格式如下,"<doc>xxx</doc>" 表示占位符:
|
134 |
+
**直译**:
|
135 |
+
|
136 |
+
<doc>直译结果</doc>
|
137 |
+
|
138 |
+
**意译**:
|
139 |
+
|
140 |
+
<doc>意译结果</doc>
|
141 |
+
'''
|
142 |
+
|
143 |
+
if _chat is None:
|
144 |
+
_chat = init_chat()
|
145 |
+
print('????', prompt)
|
146 |
+
chat_messages = [
|
147 |
+
SystemMessage(content=prompt),
|
148 |
+
HumanMessage(content=_doc),
|
149 |
+
]
|
150 |
+
response_message = ''
|
151 |
+
for chunk in _chat.stream(chat_messages):
|
152 |
+
response_message = response_message + chunk.content
|
153 |
+
yield response_message
|
154 |
+
|
155 |
+
|
156 |
with gr.Blocks() as app:
|
157 |
with gr.Tab('聊天'):
|
158 |
chat_engine = gr.State(value=None)
|
|
|
165 |
textbox=gr.MultimodalTextbox(lines=1),
|
166 |
additional_inputs=[chat_engine]
|
167 |
)
|
168 |
+
with gr.Column(scale=1, min_width=300) as model_panel:
|
169 |
with gr.Accordion('模型参数设置', open=True):
|
170 |
with gr.Column():
|
171 |
provider = gr.Dropdown(
|
|
|
223 |
with gr.Tab('代码优化'):
|
224 |
chat_engine = gr.State(value=None)
|
225 |
with gr.Row():
|
226 |
+
with gr.Column(scale=2):
|
227 |
with gr.Row(variant="panel"):
|
228 |
code_result = gr.Markdown(label='解释结果', value=None)
|
229 |
with gr.Column(scale=1):
|
230 |
+
with gr.Accordion('代码助手', open=True):
|
231 |
+
code_type = gr.Dropdown(
|
232 |
+
label='代码类型',
|
233 |
+
choices=['Javascript', 'Typescript', 'Python', 'C++', 'PHP', 'Java'],
|
234 |
+
value='Javascript',
|
235 |
+
)
|
236 |
+
code = gr.Textbox(label='代码', lines=10, value=None)
|
237 |
+
with gr.Row(variant='panel'):
|
238 |
+
explain_code_btn = gr.Button('解释代码')
|
239 |
+
optimize_code_btn = gr.Button('优化代码')
|
240 |
+
debug_code_btn = gr.Button('错误修复')
|
241 |
+
function_gen_btn = gr.Button('函数生成')
|
242 |
explain_code_btn.click(fn=explain_code, inputs=[code_type, code, chat_engine], outputs=[code_result])
|
243 |
optimize_code_btn.click(fn=optimize_code, inputs=[code_type, code, chat_engine], outputs=[code_result])
|
244 |
debug_code_btn.click(fn=debug_code, inputs=[code_type, code, chat_engine], outputs=[code_result])
|
245 |
function_gen_btn.click(fn=function_gen, inputs=[code_type, code, chat_engine], outputs=[code_result])
|
246 |
|
247 |
+
with gr.Tab('日常事务助手'):
|
248 |
+
chat_engine = gr.State(value=None)
|
249 |
+
with gr.Row():
|
250 |
+
with gr.Column(scale=2):
|
251 |
+
with gr.Row(variant="panel"):
|
252 |
+
code_result = gr.Markdown(label='解释结果', value=None)
|
253 |
+
with gr.Column(scale=1):
|
254 |
+
with gr.Accordion('文档助手', open=True):
|
255 |
+
with gr.Row():
|
256 |
+
language_input = gr.Dropdown(
|
257 |
+
label='输入语言',
|
258 |
+
choices=['英语', '简体中文', '日语'],
|
259 |
+
value='英语',
|
260 |
+
)
|
261 |
+
language_output = gr.Dropdown(
|
262 |
+
label='输出语言',
|
263 |
+
choices=['英语', '简体中文', '日语'],
|
264 |
+
value='简体中文',
|
265 |
+
)
|
266 |
+
doc = gr.Textbox(label='文本', lines=10, value=None)
|
267 |
+
with gr.Row(variant='panel'):
|
268 |
+
translate_doc_btn = gr.Button('翻译文档')
|
269 |
+
summarize_doc_btn = gr.Button('摘要提取')
|
270 |
+
email_doc_btn = gr.Button('邮件撰写')
|
271 |
+
doc_gen_btn = gr.Button('文档润色')
|
272 |
+
translate_doc_btn.click(fn=translate_doc, inputs=[language_input, language_output, doc, chat_engine], outputs=[code_result])
|
273 |
+
|
274 |
|
275 |
app.launch(debug=settings.debug, show_api=False)
|