Skip to content

Commit

Permalink
0.1.5 gr.Info gr.Warning Help Methods
Browse files Browse the repository at this point in the history
  • Loading branch information
feiyun0112 committed Jun 25, 2024
1 parent f7919c5 commit ddc3404
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 6 deletions.
5 changes: 5 additions & 0 deletions Demo/FormDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ private static async Task<Output> update_props(Input input)

private static async Task<Output> 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));
}
Expand Down
2 changes: 1 addition & 1 deletion Gradio.Net.AspNetCore/Gradio.Net.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageId>Gradio.Net.AspNetCore</PackageId>
<Version>0.1.3</Version>
<Version>0.1.5</Version>
<Title>Gradio.Net.AspNetCore</Title>
<Authors>feiyun0112</Authors>
<Description>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 应用程序。</Description>
Expand Down
2 changes: 1 addition & 1 deletion Gradio.Net.AspNetCore/GradioServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
5 changes: 4 additions & 1 deletion Gradio.Net/Context.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using Gradio.Net.Models;
using System.Collections.Concurrent;
using System.Threading.Channels;


Expand All @@ -13,6 +14,8 @@ internal static class Context
internal static Channel<Event> EventChannel { get; private set; } = Channel.CreateUnbounded<Event>();
internal static Blocks RootBlock { get; private set; } = null;

internal static readonly Channel<LogMessage> LogMessageChannel = Channel.CreateUnbounded<LogMessage>();

private static Blocks _currentBlocks = null;
internal static void SetCurrentBlocks(Blocks blocks)
{
Expand Down
2 changes: 1 addition & 1 deletion Gradio.Net/Gradio.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageId>Gradio.Net</PackageId>
<Version>0.1.4</Version>
<Version>0.1.5</Version>
<Title>Gradio.Net</Title>
<Authors>feiyun0112</Authors>
<Description>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 应用程序。</Description>
Expand Down
12 changes: 11 additions & 1 deletion Gradio.Net/GradioApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ public async IAsyncEnumerable<SSEMessage> 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)
Expand Down Expand Up @@ -205,6 +210,11 @@ public async IAsyncEnumerable<SSEMessage> 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);
Expand Down Expand Up @@ -313,7 +323,7 @@ public async IAsyncEnumerable<SSEMessage> QueueData(string sessionHash, Cancella
}
}

public List<string>? ClonseSession(string sessionHash)
public List<string>? CloseSession(string sessionHash)
{
Context.PendingEventIdsSession.TryRemove(sessionHash, out List<string>? tmpIds);
return tmpIds;
Expand Down
29 changes: 28 additions & 1 deletion Gradio.Net/Models/SSEMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down Expand Up @@ -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
{
Expand Down
9 changes: 9 additions & 0 deletions Gradio.Net/_gr/gr_Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit ddc3404

Please sign in to comment.