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中代码如下。

第一种代码。

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;
                }
            }
        }
    }
}

第二种代码。

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. Semaphore简单使用

    2024-06-11 11:02:02       32 阅读
  2. 过滤器简单使用

    2024-06-11 11:02:02       42 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-11 11:02:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-11 11:02:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-11 11:02:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-11 11:02:02       18 阅读

热门阅读

  1. 数据仓库技术及应用(Hive调优)

    2024-06-11 11:02:02       8 阅读
  2. 现代 C++的高效并发编程模式

    2024-06-11 11:02:02       8 阅读
  3. 2024.6.10刷题记录

    2024-06-11 11:02:02       11 阅读
  4. 三分的空间至关重要

    2024-06-11 11:02:02       6 阅读
  5. 【烟花game】

    2024-06-11 11:02:02       10 阅读
  6. 【DevOps】什么是 pfSense?免费构建SDWAN

    2024-06-11 11:02:02       12 阅读
  7. MATLAB入门教程

    2024-06-11 11:02:02       10 阅读
  8. PHP的基础代码

    2024-06-11 11:02:02       9 阅读