zxw commited on
Commit
9d58ac2
1 Parent(s): 0cff36c
Files changed (3) hide show
  1. README.md +7 -6
  2. app.py +124 -0
  3. requirements.txt +4 -0
README.md CHANGED
@@ -1,13 +1,14 @@
1
  ---
2
- title: ChatYuan 7B
3
- emoji: 🔥
4
- colorFrom: pink
5
- colorTo: purple
6
  sdk: gradio
7
- sdk_version: 3.33.1
8
  app_file: app.py
9
  pinned: false
10
- license: gpl-3.0
 
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: ChatYuan Large V2
3
+ emoji: 📊
4
+ colorFrom: red
5
+ colorTo: pink
6
  sdk: gradio
7
+ sdk_version: 3.23.0
8
  app_file: app.py
9
  pinned: false
10
+ license: creativeml-openrail-m
11
+ duplicated_from: ClueAI/ChatYuan-large-v2
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import clueai
4
+ import torch
5
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
6
+
7
+
8
+ base_info = ""
9
+
10
+
11
+
12
+
13
+ def clear_session():
14
+ return '', None
15
+
16
+
17
+ def ChatYuan(api_key, text_prompt, top_p):
18
+ cl = clueai.Client(api_key, check_api_key=True)
19
+ # generate a prediction for a prompt
20
+ # 需要返回得分的话,指定return_likelihoods="GENERATION"
21
+ prediction = cl.generate(model_name='ChatYuan-7B', prompt=text_prompt)
22
+ # print the predicted text
23
+ #print('prediction: {}'.format(prediction.generations[0].text))
24
+ response = prediction.generations[0].text
25
+ if response == '':
26
+ response = "很抱歉,我无法回答这个问题"
27
+
28
+ return response
29
+
30
+
31
+ def chatyuan_bot_api(api_key, input, history, top_p, num):
32
+ if not api_key:
33
+ return "请填写api key再尝试输入", [], []
34
+ history = history or []
35
+
36
+ if len(history) > num:
37
+ history = history[-num:]
38
+
39
+ #print(f"history{history}")
40
+ history_context = [f"用户:{input_text}\n小元:{answer_text}" for input_text, answer_text in history]
41
+ context = "\n".join(history_context)
42
+ #print(f"context:{context}")
43
+
44
+ while len(context) > 768:
45
+ history_context = history_context[1:]
46
+ context = "\n".join(history_context)
47
+
48
+ input_text = context + "\n用户:" + input + "\n小元:"
49
+ input_text = input_text.strip()
50
+ output_text = ChatYuan(api_key, input_text, top_p)
51
+ print("api".center(20, "="))
52
+ print(f"api_key:{api_key}\n{input_text}\n{output_text}")
53
+
54
+ history.append((input, output_text))
55
+
56
+ return '', history, history
57
+
58
+
59
+ block = gr.Blocks()
60
+
61
+ with block as demo_1:
62
+ gr.Markdown("""<h1><center>元语智能——ChatYuan</center></h1>
63
+ <font size=4>回答来自ChatYuan, 以上是模型生成的结果, 请谨慎辨别和参考, 不代表任何人观点 | Answer generated by ChatYuan model</font>
64
+ <font size=4>注意:gradio对markdown代码格式展示有限</font>
65
+ <font size=4>在使用此功能前,你需要有个API key. API key 可以通过这个<a href='https://www.clueai.cn/' target="_blank">平台</a>获取</font>
66
+ """)
67
+ with gr.Row():
68
+ with gr.Column(scale=3):
69
+ chatbot = gr.Chatbot(label='ChatYuan').style(height=400)
70
+
71
+ with gr.Column(scale=1):
72
+ api_key = gr.inputs.Textbox(label="请输入你的api-key(必填)",
73
+ default="",
74
+ type='password')
75
+ num = gr.Slider(minimum=4,
76
+ maximum=10,
77
+ label="最大的对话轮数",
78
+ value=5,
79
+ step=1)
80
+ top_p = gr.Slider(minimum=0,
81
+ maximum=1,
82
+ label="top_p",
83
+ value=0.7,
84
+ step=0.1)
85
+ clear_history = gr.Button("👋 清除历史对话 | Clear History")
86
+ send = gr.Button("🚀 发送 | Send")
87
+
88
+ message = gr.Textbox()
89
+ state = gr.State()
90
+ message.submit(chatyuan_bot_api,
91
+ inputs=[api_key, message, state, top_p, num],
92
+ outputs=[message, chatbot, state])
93
+
94
+ send.click(chatyuan_bot_api,
95
+ inputs=[api_key, message, state, top_p, num],
96
+ outputs=[message, chatbot, state])
97
+ clear_history.click(fn=clear_session,
98
+ inputs=[],
99
+ outputs=[chatbot, state],
100
+ queue=False)
101
+
102
+ block = gr.Blocks()
103
+ with block as introduction:
104
+ gr.Markdown("""<h1><center>元语智能——ChatYuan</center></h1>
105
+
106
+ <font size=4>😉ChatYuan: 元语功能型对话大模型 | General Model for Dialogue with ChatYuan
107
+ <br>
108
+ 👏ChatYuan-7B是一个支持中英双语的功能型对话语言大模型。
109
+ <br>
110
+ ChatYuan-7B is an open-source large language model for dialogue, supports both Chinese and English languages, and in ChatGPT style.
111
+ <br>
112
+ ChatYuan-7B是ChatYuan系列中以轻量化实现高质量效果的模型之一。
113
+ <br>
114
+ <br>
115
+ <br>
116
+ < br>
117
+ </font>
118
+ <center><a href="https://clustrmaps.com/site/1bts0" title="Visit tracker"><img src="//www.clustrmaps.com/map_v2.png?d=ycVCe17noTYFDs30w7AmkFaE-TwabMBukDP1802_Lts&cl=ffffff" /></a></center>
119
+ """)
120
+
121
+ gui = gr.TabbedInterface(
122
+ interface_list=[introduction, demo_1],
123
+ tab_names=["相关介绍 | Introduction", "API调用"])
124
+ gui.launch(quiet=True, show_api=False, share=False)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ transformers
2
+ torch
3
+ SentencePiece
4
+ clueai