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

相关推荐
~plus~7 小时前
.NET 8 C# 委托与事件实战教程
网络·c#·.net·.net 8·委托与事件·c#进阶
rockey62711 小时前
AScript动态脚本多语言环境支持
sql·c#·.net·script·eval·function·动态脚本
dotNET实验室12 小时前
ASP.NET Core 内存缓存实战:一篇搞懂该怎么配、怎么避坑
.net
龙侠九重天12 小时前
ML.NET 实战:快速构建分类模型
分类·数据挖掘·c#·.net
无风听海14 小时前
.NET10之内置日志配置与使用指南
asp.net·.net
沃尔威武1 天前
数据库 Sinks(.net8)
数据库·.net·webview
大尚来也1 天前
告别“字符串拼接”:在.NET中用LINQ重塑数据查询
.net·solr·linq
无风听海1 天前
.NET10之Record 深度解析
.net
CodeCraft Studio1 天前
LightningChart .NET v12.5.1 发布:高性能数据可视化再升级,赋能工业与实时数据场景
信息可视化·.net·gpu·数据可视化·lightningchart·高性能图表开发·数据可视化引擎
CyL_Cly1 天前
.net framework 3.5下载( 2.0-4.8大全 )
.net