Add gr.Workflow app for GLM-5.3 chat via HF Inference Providers
Browse files- Bound fn node streams from zai-org/GLM-5.3:zai-org via the HF router
- Visitor OAuth token injected via gr.OAuthToken (billed to the user)
- Pre-wired canvas: Prompt + System Prompt -> GLM-5.3 Chat -> Response
- .gitignore +2 -0
- app.py +43 -0
- requirements.txt +3 -0
- workflow.json +1 -1
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.venv
|
| 2 |
+
__pycache__
|
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from huggingface_hub import get_token
|
| 5 |
+
from openai import OpenAI
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def _client(oauth_token: Optional[gr.OAuthToken]) -> OpenAI:
|
| 9 |
+
# gr.Workflow injects the visitor's OAuth token into bound functions,
|
| 10 |
+
# so inference is billed to the user running the workflow. Fall back to
|
| 11 |
+
# the local `hf auth login` token when there is no OAuth session.
|
| 12 |
+
token = oauth_token.token if oauth_token else get_token()
|
| 13 |
+
return OpenAI(
|
| 14 |
+
base_url="https://router.huggingface.co/v1",
|
| 15 |
+
api_key=token,
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def glm_chat(
|
| 20 |
+
prompt: str,
|
| 21 |
+
system_prompt: str = "You are a helpful assistant.",
|
| 22 |
+
token: Optional[gr.OAuthToken] = None,
|
| 23 |
+
) -> str:
|
| 24 |
+
"""Chat with GLM-5.3 via Hugging Face Inference Providers (streaming)."""
|
| 25 |
+
stream = _client(token).chat.completions.create(
|
| 26 |
+
model="zai-org/GLM-5.3:zai-org",
|
| 27 |
+
messages=[
|
| 28 |
+
{"role": "system", "content": system_prompt},
|
| 29 |
+
{"role": "user", "content": prompt},
|
| 30 |
+
],
|
| 31 |
+
stream=True,
|
| 32 |
+
)
|
| 33 |
+
return "".join(
|
| 34 |
+
chunk.choices[0].delta.content or ""
|
| 35 |
+
for chunk in stream
|
| 36 |
+
if chunk.choices and chunk.choices[0].delta
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
gr.Workflow(
|
| 41 |
+
bind={"GLM-5.3 Chat": glm_chat},
|
| 42 |
+
graph="workflow.json",
|
| 43 |
+
).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=6.26.0
|
| 2 |
+
openai
|
| 3 |
+
huggingface_hub
|
workflow.json
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
{"schema_version":"2","name":"
|
|
|
|
| 1 |
+
{"schema_version":"2","name":"GLM-5.3 Chat Workflow","references":[{"id":"ref_prompt","label":"Prompt","role":"reference","asset_type":"text","inputs":[{"id":"in","label":"Text","type":"text"}],"outputs":[{"id":"out","label":"Text","type":"text"}],"data":{"out":""},"width":200},{"id":"ref_system","label":"System Prompt","role":"reference","asset_type":"text","inputs":[{"id":"in","label":"Text","type":"text"}],"outputs":[{"id":"out","label":"Text","type":"text"}],"data":{"out":""},"width":200}],"operators":[{"id":"op_glm","label":"GLM-5.3 Chat","role":"operator","kind":"fn","fn":"GLM-5.3 Chat","inputs":[{"id":"prompt","label":"Prompt","type":"text","required":true},{"id":"system_prompt","label":"System Prompt","type":"text"}],"outputs":[{"id":"out_0","label":"Response","type":"text","output_index":0}],"data":{},"width":200}],"subjects":[{"id":"sub_response","label":"Response","role":"subject","asset_type":"text","inputs":[{"id":"in","label":"Text","type":"text"}],"outputs":[{"id":"out","label":"Text","type":"text"}],"data":{"in":""},"width":200}],"edges":[{"id":"e1","from_node_id":"ref_prompt","from_port_id":"out","to_node_id":"op_glm","to_port_id":"prompt","type":"text"},{"id":"e2","from_node_id":"ref_system","from_port_id":"out","to_node_id":"op_glm","to_port_id":"system_prompt","type":"text"},{"id":"e3","from_node_id":"op_glm","from_port_id":"out_0","to_node_id":"sub_response","to_port_id":"in","type":"text"}]}
|