File size: 892 Bytes
34a24fb 1e7017f 4726aa5 86c6031 1e7017f 9efcd61 86c6031 9efcd61 86c6031 9efcd61 86c6031 9efcd61 86c6031 9efcd61 c4800ec | 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 | import gradio as gr
from huggingface_hub import InferenceClient
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
def respond(message, history):
messages = [{"role": "system", "content": "You are a friendly chatbot!"}]
# 添加历史消息
for user_msg, bot_msg in history:
messages.append({"role": "user", "content": user_msg})
messages.append({"role": "assistant", "content": bot_msg})
# 添加当前消息
messages.append({"role": "user", "content": message})
# 调用API
response = client.chat_completion(
messages,
max_tokens=100 # 限制响应为100个token
)
return response.choices[0].message.content.strip()
chatbot = gr.ChatInterface(
respond,
title="Zephyr-7b Chatbot",
description="A chatbot powered by Zephyr-7b-beta"
)
if __name__ == "__main__":
chatbot.launch() |