Gradio.Net 开发指南 -- 4 种Interface类型
目录4 种Interface类型1) 标准演示Standard demos2) 仅输出演示Output-only demos3) 仅输入演示Input-only demos4) 一体化演示Unified demos总结Gradio.Net (https://github.com/feiyun0112/Gradio.Net)是一个开源的 .NET 库它是 Gradio 的 .NET 移植版本允许你为机器学习模型、API 或任何 C# 函数快速构建演示或 Web 应用程序无需任何 JavaScript、CSS 或 Web 开发经验4 种Interface类型到目前为止我们通常会默认一个 Gradio 演示需要同时有输入和输出。但在真实场景中并不总是这样。例如无条件图像生成模型不接收输入却会输出图像。gr.Interface实际上支持 4 种不同类型的演示标准演示Standard demos有独立的输入和输出例如图像分类、语音转文本。仅输出演示Output-only demos没有输入只有输出例如无条件图像生成。仅输入演示Input-only demos只有输入不产生可视输出例如把上传文件保存到磁盘或数据库。一体化演示Unified demos输入和输出是同一个组件输出会覆盖输入例如文本续写。下面分别看每一种。1) 标准演示Standard demos只要在Interface()中同时设置inputs和outputs即可。using Gradio.Net; using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; string Sepia(string inputImg) { using var image SixLabors.ImageSharp.Image.LoadRgb24(inputImg); for (int y 0; y image.Height; y) { for (int x 0; x image.Width; x) { var pixel image[x, y]; int tr (int)(0.393 * pixel.R 0.769 * pixel.G 0.189 * pixel.B); int tg (int)(0.349 * pixel.R 0.686 * pixel.G 0.168 * pixel.B); int tb (int)(0.272 * pixel.R 0.534 * pixel.G 0.131 * pixel.B); pixel.R (byte)Math.Min(255, tr); pixel.G (byte)Math.Min(255, tg); pixel.B (byte)Math.Min(255, tb); image[x, y] pixel; } } string outputPath Path.Combine(Path.GetTempPath(), $sepia_{Guid.NewGuid()}.png); image.Save(outputPath); return outputPath; } var demo gr.Interface( fn: Sepia, inputs: gr.Image(), outputs: image ); await demo.Launch();2) 仅输出演示Output-only demos在Interface()中将inputs设为null。using Gradio.Net; using Gradio.Net.Components; Liststring FakeGan() { Thread.Sleep(1000); return new Liststring { https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?autoformatfitcropw387q80, https://images.unsplash.com/photo-1542909168-82c3e7fdca5c?autoformatfitcropw1000q80, https://images.unsplash.com/photo-1554151228-14d9def656e4?autoformatfitcropw386q80, https://images.unsplash.com/photo-1542178243-bc20204b769f?autoformatfitcropw800q80 }; } var demo gr.Interface( fn: FakeGan, inputs: null!, outputs: gr.Gallery(label: Generated Images, columns: 2), title: FD-GAN, description: This is a fake demo of a GAN. In reality, the images are randomly chosen from Unsplash. ); await demo.Launch();3) 仅输入演示Input-only demos在Interface()中将outputs设为null。using Gradio.Net; using System.Security.Cryptography; void SaveImageRandomName(string imagePath) { var bytes RandomNumberGenerator.GetBytes(20); var randomString Convert.ToHexString(bytes).ToLowerInvariant(); var outputFile ${randomString}.png; File.Copy(imagePath, outputFile, overwrite: true); Console.WriteLine($Saved image to {outputFile}!); } var demo gr.Interface( fn: SaveImageRandomName, inputs: gr.Image(type: filepath), outputs: null! ); await demo.Launch();4) 一体化演示Unified demos输入和输出使用同一个组件。using Gradio.Net; string GenerateText(string textPrompt) { return ${textPrompt} ...and this is generated continuation.; } var textbox gr.Textbox(); var demo gr.Interface( fn: GenerateText, inputs: textbox, outputs: textbox ); await demo.Launch();如果这 4 种方式都不能满足你的 UI 和数据流需求就该使用gr.Blocks()。总结本章你学习了Interface的 4 种构建模式标准、仅输出、仅输入、统一。它们覆盖了大量常见 ML/Web Demo 场景能让你快速搭建原型。引入地址