All guides

Build a File-Processing Page with Gradio Interface

Use gr.Interface to turn a Python function that handles an image file path into a page: match components, set type="filepath", add validation and check the result.

AINavWritten / Updated Sources checked
On this page

At a glance

When you have a Python function that processes a file and want colleagues to pick a file, press a button and read the result in a browser, the documentation's high-level route is gr.Interface: you state the function, the input components and the output components. This guide uses an image file as the case. It focuses on making the function receive a file path instead of a pixel array and on showing a clear message when input is missing. File names and content are synthetic; no real material is involved.

Workflow

  1. Count-matching layer

    The number of inputs components must equal the number of function parameters, and the number of outputs components must equal the number of return values, in order.

  2. Data-shape layer

    The Image component converts to a NumPy array by default. If the function needs a file path, write gr.Image(type="filepath").

  3. Page-behavior layer

    After launch, check that the labels and description are clear, then try one normal file and one failure case.

Each stage cites official sources.

Before you start

  • A working Python environment with Gradio installed. This guide requires Python 3.10 or higher.
  • A small test image, for example a blank sample_a.png you generate yourself; do not use images containing personal information.
  • A script file such as file_demo.py.
  • Know how many parameters the function takes and how many values it returns, because the component counts must match.

1. Fix the function's inputs and outputs first

In file_demo.py, write inspect(path, note) returning a string. path is the image file path and note is a user-typed remark. Two parameters need two input components, and one return value needs one output component.

2. Choose the input components

Use gr.Image(type="filepath") for the first input so the function gets a path rather than a NumPy array, and "text" for the second. The order must match the function's parameter order.

3. Choose the output and add descriptions

Use gr.Textbox(label="Check result", lines=3) as the output and set a label on each input, adding info where usage needs explaining. The title and description appear at the top of the interface and explain its purpose.

4. Add validation

At the start of the function, test whether path is empty and, if so, raise gr.Error("Please choose an image first"). The documentation's calculator example handles division by zero this way, so users see a clear message instead of a crash.

5. Launch and try it

Create demo = gr.Interface(...), call demo.launch(), run the script in a terminal and open http://localhost:7860 in a browser with the default launch settings. Upload sample_a.png with a remark and check the output text. Then submit with no file and confirm the error message appears.

6. Fold away extras and stop

If you later add many parameters, put them in additional_inputs, which sits in a collapsed accordion by default and is passed to the function in order after the standard inputs. When testing ends, interrupt the process in the terminal running the script to stop the page.

Original example

A synthetic sample that works on your own sample_a.png:

python
import os
import gradio as gr

def inspect(path, note):
    if not path:
        raise gr.Error("Please choose an image first")
    return f"File name: {os.path.basename(path)}; note: {note}"

demo = gr.Interface(
    fn=inspect,
    inputs=[gr.Image(type="filepath", label="Image"), gr.Textbox(label="Note")],
    outputs=[gr.Textbox(label="Check result", lines=3)],
    title="File check sample",
    description="Synthetic demo",
    api_name="predict",
)
demo.launch()

Upload sample_a.png with the note "batch one"; the output box should show the file name and note. Submit with no file and you should see "Please choose an image first". The exact file name shown depends on what happens when you run it locally.

Completion checks

  • Two input components map to two function parameters, and one output component to one return value.
  • Inside the function, path is a string path, not an array.
  • Submitting without a file shows the custom error message.
  • Each component has a clear label, and the page has a title and description.
  • The test image contains no personal or internal information.

Limits

  • This covers only one image file handled by gr.Interface and returned as text; batch jobs, long-task progress, file access permissions and public sharing are not included.
  • The documentation example shows the Image component with type="filepath". For other file-type components, consult the official component docs; this guide does not infer their formats.
  • Interface suits functions with clear inputs and outputs; complex layouts or linked events belong to Blocks.
  • The sample returns only a file name and note, performs no analysis of image content, and promises no processing speed.

Verification scope

This guide is based on reading the official documentation. I did not log in, install Gradio, run the sample code, execute any product API, upload files or produce generated output; the code is an illustrative sample written to follow the documented pattern and has not been run.

Article sources

Original editorial content; official descriptions and checking suggestions are distinguished.

  1. Gradio · Official sources
    View details
  2. Gradio · Official sources
    View details