Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: exception handling about file_count params of File component#8529

Merged
freddyaboulton merged 3 commits into
gradio-app:mainfrom
iamcodingcat:blocks/revise-file
Jun 11, 2024
Merged

feat: exception handling about file_count params of File component#8529
freddyaboulton merged 3 commits into
gradio-app:mainfrom
iamcodingcat:blocks/revise-file

Conversation

@iamcodingcat

Copy link
Copy Markdown
Contributor

Description

According to docs, Parameter file_count of File component must be one of them(single, multiple, directory). But I put other integer value(ex. file_count=0) in the parameter by mistake, below error occurs.

Traceback (most recent call last):
  File "/Users/zedd.ai/.pyenv/versions/3.10.12/envs/gradio-pr/lib/python3.10/site-packages/gradio/queueing.py", line 532, in process_events
    response = await route_utils.call_process_api(
  File "/Users/zedd.ai/.pyenv/versions/3.10.12/envs/gradio-pr/lib/python3.10/site-packages/gradio/route_utils.py", line 276, in call_process_api
    output = await app.get_blocks().process_api(
  File "/Users/zedd.ai/.pyenv/versions/3.10.12/envs/gradio-pr/lib/python3.10/site-packages/gradio/blocks.py", line 1924, in process_api
    inputs = await self.preprocess_data(
  File "/Users/zedd.ai/.pyenv/versions/3.10.12/envs/gradio-pr/lib/python3.10/site-packages/gradio/blocks.py", line 1653, in preprocess_data
    inputs_cached = block.data_model(**inputs_cached)  # type: ignore
TypeError: gradio.data_classes.FileData() argument after ** must be a mapping, not list

The error confused me because the message of error doesn't tell me the reason why the error occurs. So, I debugged above traceback error and followed the objects of all funcions in the stdout error. Finally, I found my mistake of putting the integer value in file_count parameter of File component. my error code is below.

import gradio as gr
import pandas as pd

from gradio.themes.base import Base

def preprocess_file(file) -> str:
    df = pd.read_csv(file.name)

    column = df.columns[0]
    print("file:", type(file), type(file.name))
    print("df:", type(df))
    return column


class Seafoam(Base):
    pass


with gr.Blocks(theme=Seafoam(), title="TEST") as demo:
    gr.Markdown("""
    # TEST
    """)
    with gr.Tab("TEST"):
        file_output = gr.File(file_count=1, file_types=["csv"])   # it causes error
        upload_button = gr.Button(value="submit button")

    opt = gr.Textbox()
    upload_button.click(
        fn=preprocess_file,
        inputs=file_output,
        outputs=opt
    )

demo.launch(debug=True)

Of course, according to official docs, there is a content about type of the parameter. But, if the validation logic exists in the parameter, I should have found the reason of error earlier. So, for like me, I append validation logic of the parameter in this PR

🎯 PRs Should Target Issues

According to the list of issue, I can't find the issue about this parameter of File component now

Tests

  1. PRs will only be merged if tests pass on CI. To run the tests locally, please set up your Gradio environment locally and run the tests: bash scripts/run_all_tests.sh

  2. You may need to run the linters: bash scripts/format_backend.sh and bash scripts/format_frontend.sh

@gradio-pr-bot

gradio-pr-bot commented Jun 11, 2024

Copy link
Copy Markdown
Collaborator

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/9a4fc1b0d2d80ed578dc24406277adaa2988e44e/gradio-4.36.1-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@9a4fc1b0d2d80ed578dc24406277adaa2988e44e#subdirectory=client/python"

Install Gradio JS Client from this PR

npm install https://gradio-builds.s3.amazonaws.com/9a4fc1b0d2d80ed578dc24406277adaa2988e44e/gradio-client-1.1.1.tgz

@iamcodingcat

Copy link
Copy Markdown
Contributor Author

Above test / python / linux job is failed and I checked the details. But why the error occurs? I'm newbie in contribution and the error is just okay?

@gradio-pr-bot

Copy link
Copy Markdown
Collaborator

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
gradio minor
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

feat: exception handling about file_count params of File component

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@freddyaboulton

Copy link
Copy Markdown
Contributor

Hi @young-hun-jo , the issue is that the code was not up to the standard of our automatic formatter. In the future, you can fix this with bash scripts/format_backend.sh. I pushed up a fix. This looks good to me, thanks!

@freddyaboulton freddyaboulton enabled auto-merge (squash) June 11, 2024 18:07
@freddyaboulton freddyaboulton merged commit d43d696 into gradio-app:main Jun 11, 2024
@pngwn pngwn mentioned this pull request Jun 11, 2024
@pngwn pngwn mentioned this pull request Jun 25, 2024
@iamcodingcat iamcodingcat deleted the blocks/revise-file branch May 8, 2025 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants