GLM-5.3-Flash / run.py
akhaliq's picture
akhaliq HF Staff
Capture reasoning_content, set max_tokens, surface empty-response diagnostics
579cf98
Raw
History Blame Contribute Delete
1.4 kB
import os
import gradio as gr
from openai import OpenAI
def chat(prompt: str, oauth_token: gr.OAuthToken | None = None) -> str:
"""Send a prompt to GLM-5.3-Flash via HF Inference Providers and return the reply."""
api_key = oauth_token.token if oauth_token else os.environ.get("HF_TOKEN")
if not api_key:
return "🔒 Please sign in with Hugging Face (top of the page) to run this workflow."
client = OpenAI(
base_url="https://router.huggingface.co/v1",
api_key=api_key,
)
stream = client.chat.completions.create(
model="zai-org/GLM-5.3-Flash:fastest",
messages=[{"role": "user", "content": prompt}],
stream=True,
max_tokens=8192,
)
out = []
reasoning = []
for chunk in stream:
if not chunk.choices:
continue
delta = chunk.choices[0].delta
if getattr(delta, "reasoning_content", None):
reasoning.append(delta.reasoning_content)
if delta.content:
out.append(delta.content)
answer = "".join(out).strip()
if answer:
return answer
if reasoning:
return "⚠️ The model returned only reasoning (no final answer):\n\n" + "".join(reasoning)
return "⚠️ The model returned an empty response. Please try again."
demo = gr.Workflow(bind={"glm_chat": chat})
if __name__ == "__main__":
demo.launch()