|
- import gradio as gr
- from transformers import AutoModel, AutoTokenizer
- import mdtex2html
- from peft import PeftModel
-
- tokenizer = AutoTokenizer.from_pretrained("/dataset/chatglm-6b", trust_remote_code=True)
- model = AutoModel.from_pretrained("/dataset/chatglm-6b", trust_remote_code=True, device_map='auto')
- model = PeftModel.from_pretrained(model, "output").half()
- model = model.eval()
-
- def get_answer(query, chatbot, history):
- chatbot.append((query, ""))
-
- for resp, history in model.stream_chat(tokenizer, query, history):
- chatbot[-1] = (query, resp)
-
- yield chatbot, "", history
-
- def generate_record(input):
-
- for resp, history in model.stream_chat(tokenizer, input):
-
- yield resp
-
-
- def reset_state():
- return [], []
-
-
- block_css = """.importantButton {
- background: linear-gradient(45deg, #7e0570,#5d1c99, #6e00ff) !important;
- border: none !important;
- }
-
- .importantButton:hover {
- background: linear-gradient(45deg, #ff00e0,#8500ff, #6e00ff) !important;
- border: none !important;
- }"""
-
-
- with gr.Blocks(css=block_css) as demo:
- gr.HTML("""<h1 align="center">肝癌辅助问诊和问答系统</h1>""")
- history = gr.State([])
-
- with gr.Tab("肝癌知识问答"):
- with gr.Column(scale=3):
- chatbot = gr.Chatbot().style(height=400)
- with gr.Row():
- with gr.Column(scale=18):
- query = gr.Textbox(show_label=False, placeholder="请输入提问内容", lines=2).style(container=False)
- with gr.Column(scale=1):
- send_masg = gr.Button("发送")
- with gr.Column(scale=1):
- clear_nr = gr.Button("开始新的对话")
-
- clear_nr.click(reset_state, outputs=[chatbot, history], show_progress=True)
- send_masg.click(get_answer, [query, chatbot, history], [chatbot, query, history])
-
- with gr.Tab("辅助诊断"):
- with gr.Row():
- with gr.Column(scale=3):
- gr.Markdown("""<h1 align="left"><font size=3>影像所见:</h1></font>""")
- input_txt = gr.Textbox(show_label=False, placeholder="请输入", lines=20).style(container=False)
-
- with gr.Column(scale=2):
- gr.Markdown("""<h1 align="left"><font size=3>影像诊断:</h1></font>""")
- Diag_record = gr.Markdown()
-
- with gr.Row():
- with gr.Column(scale=1):
- next_input = gr.Button("下一个")
- with gr.Column(scale=3):
- Diagnosis = gr.Button("诊断", variant="primary")
-
- next_input.click(reset_state, outputs=[input_txt, Diag_record], show_progress=True)
- Diagnosis.click(generate_record, history, Diag_record)
-
- demo.queue().launch(share=True, inbrowser=True)
|