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

相关推荐
白衣衬衫 两袖清风9 小时前
ABP框架+Dapper执行原生sql
sql·c#·.net
我是唐青枫11 小时前
深入理解 C#.NET 运算符重载:语法、设计原则与最佳实践
开发语言·c#·.net
武藤一雄14 小时前
.NET 中常见计时器大全
microsoft·微软·c#·.net·wpf·.netcore
用户44884667106017 小时前
.NET进阶——深入理解线程(1)同步异步与单线程多线程的区分
c#·.net
编程乐趣17 小时前
qdrant-dotnet:官方提供的开源 .NET 客户端库,用于与 Qdrant 向量搜索引擎操作!
c#·.net
我是唐青枫18 小时前
告别频繁 GC:C#.NET PooledList 的设计与使用场景
c#·.net
唐青枫18 小时前
C#.NET ref struct 深度解析:语义、限制与最佳实践
c#·.net
Zhen (Evan) Wang2 天前
.NET 6 API使用Serilog APM
c#·.net
武藤一雄2 天前
[.NET] 中 System.Collections.Generic命名空间详解
windows·微软·c#·asp.net·.net·.netcore
军训猫猫头2 天前
3.NModbus4 长距离多设备超时 C# + WPF 完整示例
c#·.net·wpf·modbus