Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| import spaces | |
| import os | |
| # Dummy handshake for Zero-GPU boot scanner validation | |
| 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()) | |