lujin commited on
Commit
cdfd8d2
·
verified ·
1 Parent(s): 8cceae0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -13
app.py CHANGED
@@ -1,18 +1,30 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
3
 
4
- # 加载对话模型
5
- chatbot = pipeline("text-generation", model="zai-org/GLM-5.1")
6
 
7
- # 定义聊天函数
8
- def chat_function(input_text):
9
- conversation = chatbot(input_text)
10
- return conversation[-1]["generated_text"]
11
 
12
- # 使用 Gradio 创建聊天 API
13
- def create_chat_api():
14
- with gr.Interface(fn=chat_function, inputs="text", outputs="text", title="Chatbot API") as demo:
15
- demo.launch()
 
 
16
 
17
- # 启动服务
18
- create_chat_api()
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+ import os
4
 
5
+ # 从环境变量读取 token
6
+ HF_TOKEN = os.environ.get("HF_TOKEN")
7
 
8
+ client = InferenceClient(
9
+ provider="zai-org",
10
+ api_key=HF_TOKEN,
11
+ )
12
 
13
+ def chat_function(message, history):
14
+ messages = []
15
+ for human, assistant in history:
16
+ messages.append({"role": "user", "content": human})
17
+ messages.append({"role": "assistant", "content": assistant})
18
+ messages.append({"role": "user", "content": message})
19
 
20
+ response = client.chat.completions.create(
21
+ model="zai-org/GLM-5.1",
22
+ messages=messages,
23
+ max_tokens=512,
24
+ )
25
+ return response.choices[0].message.content
26
+
27
+ with gr.Blocks() as demo:
28
+ gr.ChatInterface(fn=chat_function)
29
+
30
+ demo.launch(server_name="0.0.0.0", server_port=7860)