Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
#
|
69 |
-
#
|
70 |
-
|
71 |
-
#
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
|
79 |
-
|
80 |
-
|
81 |
|
82 |
-
|
83 |
-
|
84 |
|
85 |
-
|
86 |
-
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
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(
|
105 |
with gr.Row():
|
106 |
clear_history = gr.Button("👋 清除历史对话")
|
107 |
clear = gr.Button('🧹 清除发送框')
|
108 |
send = gr.Button("🚀 发送")
|
109 |
|
110 |
-
send.click(
|
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 |
|