gradio icon indicating copy to clipboard operation
gradio copied to clipboard

Unable to access gr.Request and gr.Progress within the same function call.

Open Solomin0 opened this issue 1 year ago • 1 comments

Describe the bug

I have no idea what is going on but I am going to try my best to explain this. I included a simplified document upload function for the issue. I have a file upload method, the method is called either on file upload from the gr.File signal or it can also be called when a gr.Button is clicked.

When the signal comes from the button click the progress bar functions properly however the request variable is None and i am unable to get it to show up any other way.

When the signal comes from the file upload the request works as expected but the letter variable in my for loop becomes a <gradio.helpers.Progress object at 0x000001E239FD9F30> instead of a char which it should be.

If you need more code let me know I would really like to get this sorted but dont know if I'm doing something wrong or if it is a gradio bug.

I saw https://github.com/gradio-app/gradio/issues/3568 which seemed similar so I'm linking it here.

Have you searched existing issues? 🔎

  • [X] I have searched and found no existing issues

Reproduction

import gradio as gr

def file_upload_chroma(file_list,collection_name,chunksize,overlap, request: gr.Request,progress=gr.Progress()):
    progress(0, desc="Test", unit = "Files")
    username = request.username 
    
    a = "abcdefghijklmnopqrstuv"    
    for letter in progress.tqdm(a, desc = "TEST", unit = "Files"):
        print(letter)
        time.sleep(0.1)
    return gr.Textbox(a)  

file_upload.upload(file_upload_chroma,inputs = [file_upload,file_uploader_radio_buttons,chunksize,overlap], outputs = [tb_file_listings],queue = False )

new_feature.click(file_upload_chroma , outputs = [tb_file_listings])

Screenshot

No response

Logs

When file upload signal calls the method. (not technically an error but doesnt display a progress bar as expected)
<gradio.helpers.Progress object at 0x000001E239FD9F30>
<gradio.helpers.Progress object at 0x000001E239FD9F30>
<gradio.helpers.Progress object at 0x000001E239FD9F30>
<gradio.helpers.Progress object at 0x000001E239FD9F30>

When button click signal calls the method. 
username = request.username
AttributeError: 'NoneType' object has no attribute 'username'

System Info

Gradio Environment Information:
------------------------------
Operating System: Windows
gradio version: 4.19.0
gradio_client version: 0.10.0

------------------------------------------------
gradio dependencies in your environment:

aiofiles: 23.2.1
altair: 5.2.0
fastapi: 0.109.2
ffmpy: 0.3.2
gradio-client==0.10.0 is not installed.
httpx: 0.26.0
huggingface-hub: 0.20.3
importlib-resources: 6.1.1
jinja2: 3.1.3
markupsafe: 2.1.5
matplotlib: 3.8.3
numpy: 1.26.4
orjson: 3.9.14
packaging: 23.2
pandas: 2.2.0
pillow: 10.2.0
pydantic: 2.6.1
pydub: 0.25.1
python-multipart: 0.0.9
pyyaml: 6.0.1
ruff: 0.2.1
semantic-version: 2.10.0
tomlkit==0.12.0 is not installed.
typer: 0.9.0
typing-extensions: 4.9.0
uvicorn: 0.27.1
authlib; extra == 'oauth' is not installed.
itsdangerous; extra == 'oauth' is not installed.


gradio_client dependencies in your environment:

fsspec: 2024.2.0
httpx: 0.26.0
huggingface-hub: 0.20.3
packaging: 23.2
typing-extensions: 4.9.0
websockets: 11.0.3

Severity

Blocking usage of gradio

Solomin0 avatar Feb 16 '24 23:02 Solomin0

Hi @Solomin0 can you provide a complete, minimal example to reproduce this issue? For example, I'm not able to reproduce the issue with this code example:

import time

import gradio as gr

def test(x, request: gr.Request, progress=gr.Progress()):
    progress(0, desc="Test", unit = "Files")
    print("request", request)    
    a = "abcdefghijklmnopqrstuv"    
    for letter in progress.tqdm(a, desc = "TEST", unit = "Files"):
        time.sleep(0.1)
    return gr.Textbox(a)  

with gr.Blocks() as demo:
    t = gr.Textbox()
    b = gr.Button()
    f = gr.File()
    
    b.click(test, t, t)
    t.submit(test, t, t)
    f.upload(test, t, t)
    
demo.launch()

abidlabs avatar Feb 22 '24 19:02 abidlabs

I think I figured out the difference. I had queue = False in one of my signals when it needed to be true for both of them. That is what caused the "letter" variable to be a <gradio.helpers.Progress object >. I was looking at method signature the whole time it was actually the signal call. Thanks @abidlabs for the working example.

I can leave queue enabled but are queues object specific? (my inferences can take a while and i dont want to block all file uploads if a user is using the llm or visa versa)

or is there a seperate queue for each widget?

Solomin0 avatar Feb 23 '24 19:02 Solomin0

Just read over the .queue documentation it looks like because my chat function is a different function it gets a different cpu/stack so I think they dont interfere. Please correct me if that is incorrect.

Thanks

Solomin0 avatar Feb 23 '24 19:02 Solomin0

Ah that explains it. Yes, you need queue enabled to use gr.Progress. Yes, although there is a single queue, you get one worker per function by default. So your functions should not interfere with each other.

See https://www.gradio.app/guides/setting-up-a-demo-for-maximum-performance#overview-of-gradios-queueing-system for more info

abidlabs avatar Feb 23 '24 19:02 abidlabs