Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,30 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|