Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
MAX_NON_INT_ITERATIONS = 5
|
| 5 |
+
|
| 6 |
+
def test_loop_function(iterations_count, text_param, progress=gr.Progress()):
|
| 7 |
+
print(f"\n--- test_loop_function CALLED ---")
|
| 8 |
+
print(f"Expected iterations_count: {iterations_count} (type: {type(iterations_count)})")
|
| 9 |
+
print(f"text_param: '{text_param}' (type: {type(text_param)})")
|
| 10 |
+
print(f"Progress object received: {progress} (type: {type(progress)})")
|
| 11 |
+
|
| 12 |
+
if not isinstance(iterations_count, int):
|
| 13 |
+
try:
|
| 14 |
+
iterations_count = int(iterations_count)
|
| 15 |
+
print(f"Converted iterations_count to int: {iterations_count}")
|
| 16 |
+
except ValueError:
|
| 17 |
+
error_msg = f"ERROR: iterations_count ('{iterations_count}') could not be converted to int."
|
| 18 |
+
print(error_msg)
|
| 19 |
+
return error_msg
|
| 20 |
+
|
| 21 |
+
log_messages = [
|
| 22 |
+
f"Expected iterations: {iterations_count}",
|
| 23 |
+
f"Text param: {text_param}"
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
non_int_i_counter = 0
|
| 27 |
+
|
| 28 |
+
# The loop in question
|
| 29 |
+
for i in progress.tqdm(range(iterations_count), desc="Processing..."):
|
| 30 |
+
current_log = f"Loop variable i = {i}, type(i) = {type(i)}"
|
| 31 |
+
print(current_log)
|
| 32 |
+
log_messages.append(current_log)
|
| 33 |
+
|
| 34 |
+
if not isinstance(i, int):
|
| 35 |
+
print(f"!!! BUG BEHAVIOR OBSERVED: 'i' is not an int !!!")
|
| 36 |
+
log_messages.append(f"!!! BUG: 'i' is {type(i)}, not int !!!")
|
| 37 |
+
non_int_i_counter += 1
|
| 38 |
+
if non_int_i_counter >= MAX_NON_INT_ITERATIONS:
|
| 39 |
+
print(f"Breaking loop after {non_int_i_counter} non-integer 'i' values.")
|
| 40 |
+
log_messages.append("Breaking loop due to repeated non-integer 'i'.")
|
| 41 |
+
break
|
| 42 |
+
|
| 43 |
+
time.sleep(0.1)
|
| 44 |
+
|
| 45 |
+
final_message = "Loop finished."
|
| 46 |
+
if non_int_i_counter > 0 :
|
| 47 |
+
final_message = "Loop finished (or was broken) with non-integer 'i' issues."
|
| 48 |
+
|
| 49 |
+
print(final_message)
|
| 50 |
+
log_messages.append(final_message)
|
| 51 |
+
return "\n".join(log_messages)
|
| 52 |
+
|
| 53 |
+
with gr.Blocks() as demo:
|
| 54 |
+
gr.Markdown(
|
| 55 |
+
"# Bug Reproduction: `gr.Progress.tqdm` with `gr.Examples`\n"
|
| 56 |
+
"Checking if `i` in `for i in progress.tqdm(range(N))` becomes a `gr.Progress` object "
|
| 57 |
+
"when the function is called by `gr.Examples`."
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
iterations_slider = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Number of Iterations")
|
| 62 |
+
dummy_text = gr.Textbox(label="Dummy Text Parameter", value="Test")
|
| 63 |
+
|
| 64 |
+
output_log = gr.Textbox(label="Function Log Output", lines=15, interactive=False)
|
| 65 |
+
|
| 66 |
+
run_button = gr.Button()
|
| 67 |
+
run_button.click(
|
| 68 |
+
fn=test_loop_function,
|
| 69 |
+
inputs=[iterations_slider, dummy_text],
|
| 70 |
+
outputs=[output_log]
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
gr.Examples(
|
| 74 |
+
examples=[
|
| 75 |
+
[5, "Example Case 1"],
|
| 76 |
+
[2, "Example Case 2"],
|
| 77 |
+
],
|
| 78 |
+
inputs=[iterations_slider, dummy_text],
|
| 79 |
+
outputs=[output_log],
|
| 80 |
+
fn=test_loop_function,
|
| 81 |
+
cache_examples="lazy"
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
gr.Markdown("Console output (printed by the function) will also show detailed logs.")
|
| 85 |
+
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
demo.launch()
|
| 88 |
+
|
| 89 |
+
demo.launch(debug=True) # debug=True can be helpful
|