import gradio as gr import time MAX_NON_INT_ITERATIONS = 5 def test_loop_function(iterations_count, text_param, progress=gr.Progress()): print(f"\n--- test_loop_function CALLED ---") print(f"Expected iterations_count: {iterations_count} (type: {type(iterations_count)})") print(f"text_param: '{text_param}' (type: {type(text_param)})") print(f"Progress object received: {progress} (type: {type(progress)})") log_messages = [ f"Expected iterations: {iterations_count}", f"Text param: {text_param}" ] non_int_i_counter = 0 # The loop in question for i in progress.tqdm(range(iterations_count), desc="Processing..."): current_log = f"Loop variable i = {i}, type(i) = {type(i)}" print(current_log) log_messages.append(current_log) if not isinstance(i, int): print(f"!!! BUG BEHAVIOR OBSERVED: 'i' is not an int !!!") log_messages.append(f"!!! BUG: 'i' is {type(i)}, not int !!!") non_int_i_counter += 1 if non_int_i_counter >= MAX_NON_INT_ITERATIONS: print(f"Breaking loop after {non_int_i_counter} non-integer 'i' values.") log_messages.append("Breaking loop due to repeated non-integer 'i'.") break time.sleep(0.1) final_message = "Loop finished." if non_int_i_counter > 0 : final_message = "Loop finished (or was broken) with non-integer 'i' issues." print(final_message) log_messages.append(final_message) return "\n".join(log_messages) with gr.Blocks() as demo: gr.Markdown( "# Bug Reproduction: `gr.Progress.tqdm` with `gr.Examples`\n" "Checking if `i` in `for i in progress.tqdm(range(N))` becomes a `gr.Progress` object " "when the function is called by `gr.Examples`." ) with gr.Row(): iterations_slider = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Number of Iterations") dummy_text = gr.Textbox(label="Dummy Text Parameter", value="Test") output_log = gr.Textbox(label="Function Log Output", lines=15, interactive=False) run_button = gr.Button() run_button.click( fn=test_loop_function, inputs=[iterations_slider, dummy_text], outputs=[output_log] ) gr.Examples( examples=[ [5, "Example Case 1"], [2, "Example Case 2"], ], inputs=[iterations_slider, dummy_text], outputs=[output_log], fn=test_loop_function, cache_examples="lazy" ) gr.Markdown("Console output (printed by the function) will also show detailed logs.") if __name__ == "__main__": demo.launch()