Skip to main content

Define Gradio using type hints

Project description

FnGradio

This is an experimental library that allows you to define Gradio apps using type hints.

Install

pip install fngradio

Simple Example

Instead of (where type hints are not used for the interface):

import gradio as gr


def add_int_numbers(a: int, b: int) -> int:
    """
    Add two int numbers
    """
    return a + b


demo = gr.Interface(
    fn=add_int_numbers,
    api_name="add_int_numbers",
    inputs=[
        gr.Number(precision=0),
        gr.Number(precision=0)
    ],
    outputs=[gr.Number(precision=0)],
)

if __name__ == '__main__':
    demo.launch(share=False)

You can define the Gradio interface around by just adding the fngr.interface annotation which will create inputs and outputs based on the type hints:

import fngradio as fngr


@fngr.interface()
def add_int_numbers(a: int, b: int) -> int:
    """
    Add two int numbers
    """
    return a + b


if __name__ == '__main__':
    add_int_numbers.launch(share=False)

Slider for Integer With Range

You can use pydantic's Field annotation to provide additional information. If ge and le are defined for an integer, then it will use the Gradio's Slider component.

from typing import Annotated
from pydantic import Field
import fngradio as fngr


@fngr.interface()
def add_int_numbers_with_sliders(
    a: Annotated[int, Field(title="First value", ge=0, le=100)] = 50,
    b: Annotated[int, Field(title="Second value", ge=0, le=100)] = 50
) -> int:
    """
    Add two int numbers
    """
    return a + b

Dropdown for Literal

from typing import Literal
from pydantic import Field
import fngradio as fngr


@fngr.interface
def say(what: Literal["hi", "bye"]) -> str:
    """
    Says Hi! or Bye!
    """
    return "Hi!" if what == "hi" else "Bye!"

Image for PIL.Image.Image

from typing import Literal
import PIL.Image
from pydantic import Field
import fngradio as fngr


@fngr.interface
def generate_image(
    width: Annotated[int, Field(ge=10, le=200)] = 100,
    height: Annotated[int, Field(ge=10, le=200)] = 100
) -> PIL.Image.Image:
    """
    Generates sample image
    """
    image = PIL.Image.new(
        mode="RGB",
        size=(width, height),
        color="lightblue"
    )
    return image

Specify Component in Type Annotation

You can also specify the Gradio Component to use by adding it to the type annotation:

from typing import Annotated
import gradio as gr
import fngradio as fngr


@fngr.interface()
def to_upper_case(
    s: Annotated[str, gr.TextArea(label="text", value="Hello")]
) -> Annotated[str, gr.TextArea()]:
    """
    Converts text to upper case
    """
    return s.upper()

Tabbed Interface

A tabbed interface can be useful when you have multiple tools (e.g. multiple MCP tools).

Instead of:

demo = gr.TabbedInterface(
    interface_list=[
        add_int_numbers,
        to_upper_case
    ],
    tab_names=["add_int_numbers", "to_upper_case"]
)

You could use the fngr.tabbed_interface:

demo = fngr.tabbed_interface([
    add_int_numbers,
    to_upper_case
])

The main advantage is that it will try to infer the names from the interface.

Or even simpler use FnGradioApp for defining interfaces:

from fngradio import FnGradioApp


app = FnGradioApp()

@app.interface()
def add_int_numbers(a: int, b: int) -> int:
    """
    Add two int numbers
    """
    return a + b


@app.interface()
def to_upper_case(s: str) -> str:
    """
    Converts text to upper case
    """
    return s.upper()


demo = app.tabbed()

if __name__ == '__main__':
    demo.launch(share=False)

Combine Apps

In order to support apps across modules, you can combine apps:

from fngradio import FnGradioApp


app_1 = FnGradioApp()
app_2 = FnGradioApp()

@app_1.interface()
def upper(s: str) -> str:
    return s.upper()


@app_2.interface()
def lower(s: str) -> str:
    return s.lower()


app = FnGradioApp(apps=[app_1, app_2])

Example Apps

Example Gradio apps demonstrating use:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fngradio-0.0.9.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fngradio-0.0.9-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file fngradio-0.0.9.tar.gz.

File metadata

  • Download URL: fngradio-0.0.9.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fngradio-0.0.9.tar.gz
Algorithm Hash digest
SHA256 80acb6cf5923ddf7289c3f2bb66a7a288f912c6523baaecc7c226c984c8e455f
MD5 efc8e50135f01c13dc4eb51dafde6288
BLAKE2b-256 0eaf3c81fb3f41c50463705eafa76c4c51aa2993548a61d0d68269c1863cf78a

See more details on using hashes here.

Provenance

The following attestation bundles were made for fngradio-0.0.9.tar.gz:

Publisher: ci.yml on de-code/fngradio

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fngradio-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: fngradio-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fngradio-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 114fda3f5a4ae9c6425d0dc5c4311ecf1bc824ee3c38e3c64cd994307c8628cd
MD5 e6de6d824979c8ab8cffd9bfb1fd36c6
BLAKE2b-256 34436703c29f43cee93f8969980c39af2d6934b59e48adba76b7203d0865f02d

See more details on using hashes here.

Provenance

The following attestation bundles were made for fngradio-0.0.9-py3-none-any.whl:

Publisher: ci.yml on de-code/fngradio

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page