Skip to content

Commit

Permalink
0.0.9 audio and video
Browse files Browse the repository at this point in the history
  • Loading branch information
feiyun0112 committed Jun 5, 2024
1 parent df59f7e commit 73dcb2e
Show file tree
Hide file tree
Showing 306 changed files with 2,156 additions and 665 deletions.
2 changes: 1 addition & 1 deletion Gradio.Net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "readme_files", "readme_file
ProjectSection(SolutionItems) = preProject
readme_files\chatbot_demo.md = readme_files\chatbot_demo.md
readme_files\form_demo.md = readme_files\form_demo.md
readme_files\image_demo.md = readme_files\image_demo.md
readme_files\media_demo.md = readme_files\media_demo.md
readme_files\layout_demo.md = readme_files\layout_demo.md
readme_files\progress_demo.md = readme_files\progress_demo.md
readme_files\README_zh-cn.md = readme_files\README_zh-cn.md
Expand Down
273 changes: 171 additions & 102 deletions Gradio.Net/App.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Gradio.Net.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System.Collections.Concurrent;

namespace Gradio.Net;

public class App
{
const string GRADIO_VERSION = "4.29.0";
const string GRADIO_VERSION = "4.32.2";
private readonly long _appId;

public static void Launch(Blocks blocks, Action<GradioServiceConfig>? additionalConfigurationAction = null, params string[] args)
Expand All @@ -32,6 +33,8 @@ internal Dictionary<string, object> GetConfig(HttpRequest httpRequest)
{
Dictionary<string, object> result = Context.RootBlock.GetConfig();

result["stylesheets"] = this._gradioServiceConfig.Stylesheets;
result["body_css"] =this._gradioServiceConfig.BodyCss;
result["root"] = httpRequest.GetRootUrl();
result["version"] = GRADIO_VERSION;
result["app_id"] = _appId;
Expand Down Expand Up @@ -65,165 +68,231 @@ internal async Task<QueueJoinOut> QueueJoin(HttpRequest request, PredictBodyIn b
};
eventIn.Data = body;

lock (Context.PendingMessageLock)
{
if (!Context.PendingEventIdsSession.TryGetValue(eventIn.SessionHash, out List<string> pendingEventIds))
{
pendingEventIds = new List<string>();
}
pendingEventIds.Add(eventIn.ToString());
Context.PendingEventIdsSession[eventIn.SessionHash]=pendingEventIds;
}

await Context.EventChannel.Writer.WriteAsync(eventIn);

return new QueueJoinOut { EventId = eventIn.Id };
}

internal async IAsyncEnumerable<SSEMessage> QueueData(string sessionHash, CancellationToken stoppingToken)
{


const int heartbeatRate = 150;
const int checkRate = 50;
int heartbeatCount = 0;

if (string.IsNullOrEmpty(sessionHash))
{
yield return new UnexpectedErrorMessage("Session not found");
yield return new UnexpectedErrorMessage(null,"Session not found");

yield break;
}

const int heartbeatRate = 15;
const int checkRate = 500;
EventResult eventResult = null;
int heartbeatCount = 0;
while (!stoppingToken.IsCancellationRequested)
while (!Context.PendingEventIdsSession.TryGetValue(sessionHash, out _))
{
await Task.Delay(checkRate);
if (Context.EventResults.TryGetValue(sessionHash, out eventResult))
{
Context.EventResults.Remove(sessionHash, out _);
break;
}

if (heartbeatCount++ > heartbeatRate)
{
break;
yield return new UnexpectedErrorMessage(null, "Session not found");

yield break;
}

yield return new HeartbeatMessage();
await Task.Delay(heartbeatRate);
}

if (eventResult == null || (eventResult.OutputTask==null && eventResult.StreamingOutputTask == null))
heartbeatCount = 0;
while (!stoppingToken.IsCancellationRequested)
{
yield return new UnexpectedErrorMessage("no task for Session");
string[] pendingEventIds = null;
lock (Context.PendingMessageLock)
{
if (!Context.PendingEventIdsSession.TryGetValue(sessionHash, out List<string> tmpIds))
{
yield break;
}

yield break;
}
pendingEventIds = tmpIds.ToArray();
}

if (eventResult.OutputTask != null)
{
while (!stoppingToken.IsCancellationRequested)
if (pendingEventIds == null || pendingEventIds.Length == 0)
{
yield break;
}

foreach (var pendingEventId in pendingEventIds)
{
while (!eventResult.OutputTask.IsCompleted)
if (!Context.EventResults.TryGetValue(pendingEventId, out EventResult eventResult))
{
if (eventResult.Input.Progress != null)
{
yield return new ProgressMessage(eventResult.Event.Id, eventResult.Input.Progress);
}
else
if (heartbeatCount++ > heartbeatRate)
{
yield return new HeartbeatMessage();
yield return new UnexpectedErrorMessage(pendingEventId,"no task for Session");

RemovePendingEvent(sessionHash,pendingEventId);
continue;
}
await Task.Delay(checkRate);
yield return new HeartbeatMessage();
await Task.Delay(heartbeatRate);
continue;
}

break;
}

if (eventResult.OutputTask.Exception != null)
{
Exception ex = eventResult.OutputTask.Exception;
while (ex.InnerException != null)
if (eventResult == null || (eventResult.OutputTask == null && eventResult.StreamingOutputTask == null))
{
ex = ex.InnerException;
yield return new UnexpectedErrorMessage(pendingEventId,"no task for Session");

RemovePendingEvent(sessionHash, pendingEventId);
continue;
}

yield return new UnexpectedErrorMessage(ex.Message);
if (eventResult.OutputTask != null)
{
while (!stoppingToken.IsCancellationRequested)
{
while (!eventResult.OutputTask.IsCompleted)
{
if (eventResult.Input.Progress != null)
{
yield return new ProgressMessage(eventResult.Event.Id, eventResult.Input.Progress);
}
else
{
yield return new HeartbeatMessage();
}
await Task.Delay(checkRate);
}

yield break;
}
break;
}

if (stoppingToken.IsCancellationRequested || !eventResult.OutputTask.IsCompletedSuccessfully)
{
yield return new UnexpectedErrorMessage("Task Cancelled");
if (eventResult.OutputTask.Exception != null)
{
Exception ex = eventResult.OutputTask.Exception;
while (ex.InnerException != null)
{
ex = ex.InnerException;
}

yield break;
}
Output result = eventResult.OutputTask.Result;
yield return new UnexpectedErrorMessage(pendingEventId, ex.Message);

if (result is ErrorOutput error)
{
yield return new UnexpectedErrorMessage(error.Exception.Message);
RemovePendingEvent(sessionHash, pendingEventId);
continue;
}

yield break;
}
if (stoppingToken.IsCancellationRequested || !eventResult.OutputTask.IsCompletedSuccessfully)
{
yield return new UnexpectedErrorMessage(pendingEventId, "Task Cancelled");

object[] data = result.Data;
RemovePendingEvent(sessionHash, pendingEventId);
continue;
}
Output result = eventResult.OutputTask.Result;

yield return new ProcessCompletedMessage(eventResult.Event.Id, new Dictionary<string, object> {
{ "data",gr.Output(eventResult,data)}
});
yield break;
}
else if (eventResult.StreamingOutputTask != null)
{
yield return new ProcessStartsMessage(eventResult.Event.Id);
if (result is ErrorOutput error)
{
yield return new UnexpectedErrorMessage(pendingEventId, error.Exception.Message);

object[] result=null;
await foreach (Output output in eventResult.StreamingOutputTask)
{
if (stoppingToken.IsCancellationRequested)
{
yield return new UnexpectedErrorMessage("Task Cancelled");
RemovePendingEvent(sessionHash, pendingEventId);
continue;
}

yield break;
}
object[] data = result.Data;

object[] outputData = output.Data;
if (result == null)
{
result = gr.Output(eventResult, outputData);
yield return new ProcessGeneratingMessage(eventResult.Event.Id, new Dictionary<string, object> {
{ "data",result}
yield return new ProcessCompletedMessage(eventResult.Event.Id, new Dictionary<string, object> {
{ "data",gr.Output(eventResult,data)}
});
RemovePendingEvent(sessionHash, pendingEventId);
continue;
}
else
else if (eventResult.StreamingOutputTask != null)
{
object[] oldResult = result;
result = gr.Output(eventResult, outputData);
object[] diffResult = new object[result.Length];
for (int i = 0; i < result.Length; i++)
yield return new ProcessStartsMessage(eventResult.Event.Id);

object[] result = null;
await foreach (Output output in eventResult.StreamingOutputTask)
{
if (oldResult[i] != null && result[i] != null && oldResult[i] is string oldStr && result[i] is string str && oldStr==str)
if (stoppingToken.IsCancellationRequested)
{
yield return new UnexpectedErrorMessage(pendingEventId, "Task Cancelled");

RemovePendingEvent(sessionHash, pendingEventId);
continue;
}

object[] outputData = output.Data;
if (result == null)
{
diffResult[i] = Array.Empty<object>();
result = gr.Output(eventResult, outputData);
yield return new ProcessGeneratingMessage(eventResult.Event.Id, new Dictionary<string, object> {
{ "data",result}
});
}
else
{
diffResult[i] = new object[] { new List<object> { "replace", Array.Empty<object>(), result[i] } };
object[] oldResult = result;
result = gr.Output(eventResult, outputData);
object[] diffResult = new object[result.Length];
for (int i = 0; i < result.Length; i++)
{
if (oldResult[i] != null && result[i] != null && oldResult[i] is string oldStr && result[i] is string str && oldStr == str)
{
diffResult[i] = Array.Empty<object>();
}
else
{
diffResult[i] = new object[] { new List<object> { "replace", Array.Empty<object>(), result[i] } };
}

}
yield return new ProcessGeneratingMessage(eventResult.Event.Id, new Dictionary<string, object> {
{ "data", diffResult}
});
}

}
yield return new ProcessGeneratingMessage(eventResult.Event.Id, new Dictionary<string, object> {
{ "data", diffResult}
if (result == null)
{
yield return new UnexpectedErrorMessage(pendingEventId, "Task Cancelled");

RemovePendingEvent(sessionHash, pendingEventId);
continue;
}


yield return new ProcessCompletedMessage(eventResult.Event.Id, new Dictionary<string, object> {
{ "data",result}
});
RemovePendingEvent(sessionHash, pendingEventId);
continue;
}
}
if (result == null)
{
yield return new UnexpectedErrorMessage("Task Cancelled");
else
{
yield return new UnexpectedErrorMessage(pendingEventId, "no task for Session");

yield break;
RemovePendingEvent(sessionHash, pendingEventId);
continue;
}
}


yield return new ProcessCompletedMessage(eventResult.Event.Id, new Dictionary<string, object> {
{ "data",result}
});
yield break;
}
else
}

private void RemovePendingEvent(string sessionHash, string pendingEventId)
{
lock (Context.PendingMessageLock)
{
yield return new UnexpectedErrorMessage("no task for Session");
if (!Context.PendingEventIdsSession.TryGetValue(sessionHash, out List<string> tmpIds))
{
return;
}

yield break;
tmpIds.Remove(pendingEventId);
}
}

Expand Down Expand Up @@ -272,7 +341,7 @@ internal async Task<List<string>> Upload(string uploadId, IFormFileCollection fi
throw new ArgumentException($"File not allowed: {pathOrUrl}.");
}

Context.DownloadableFiles.Remove(filePath, out _);
//Context.DownloadableFiles.Remove(filePath, out _);



Expand Down
10 changes: 5 additions & 5 deletions Gradio.Net/BlockFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ internal Dictionary<string, object> GetConfig()
["backend_fn"] = (this.Fn != null || this.StreamingFn != null),
["js"] = this.Js,
["queue"] = this.Queue,
["apiName"] = ApiName,
["scrollToOutput"] = this.ScrollToOutput,
["showProgress"] = ShowProgress.ToString().ToLowerInvariant(),
["api_name"] = ApiName,
["scroll_to_output"] = this.ScrollToOutput,
["show_progress"] = ShowProgress.ToString().ToLowerInvariant(),
["every"] = this.Every,
["batch"] = this.Batch,
["maxBatchSize"] = MaxBatchSize,
["max_batch_size"] = MaxBatchSize,
["cancels"] = this.Cancels,
["types"] = new Dictionary<string, object> {
{"continuous",this.TypesContinuous },
Expand All @@ -59,7 +59,7 @@ internal Dictionary<string, object> GetConfig()
["collects_event_data"] = this.CollectsEventData,
["trigger_after"] = this.TriggerAfter,
["trigger_only_on_success"] = this.TriggerOnlyOnSuccess,
["triggerMode"] = TriggerMode.ToString().ToLowerInvariant(),
["trigger_mode"] = TriggerMode.ToString().ToSnakeCase(),
["show_api"] = this.ShowApi,
["zerogpu"] = this.ZeroGpu
};
Expand Down
Loading

0 comments on commit 73dcb2e

Please sign in to comment.