diff --git a/Demo/FormDemo.cs b/Demo/FormDemo.cs index 22ee919..d72c7de 100644 --- a/Demo/FormDemo.cs +++ b/Demo/FormDemo.cs @@ -68,7 +68,12 @@ private static async Task update_props(Input input) private static async Task update_symbols(Input input) { + gr.Warning("This is some warning."); + await Task.Delay(1000); + + gr.Info("This is some info."); + var symbols = new[] { "FFIU", "IGEB", "VCIT", "FCOR", "SKOR", "KORP", "LQDI" }; return gr.Output(gr.Dropdown(choices: symbols, interactive: true)); } diff --git a/Gradio.Net.AspNetCore/Gradio.Net.AspNetCore.csproj b/Gradio.Net.AspNetCore/Gradio.Net.AspNetCore.csproj index 4af9332..2df3112 100644 --- a/Gradio.Net.AspNetCore/Gradio.Net.AspNetCore.csproj +++ b/Gradio.Net.AspNetCore/Gradio.Net.AspNetCore.csproj @@ -8,7 +8,7 @@ true snupkg Gradio.Net.AspNetCore - 0.1.3 + 0.1.5 Gradio.Net.AspNetCore feiyun0112 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 应用程序。 diff --git a/Gradio.Net.AspNetCore/GradioServiceExtensions.cs b/Gradio.Net.AspNetCore/GradioServiceExtensions.cs index 8276f89..2ffb5f8 100644 --- a/Gradio.Net.AspNetCore/GradioServiceExtensions.cs +++ b/Gradio.Net.AspNetCore/GradioServiceExtensions.cs @@ -95,7 +95,7 @@ public static WebApplication UseGradio(this WebApplication webApplication } await streamWriter.WriteLineAsync(new CloseStreamMessage().ProcessMsg()); await streamWriter.FlushAsync(); - app.ClonseSession(sessionHash); + app.CloseSession(sessionHash); }); webApplication.MapPost("/upload", async (HttpRequest request, [FromServices] GradioApp app) => diff --git a/Gradio.Net/Context.cs b/Gradio.Net/Context.cs index 0206c39..f31c67c 100644 --- a/Gradio.Net/Context.cs +++ b/Gradio.Net/Context.cs @@ -1,4 +1,5 @@ -using System.Collections.Concurrent; +using Gradio.Net.Models; +using System.Collections.Concurrent; using System.Threading.Channels; @@ -13,6 +14,8 @@ internal static class Context internal static Channel EventChannel { get; private set; } = Channel.CreateUnbounded(); internal static Blocks RootBlock { get; private set; } = null; + internal static readonly Channel LogMessageChannel = Channel.CreateUnbounded(); + private static Blocks _currentBlocks = null; internal static void SetCurrentBlocks(Blocks blocks) { diff --git a/Gradio.Net/Gradio.Net.csproj b/Gradio.Net/Gradio.Net.csproj index d2c0850..e9b520e 100644 --- a/Gradio.Net/Gradio.Net.csproj +++ b/Gradio.Net/Gradio.Net.csproj @@ -8,7 +8,7 @@ true snupkg Gradio.Net - 0.1.4 + 0.1.5 Gradio.Net feiyun0112 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 应用程序。 diff --git a/Gradio.Net/GradioApp.cs b/Gradio.Net/GradioApp.cs index 3b49cc5..00abdfb 100644 --- a/Gradio.Net/GradioApp.cs +++ b/Gradio.Net/GradioApp.cs @@ -122,6 +122,11 @@ public async IAsyncEnumerable QueueData(string sessionHash, Cancella foreach (string pendingEventId in pendingEventIds) { + while (Context.LogMessageChannel.Reader.TryRead(out LogMessage logMessage)) + { + yield return logMessage; + } + if (!Context.EventResults.TryGetValue(pendingEventId, out EventResult eventResult)) { if (heartbeatCount++ > heartbeatRate) @@ -205,6 +210,11 @@ public async IAsyncEnumerable QueueData(string sessionHash, Cancella outputEx = ex; } + while (Context.LogMessageChannel.Reader.TryRead(out LogMessage logMessage)) + { + yield return logMessage; + } + if (outputEx != null) { yield return new UnexpectedErrorMessage(pendingEventId, outputEx.Message); @@ -313,7 +323,7 @@ public async IAsyncEnumerable QueueData(string sessionHash, Cancella } } - public List? ClonseSession(string sessionHash) + public List? CloseSession(string sessionHash) { Context.PendingEventIdsSession.TryRemove(sessionHash, out List? tmpIds); return tmpIds; diff --git a/Gradio.Net/Models/SSEMessage.cs b/Gradio.Net/Models/SSEMessage.cs index e4ee9d2..8671064 100644 --- a/Gradio.Net/Models/SSEMessage.cs +++ b/Gradio.Net/Models/SSEMessage.cs @@ -2,8 +2,9 @@ using Gradio.Net.Helpers; using System.Text.Json.Serialization; -namespace Gradio.Net.Models; +namespace Gradio.Net; +[JsonDerivedType(typeof(LogMessage))] [JsonDerivedType(typeof(DoneMessage))] [JsonDerivedType(typeof(ProcessCompletedMessage))] [JsonDerivedType(typeof(UnexpectedErrorMessage))] @@ -32,6 +33,32 @@ public string ProcessMsg() } } +public class LogMessage : SSEMessage +{ + private LogMessage() : base(SSEMessageType.Log, null, null) { } + public string Log { get; set; } + public string Level { get; set; } + + + internal static LogMessage Info(string message) + { + return new LogMessage() + { + Log = message, + Level = "info", + }; + } + + internal static LogMessage Warning(string message) + { + return new LogMessage() + { + Log = message, + Level = "warning", + }; + } +} + public class DoneMessage : SSEMessage { diff --git a/Gradio.Net/_gr/gr_Helpers.cs b/Gradio.Net/_gr/gr_Helpers.cs index 917eaf8..d936ab5 100644 --- a/Gradio.Net/_gr/gr_Helpers.cs +++ b/Gradio.Net/_gr/gr_Helpers.cs @@ -10,4 +10,13 @@ public static Progress Progress(int total) { return new Progress(total); } + + public static void Info(string message) + { + Context.LogMessageChannel.Writer.TryWrite(LogMessage.Info( message)); + } + public static void Warning(string message) + { + Context.LogMessageChannel.Writer.TryWrite(LogMessage.Warning(message)); + } }