Skip to content

Commit

Permalink
fix darkmode; fix ci-chat-stop button; change button name and add war…
Browse files Browse the repository at this point in the history
…ning;
  • Loading branch information
tuhahaha authored and JianxinMa committed Sep 25, 2023
1 parent 6b3d814 commit 943e9a5
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 53 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ python run_server.py --model_server http://127.0.0.1:7905/v1
- /plug + content: Qwen enables plugin and select appropriate plugin to generate reply
- Chat: Interactive area. Qwen generates replies based on given reference materials. Selecting Code Interpreter will enable the code interpreter plugin

- Note: About PDF Documents
- When adding online PDF to Qwen's reading list, it may take a long time for Qwen's preprocessing due to network reasons. Please be patient. It is recommended to first download and then open it as a local PDF in the browser.
- First time processing online PDF requires downloading nltk_data, which may fail to install due to network issues. It is recommended to download it yourself and place it in the user's root directory

# Code structure

- qwen_agent
Expand Down
3 changes: 3 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ python run_server.py --model_server http://127.0.0.1:7905/v1
- /plug + 内容:Qwen启用plug-in,选择合适的插件生成回复
- Chat:交互区,Qwen根据给定的参考资料,生成回复;勾选Code Interpreter后启用代码解释器插件

- Note:关于PDF文档
- 将在线PDF加入Qwen阅读列表时,可能由于网络等原因导致Qwen预处理时间较长,请耐心等待。建议优先下载后当作本地PDF再浏览器打开。
- 首次处理在线PDF需要下载nltk_data,可能由于网络问题安装失败,建议可以自行下载后放置于用户根目录下

# 代码结构
- qwen_agent
Expand Down
28 changes: 18 additions & 10 deletions qwen_server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,17 @@ def chat_clear():


def chat_clear_last():
print(app_global_para['last_turn_msg_id'][::-1])
print(app_global_para['messages'])
for index in app_global_para['last_turn_msg_id'][::-1]:
del app_global_para['messages'][index]
app_global_para['last_turn_msg_id'] = []


def add_file(file):
if not app_global_para['use_ci_flag']:
gr.Warning('Upload failed, please check Code Interpreter first!')
return '', ''
output_filepath = config_browserqwen.code_interpreter_ws
if '/' in file.name:
fn = file.name.split('/')[-1]
Expand Down Expand Up @@ -516,39 +521,42 @@ def format_generate(edit, context):
avatar_images=(None, (os.path.join(
Path(__file__).resolve().parent, 'img/logo.png'))))
with gr.Row():
with gr.Column(scale=0.05, min_width=0):
chat_clr_bt = gr.Button('🧹') # NOQA
with gr.Column(scale=0.16, min_width=0):
plug_bt = gr.Checkbox(label='Code Interpreter')
with gr.Column(scale=0.02, min_width=0):
show_path_md = gr.HTML('')
# with gr.Column(scale=0.03, min_width=0):
# show_path_bt = gr.Button('✖️', visible=False) # NOQA
with gr.Column(scale=0.6):
with gr.Column(scale=0.65):
chat_txt = gr.Textbox(show_label=False, placeholder='Chat with Qwen...', container=False) # NOQA
# with gr.Column(scale=0.05, min_width=0):
# chat_smt_bt = gr.Button('⏎') # NOQA
with gr.Column(scale=0.05, min_width=0):
chat_smt_bt = gr.Button('⏎') # NOQA
chat_clr_bt = gr.Button('Clear') # NOQA

with gr.Column(scale=0.05, min_width=0):
chat_stop_bt = gr.Button('🚫') # NOQA
chat_stop_bt = gr.Button('Stop') # NOQA
with gr.Column(scale=0.05, min_width=0):
chat_re_bt = gr.Button('🔄') # NOQA
chat_re_bt = gr.Button('Again') # NOQA

with gr.Column(scale=0.05, min_width=0):
file_btn = gr.UploadButton('📁', file_types=['file']) # NOQA
file_btn = gr.UploadButton('Upload', file_types=['file']) # NOQA

hidden_file_path = gr.Textbox(visible=False)

txt_msg = chat_txt.submit(add_text, [chatbot, chat_txt], [chatbot, chat_txt], queue=False).then(bot, [chatbot, hidden_file_path], chatbot)
txt_msg.then(lambda: gr.update(interactive=True), None, [chat_txt], queue=False)
txt_msg_bt = chat_smt_bt.click(add_text, [chatbot, chat_txt], [chatbot, chat_txt], queue=False).then(bot, chatbot, chatbot)
txt_msg_bt.then(lambda: gr.update(interactive=True), None, [chat_txt], queue=False)

# txt_msg_bt = chat_smt_bt.click(add_text, [chatbot, chat_txt], [chatbot, chat_txt], queue=False).then(bot, chatbot, chatbot)
# txt_msg_bt.then(lambda: gr.update(interactive=True), None, [chat_txt], queue=False)
# (None, None, None, cancels=[txt_msg], queue=False).then
re_txt_msg = chat_re_bt.click(rm_text, [chatbot], [chatbot, chat_txt], queue=False).then(chat_clear_last, None, None).then(bot, [chatbot, hidden_file_path], chatbot)
re_txt_msg.then(lambda: gr.update(interactive=True), None, [chat_txt], queue=False)

file_msg = file_btn.upload(add_file, [file_btn], [show_path_md, hidden_file_path], queue=False)
chat_clr_bt.click(chat_clear, None, [chatbot, hidden_file_path, show_path_md], queue=False)
# re_bt.click(re_bot, chatbot, chatbot)
chat_stop_bt.click(None, None, None, cancels=[txt_msg, txt_msg_bt, re_txt_msg], queue=False)
chat_stop_bt.click(chat_clear_last, None, None, cancels=[txt_msg, re_txt_msg], queue=False)

