Dynamic duration

#1
by binaryreporter - opened

Would be cool if the @spaces.GPU duration could be set in the advanced options.
Default to 85 but allow people to pick the amount they want...

@spaces.GPU also supports dynamic durations:
https://huggingface.co/docs/hub/spaces-zerogpu

Gemini says it's possible:

To set a dynamic duration based on a user-inputted field (like a slider or a number box) without any extra math, the setup remains the same: your duration function must still "receive" all the arguments from the main function, but it will simply return the value of the specific field you want.

Here is the cleanest way to implement this in Gradio:

Python

import spaces
import gradio as gr

# 1. This function 'intercepts' the inputs and tells ZeroGPU how long to run.
# It MUST take the same arguments as your main function.
def get_requested_time(prompt, user_seconds):
    return user_seconds

# 2. Use the function name as the 'duration' parameter
@spaces.GPU(duration=get_requested_time)
def my_gpu_task(prompt, user_seconds):
    # Your GPU code goes here
    # ...
    return f"Task completed using {user_seconds}s of GPU quota."

# 3. Create the UI
with gr.Blocks() as demo:
    prompt_input = gr.Textbox(label="Prompt")
    
    # This field controls the GPU duration directly
    seconds_slider = gr.Slider(
        minimum=10, 
        maximum=120, 
        value=60, 
        step=5, 
        label="GPU Duration (Seconds)"
    )
    
    run_btn = gr.Button("Run")
    output = gr.Textbox()

    # Ensure the order of inputs matches the function arguments
    run_btn.click(
        fn=my_gpu_task, 
        inputs=[prompt_input, seconds_slider], 
        outputs=output
    )

demo.launch()

Important Technical Details:
The "Mirror" Rule: The get_requested_time function and the my_gpu_task function must have the exact same signature (same number of arguments in the same order). If they don't match, ZeroGPU will throw a TypeError.

The 120s Ceiling: Even if a user types 500 into a text field, Hugging Face ZeroGPU typically enforces a hard cap of 120 seconds for free/pro tiers. If the value returned is higher than the system allows, it will either default to the max or error out.

Default Values: Always set a value and maximum on your Gradio slider/number box to prevent users from accidentally requesting 0 seconds (which would crash the task) or 10,000 seconds.

Sign up or log in to comment