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

相关推荐
波力海苔夹心脆6752 天前
C# 机器视觉实战:单相机引导机械手精准贴合,从拍照、偏差计算(平移+旋转)到坐标补偿全解
开发语言·经验分享·数码相机·c#·视觉检测·.net
慧都小妮子2 天前
Stimulsoft 2026.4 发布:Markdown 组件、桑基图与 .NET 11 支持
.net·数据可视化·stimulsoft·版本更新·报表控件·桑基图·.net 11
波力海苔夹心脆6753 天前
C# 视觉检测实战:PLC 按钮触发 VisionPro 检测,OK 亮灯 / NG 灭灯(西门子 PLC,含界面显示与图片存档)
开发语言·经验分享·c#·自动化·视觉检测·.net
hez20103 天前
一行代码在 .NET 项目里启用 Satori GC
c#·.net·.net core·gc·clr
QUDONG_biubiubiu3 天前
.NET Runtime 6.0/7.0/8.0/9.0下载修复教程,软件打不开这样解决
.net·net runtime下载·net运行库修复·net runtime安装
宝桥南山3 天前
.NET 11 - 尝试创建一下AGUIServer和AGUIClient(基于新版.NET SDK for AG-UI)
microsoft·ai·微软·c#·aigc·.net
波力海苔夹心脆6753 天前
C# LINQ 入门到上手:一篇讲透查询语法、常用操作符与延迟执行(含示例与速查表
经验分享·c#·.net·solr·linq
小羊没烦恼!4 天前
Windows Azure Platform体验(1):Windows Azure
java·大数据·后端·python·flask·word·.net
CSharp精选营5 天前
IIS 后面挂 Kestrel,这 3 个配置错了就 502
kestrel·.net·iis配置
鑫 源5 天前
C#源码开源, 获取macOS 电池数据,包括iOS (xamarin开发)
c#·.net·xamarin