File size: 1,813 Bytes
a09c22e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os

import gradio as gr



os.system(
    'pip install --upgrade torch'
)
os.system(
    'pip install "modelscope" --upgrade -f https://pypi.org/project/modelscope/'
)

os.system('pip install transformers -U')

from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks

pipe = pipeline(task=Tasks.chat, model='ZhipuAI/chatglm2-6b', model_revision='v1.0.2',device_map='auto')


def clear_session():
    return '', None


def predict(input, history=None):
    dictionary  = {
        'prmpt': input
    }
    print(dictionary)
    if history is None:
        history = []
    inputs = inputs = {'text': input, 'history': history}
    result = pipe(inputs)
    history = result['history']

    return '', history, history


block = gr.Blocks()

with block as demo:
    gr.Markdown("""<h1><center>ChatGLM-6B unofficial demo</center></h1>
     <center><font size=3><a href='https://modelscope.cn/models/ZhipuAI/ChatGLM-6B/summary' target="_blank">ChatGLM-6B </a>是一个开源的、支持中英双语的对话语言模型,基于 General Language Model (GLM) 架构,具有 62 亿参数。</center></font>
    """)
    chatbot = gr.Chatbot(label='ChatGLM-6B')
    message = gr.Textbox()
    state = gr.State()
    message.submit(predict,
                   inputs=[message, state],
                   outputs=[message, chatbot, state])
    with gr.Row():
        clear_history = gr.Button("🧹 清除历史对话")
        send = gr.Button("🚀 发送")

    send.click(predict,
               inputs=[message, state],
               outputs=[message, chatbot, state])
    clear_history.click(fn=clear_session,
                        inputs=[],
                        outputs=[chatbot, state],
                        queue=False)

demo.queue().launch(height=800, share=False)