# show_path_bt.click(lambda x:[None, gr.update(visible=False)], hidden_file_path, [show_path_md,show_path_bt])

Expand Down
82 changes: 42 additions & 40 deletions qwen_server/app_in_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,25 @@ def set_page_url():


def bot(history):
now_page = None
_ref = ''
if not os.path.exists(cache_file):
gr.Info("Please add this page to Qwen's Reading List first!")
yield history

now_page = None
for line in jsonlines.open(cache_file):
if line['url'] == page_url[-1]:
now_page = line
break

if not now_page:
gr.Info("This page has not yet been added to the Qwen's reading list!")

_ref_list = mem.get(history[-1][0], [now_page], llm=llm, stream=False, max_token=max_ref_token)
if _ref_list:
_ref = '\n'.join(json.dumps(x, ensure_ascii=False) for x in _ref_list)
else:
_ref = ''
for line in jsonlines.open(cache_file):
if line['url'] == page_url[-1]:
now_page = line

if not now_page:
gr.Info("This page has not yet been added to the Qwen's reading list!")
elif now_page['raw'] == '':
gr.Info('Qwen is analyzing, parsing PDF takes some time...')
else:
_ref_list = mem.get(history[-1][0], [now_page], llm=llm, stream=False, max_token=max_ref_token)
if _ref_list:
_ref = '\n'.join(json.dumps(x, ensure_ascii=False) for x in _ref_list)
else:
_ref = ''
# print(_ref)
agent = Simple(stream=True, llm=llm)
history[-1][1] = ''
Expand All @@ -103,16 +104,17 @@ def bot(history):
yield history

# save history
now_page['session'] = history
lines = []
for line in jsonlines.open(cache_file):
if line['url'] != page_url[-1]:
lines.append(line)
if now_page:
now_page['session'] = history
lines = []
for line in jsonlines.open(cache_file):
if line['url'] != page_url[-1]:
lines.append(line)

lines.append(now_page)
with jsonlines.open(cache_file, mode='w') as writer:
for new_line in lines:
writer.write(new_line)
lines.append(now_page)
with jsonlines.open(cache_file, mode='w') as writer:
for new_line in lines:
writer.write(new_line)


def load_history_session(history):
Expand All @@ -126,7 +128,7 @@ def load_history_session(history):
if not now_page:
gr.Info("Please add this page to Qwen's Reading List first!")
return []
if not now_page['raw']:
if now_page['raw'] == '':
gr.Info('Qwen is analyzing, parsing PDF takes some time...')
return []
return now_page['session']
Expand Down Expand Up @@ -156,34 +158,34 @@ def clear_session():
avatar_images=(None, (os.path.join(
Path(__file__).resolve().parent, 'img/logo.png'))))
with gr.Row():
with gr.Column(scale=0.06, min_width=0):
clr_bt = gr.Button('🧹')
with gr.Column(scale=0.74):
with gr.Column(scale=0.7):
txt = gr.Textbox(show_label=False,
placeholder='Chat with Qwen...',
container=False)
with gr.Column(scale=0.06, min_width=0):
smt_bt = gr.Button('⏎')
with gr.Column(scale=0.06, min_width=0):
stop_bt = gr.Button('🚫')
with gr.Column(scale=0.06, min_width=0):
re_bt = gr.Button('🔄')
# with gr.Column(scale=0.06, min_width=0):
# smt_bt = gr.Button('⏎')
with gr.Column(scale=0.1, min_width=0):
clr_bt = gr.Button('🧹', elem_classes='bt_small_font')
with gr.Column(scale=0.1, min_width=0):
stop_bt = gr.Button('🚫', elem_classes='bt_small_font')
with gr.Column(scale=0.1, min_width=0):
re_bt = gr.Button('🔁', elem_classes='bt_small_font')

txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt],
queue=False).then(bot, chatbot, chatbot)
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)

txt_msg_bt = smt_bt.click(add_text, [chatbot, txt], [chatbot, txt],
queue=False).then(bot, chatbot, chatbot)
txt_msg_bt.then(lambda: gr.update(interactive=True),
None, [txt],
queue=False)
# txt_msg_bt = smt_bt.click(add_text, [chatbot, txt], [chatbot, txt],
# queue=False).then(bot, chatbot, chatbot)
# txt_msg_bt.then(lambda: gr.update(interactive=True),
# None, [txt],
# queue=False)

clr_bt.click(clear_session, None, chatbot, queue=False)
re_txt_msg = re_bt.click(rm_text, [chatbot], [chatbot, txt], queue=False).then(bot, chatbot, chatbot)
re_txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)

stop_bt.click(None, None, None, cancels=[txt_msg, txt_msg_bt, re_txt_msg], queue=False)
stop_bt.click(None, None, None, cancels=[txt_msg, re_txt_msg], queue=False)

demo.load(set_page_url).then(load_history_session, chatbot, chatbot)

Expand Down
12 changes: 9 additions & 3 deletions qwen_server/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@
text-align:center}

.div_tmp {
background-color: white;
height: 190px;
border-radius: 5px
}

.div_rec {
background-color: white;
height: 300px;
border-radius: 5px
}

.bt_small_font{
font-size: 6px;
}

.bt_small{
width: 30px;
}


.md_tmp {
background-color: white;
height: calc(100dvh - 380px);
border-radius: 5px

Expand Down
2 changes: 2 additions & 0 deletions qwen_server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ def cache_data(data, cache_file):


def change_checkbox_state(text, cache_file):
if not os.path.exists(cache_file):
return {'result': 'no file'}
lines = []
for line in jsonlines.open(cache_file):
if line['url'] == text[3:]:
Expand Down

0 comments on commit 943e9a5

Please sign in to comment.