Gradio.NET 的简单入门使用

1、最近在网络上由发现了一个好完的东西。

2、Gradio.NET通过简单的C# Web API几行代码就可以实现一网页界面。

3、Python中也有一个Gradio,功能好像都差不多哦,不废话了,我们来开始实操。

4、在Visual Studio 2022 中创建一个 ASP.NET Croe Web API 项目,如下图:

5、在项目的NuGet中添加Gradio.Net。如下图。

6、Program.cs中代码如下。

第一种代码。

cs 复制代码
using Gradio.Net;

namespace WebApplication1
{
    public class Program
    {
        public static async Task Main(string[] args)
        {
            App.Launch(await CreateBlocks());

            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");
                    await btn.Click(fn: async (input) => gr.Output($"Welcome to Gradio.Net, {input.Data[0]}!"), inputs: new[] { input }, outputs: new[] { output });

                    return blocks;
                }
            }
        }
    }
}

第二种代码。

cs 复制代码
using Gradio.Net;

namespace WebApplication1
{
    public class Program
    {
        public static async Task Main(string[] args)
        {
            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");
                    await btn.Click(fn: async (input) => gr.Output($"Welcome to Gradio.Net, {input.Data[0]}!"), inputs: new[] { input }, outputs: new[] { output });

                    return blocks;
                }
            }

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

            var app = builder.Build();

            app.UseGradio(await CreateBlocks());

            app.Run();
        }
    }
}

7、运行程序后,在网页上访问http://localhost:5248/ 后边的5248与项目设置的端口有关。

8、项目地址

GitHub - feiyun0112/Gradio.Net: 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 应用程序。

https://github.com/khcheung/gradio-client-net

相关推荐
驰骋工作流1 小时前
请假流程:六款.NET工作流引擎实现方式对比
.net·ccflow·工作流引擎二开·.net工作流引擎对比
WarPigs2 小时前
.NET项目app.Config笔记
c#·.net
海盗12341 天前
微软技术周报——2026-07-22
microsoft·c#·.net
海盗12341 天前
微软技术日报 ——2026-07-21
microsoft·c#·.net
半亩码田1 天前
【.NET新特性·第8篇】.NET 9 AI 构建基块:Microsoft.Extensions.AI
人工智能·microsoft·.net
Ctrl+Z侠2 天前
.NET WebApi Windows / Linux Docker 全链路压测与瓶颈定位 0 到 1 教程
linux·windows·.net
慧都小妮子2 天前
Aspose.CAD for .NET 26.6 更新发布
pdf·.net·3d渲染·cad·pdf导出·ifc编辑
夜莺悠吟2 天前
关于对 C# 中 ImplicitUsings,GlobalUsings 的讨论
c#·.net