Skip to content

Latest commit

 

History

57 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gradio.NET: Build Machine Learning Web Apps — in .NET main NuGet

English | 简体中文 | 日本語

For more information, see the Guides.

Gradio for .NET – a port of Gradio, an open-source Python package that allows you to quickly build a demo or web application for your machine learning model, API, or any arbitrary Python function. No JavaScript, CSS, or web hosting experience needed!

demo

It just takes a few lines of .NET code to create a beautiful demo like the one above, so let's get started 💫

Building Your First Demo

    1. Create an ASP.NET Core Web API project.
    1. Install NuGet package Gradio.Net.
    1. Enter the sample code in Program.cs:

Using gr.Interface (quickest way)

using Gradio.Net;

string Greet(string name, double intensity)
{
    return "Hello, " + name + "!" + new string('!', (int)intensity - 1);
}

var demo = gr.Interface(
    fn: Greet,
    inputs: new object[] { "text", gr.Slider(value: 2, minimum: 1, maximum: 10, step: 1) },
    outputs: new[] { "text" }
);

await demo.Launch();

gr.Interface wraps any C# function into a UI in seconds — no layout code required.

Using gr.Blocks (full layout control)

using Gradio.Net;

await (await CreateBlocks()).Launch();

async Task<Blocks> CreateBlocks()
{
    using (var blocks = gr.Blocks())
    {
        gr.Markdown("Start typing below and then click **Run** to see the output.");
        Textbox input, output;
        using (gr.Row())
        {
            input = gr.Textbox(placeholder: "What is your name?");
            output = gr.Textbox();
        }
        var btn = gr.Button("Run");
        btn.Click(
            fn: new Func<string, (string, string)>(name => ($"Welcome to Gradio.Net, {name}!", "")),
            inputs: new[] { input },
            outputs: new[] { output, input });

        return blocks;
    }
}

That's All 🎉🎉🎉

Integrating into an existing ASP.NET Core project

Use the AddGradio and UseGradio extension methods:

using Gradio.Net;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGradio();

var app = builder.Build();

app.UseGradio(await CreateBlocks());

app.Run();

async Task<Blocks> CreateBlocks()
{
    using (var blocks = gr.Blocks())
    {
        gr.Markdown("# My Gradio App");
        Textbox input, output;
        using (gr.Row())
        {
            input = gr.Textbox(placeholder: "What is your name?");
            output = gr.Textbox();
        }
        var btn = gr.Button("Run");
        btn.Click(
            fn: new Func<string, (string, string)>(name => ($"Welcome to Gradio.Net, {name}!", "")),
            inputs: new[] { input },
            outputs: new[] { output, input });

        return blocks;
    }
}

You can also use gr.Interface with app.UseGradio:

using Gradio.Net;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGradio();

var app = builder.Build();

string Greet(string name, double intensity)
{
    return "Hello, " + name + "!" + new string('!', (int)intensity - 1);
}

var demo = gr.Interface(
    fn: Greet,
    inputs: new object[] { "text", gr.Slider(value: 2, minimum: 1, maximum: 10, step: 1) },
    outputs: new[] { "text" }
);

app.UseGradio(await demo.BuildBlocks());

app.Run();

Demos

Source Code Demo Image
Layout image
Form image
Media image
Chatbot image
Progress image
Theme image

About

⭐Gradio for .NET – a port of Gradio, an open-source Python package that allows you to quickly build a demo or web application for your machine learning model, API, or any arbitrary Python function. Gradio for .NET – 基于 Gradio 的 .NET 移植,Gradio 是一个开源 Python 包,允许你为机器学习模型、API 或任何任意 Python 函数快速构建演示或 Web 应用程序。

Resources

Stars

333 stars

Watchers

7 watching

Forks

Releases

Packages

Used by

Contributors

Languages