gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
30.56k stars 2.27k forks source link

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

Closed young-hun-jo closed 2 weeks ago

young-hun-jo commented 2 weeks ago

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 commented 2 weeks ago

🪼 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
young-hun-jo commented 2 weeks ago

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 commented 2 weeks ago

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
gradio minor

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](https://github.com/young-hun-jo/gradio/edit/blocks/revise-file/.changeset/tangy-moose-check.md).
freddyaboulton commented 2 weeks ago

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!