ClueAI commited on
Commit
77b64d3
1 Parent(s): bc3347c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -27
app.py CHANGED
@@ -61,35 +61,35 @@ with block as demo:
61
  clear_history.click(fn=clear_session , inputs=[], outputs=[chatbot, state], queue=False)
62
 
63
 
64
- # def ChatYuan(api_key, text_prompt):
65
-
66
- # cl = clueai.Client(api_key,
67
- # check_api_key=True)
68
- # # generate a prediction for a prompt
69
- # # 需要返回得分的话,指定return_likelihoods="GENERATION"
70
- # prediction = cl.generate(model_name='ChatYuan-large', prompt=text_prompt)
71
- # # print the predicted text
72
- # print('prediction: {}'.format(prediction.generations[0].text))
73
- # response = prediction.generations[0].text
74
- # if response == '':
75
- # response = "很抱歉,我无法回答这个问题"
76
-
77
- # return response
78
 
79
- # def chatyuan_bot_api(api_key, input, history):
80
- # history = history or []
81
 
82
- # if len(history) > 5:
83
- # history = history[-5:]
84
 
85
- # context = "\n".join([f"用户:{input_text}\n小元:{answer_text}" for input_text, answer_text in history])
86
- # print(context)
87
 
88
- # input_text = context + "\n用户:" + input + "\n小元:"
89
- # output_text = ChatYuan(api_key, input_text)
90
- # history.append((input, output_text))
91
- # print(history)
92
- # return history, history
93
 
94
  block = gr.Blocks()
95
 
@@ -101,13 +101,13 @@ with block as demo_1:
101
  chatbot = gr.Chatbot(label='ChatYuan')
102
  message = gr.Textbox()
103
  state = gr.State()
104
- message.submit(chatyuan_bot, inputs=[message, state], outputs=[chatbot, state])
105
  with gr.Row():
106
  clear_history = gr.Button("👋 清除历史对话")
107
  clear = gr.Button('🧹 清除发送框')
108
  send = gr.Button("🚀 发送")
109
 
110
- send.click(chatyuan_bot, inputs=[message, state], outputs=[chatbot, state])
111
  clear.click(lambda: None, None, message, queue=False)
112
  clear_history.click(fn=clear_session , inputs=[], outputs=[chatbot, state], queue=False)
113
 
 
61
  clear_history.click(fn=clear_session , inputs=[], outputs=[chatbot, state], queue=False)
62
 
63
 
64
+ def ChatYuan(api_key, text_prompt):
65
+
66
+ cl = clueai.Client(api_key,
67
+ check_api_key=True)
68
+ # generate a prediction for a prompt
69
+ # 需要返回得分的话,指定return_likelihoods="GENERATION"
70
+ prediction = cl.generate(model_name='ChatYuan-large', prompt=text_prompt)
71
+ # print the predicted text
72
+ print('prediction: {}'.format(prediction.generations[0].text))
73
+ response = prediction.generations[0].text
74
+ if response == '':
75
+ response = "很抱歉,我无法回答这个问题"
76
+
77
+ return response
78
 
79
+ def chatyuan_bot_api(api_key, input, history):
80
+ history = history or []
81
 
82
+ if len(history) > 5:
83
+ history = history[-5:]
84
 
85
+ context = "\n".join([f"用户:{input_text}\n小元:{answer_text}" for input_text, answer_text in history])
86
+ print(context)
87
 
88
+ input_text = context + "\n用户:" + input + "\n小元:"
89
+ output_text = ChatYuan(api_key, input_text)
90
+ history.append((input, output_text))
91
+ print(history)
92
+ return history, history
93
 
94
  block = gr.Blocks()
95
 
 
101
  chatbot = gr.Chatbot(label='ChatYuan')
102
  message = gr.Textbox()
103
  state = gr.State()
104
+ message.submit(chatyuan_bot_api, inputs=[message, state], outputs=[chatbot, state])
105
  with gr.Row():
106
  clear_history = gr.Button("👋 清除历史对话")
107
  clear = gr.Button('🧹 清除发送框')
108
  send = gr.Button("🚀 发送")
109
 
110
+ send.click(chatyuan_bot_api, inputs=[message, state], outputs=[chatbot, state])
111
  clear.click(lambda: None, None, message, queue=False)
112
  clear_history.click(fn=clear_session , inputs=[], outputs=[chatbot, state], queue=False)
113