Spaces:
Sleeping
Sleeping
File size: 3,976 Bytes
9498ccd c17b5c8 f02224d dbdc59b f02224d dbdc59b c17b5c8 dbdc59b 9498ccd f02224d c17b5c8 9498ccd f02224d 9498ccd f02224d c17b5c8 f02224d c17b5c8 f02224d c17b5c8 9498ccd f02224d c17b5c8 f02224d 9498ccd c17b5c8 f02224d 9498ccd c17b5c8 f02224d c17b5c8 f02224d c17b5c8 9498ccd c17b5c8 f02224d c17b5c8 f02224d c17b5c8 9498ccd f02224d c17b5c8 f02224d 9498ccd f02224d 9498ccd c17b5c8 f02224d c17b5c8 9498ccd c17b5c8 | 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | import gradio as gr
from huggingface_hub import InferenceClient
import spaces
import os
# Dummy handshake for Zero-GPU boot scanner validation
@spaces.GPU
def zero_gpu_handshake():
pass
def generate_text(prompt, system_instruction, temperature, user_token):
if not prompt.strip():
return "Please input a prompt to generate text."
# Check for authorization credentials
hf_token = user_token.strip() if user_token.strip() else os.environ.get("HF_TOKEN")
try:
# Initialize client targeting the latest Z.ai flagship: zai-org/GLM-5.2
client = InferenceClient(model="zai-org/GLM-5.2", token=hf_token)
messages = []
if system_instruction.strip():
messages.append({"role": "system", "content": system_instruction.strip()})
messages.append({"role": "user", "content": prompt.strip()})
# Max out parameters to fully utilize GLM-5.2's massive context capabilities
response = client.chat_completion(
messages=messages,
max_tokens=16384, # Maximum supported token extraction payload
temperature=float(temperature),
top_p=0.95
)
return response.choices[0].message.content
except Exception as e:
error_msg = str(e)
if "api_key" in error_msg or "Authorization" in error_msg or "401" in error_msg:
return "β Token Required: GLM-5.2 requires a Hugging Face token via Serverless endpoints.\n\nπ Set an 'HF_TOKEN' secret inside your Space's Settings tab, or paste your token into the field below."
return f"Error executing request: {error_msg}"
def apply_logic_preset():
return 0.1 # Set ultra-low temp configuration for strict, reasoning, and dataset syntax architectures
# --- GRADIO INTERFACE CONFIGURATION ---
with gr.Blocks(title="GLM-5.2 Agent Workspace") as demo:
gr.Markdown("# π GLM-5.2 Flagship Agent Workspace")
gr.Markdown("Direct interface for Z.ai's latest **GLM-5.2** model. Optimized for long-horizon reasoning, dataset synthesis, and codebase tasks.")
with gr.Row():
with gr.Column(scale=2):
system_prompt = gr.Textbox(
label="βοΈ System Persona / Core Instructions",
placeholder="e.g., You are an advanced programming assistant. Generate code cleanly without conversational filler.",
lines=2
)
user_prompt = gr.Textbox(
label="π Context / Input Prompt",
placeholder="Enter repository code, logical prompts, or structural guidelines...",
lines=12
)
with gr.Row():
submit_btn = gr.Button("π Execute Generation", variant="primary")
preset_btn = gr.Button("π Force Logic/Dataset Preset (Temp 0.1)")
with gr.Column(scale=3):
output_display = gr.Textbox(
label="π₯ Generated Output Stream",
lines=16,
interactive=False
)
with gr.Accordion("π οΈ Settings & Hyperparameters", open=True):
token_input = gr.Textbox(
label="π Optional HF Access Token",
placeholder="hf_...",
type="password"
)
temp_slider = gr.Slider(
minimum=0.01,
maximum=1.5,
value=0.1,
step=0.05,
label="Temperature"
)
# Component Connections
submit_btn.click(
fn=generate_text,
inputs=[user_prompt, system_prompt, temp_slider, token_input],
outputs=output_display
)
preset_btn.click(
fn=apply_logic_preset,
inputs=[],
outputs=temp_slider
)
demo.launch(theme=gr.themes.Soft())
|