-
Notifications
You must be signed in to change notification settings - Fork 19
Add ASR pipelines to the demo #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fa58b7a
Add an ASR via upload demo
tonybaloney 2672f08
Add dog image. Extend timeouts for stage
tonybaloney d7f8af7
Use a picture of my dog and make it easier to see from the back
tonybaloney 19abba4
Merge branch 'asr' of https://github.com/tonybaloney/TransformersShar…
tonybaloney 1dad58e
Use native controls for ASR
tonybaloney d2f81c0
Code cleanup
tonybaloney e147549
Trim the result back
tonybaloney d7b6a47
Use a para tag instead
tonybaloney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
Demo/TransformersSharpWebDemo.Web/Components/Pages/Transcribe.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| @page "/transcribe" | ||
| @rendermode InteractiveServer | ||
| @attribute [StreamRendering(true)] | ||
| @attribute [OutputCache(Duration = 5)] | ||
|
|
||
| @inject DemoApiClient demoApi | ||
|
|
||
| <PageTitle>Transcribe Audio</PageTitle> | ||
|
|
||
| <h1>ASR Demo</h1> | ||
|
|
||
| <p>Select a FLAC audio file to transcribe using the backend API service.</p> | ||
|
|
||
| <InputFile OnChange="OnFileSelected" /> | ||
| <button @onclick="UploadFile" disabled="@(!isFileSelected || isUploading)">Transcribe</button> | ||
|
|
||
| @if (isUploading) | ||
| { | ||
| <p><em>Uploading and transcribing...</em></p> | ||
| } | ||
| @if (!string.IsNullOrEmpty(transcription)) | ||
| { | ||
| <h3>Transcription Result:</h3> | ||
| <p>@transcription?.Trim()</p> | ||
| } | ||
| @if (!string.IsNullOrEmpty(error)) | ||
| { | ||
| <p style="color:red">@error</p> | ||
| } | ||
|
|
||
| @code { | ||
| private IBrowserFile? selectedFile; | ||
| private bool isFileSelected = false; | ||
| private bool isUploading = false; | ||
| private string? transcription; | ||
| private string? error; | ||
|
|
||
| private void OnFileSelected(InputFileChangeEventArgs e) | ||
| { | ||
| var file = e.File; | ||
| if (file.ContentType != "audio/flac"){ | ||
| error = "Must be flac"; | ||
| selectedFile = null; | ||
| isFileSelected = false; | ||
| return; | ||
| } | ||
|
|
||
| if (file != null) | ||
| { | ||
| selectedFile = file; | ||
| isFileSelected = true; | ||
| error = null; | ||
| } | ||
| else | ||
| { | ||
| error = "No files selected"; | ||
| selectedFile = null; | ||
| isFileSelected = false; | ||
| } | ||
| } | ||
|
|
||
| private async Task UploadFile() | ||
| { | ||
| if (selectedFile == null) | ||
| { | ||
| error = "Please select a FLAC file."; | ||
| return; | ||
| } | ||
|
|
||
| isUploading = true; | ||
| transcription = null; | ||
| error = null; | ||
|
|
||
| try | ||
| { | ||
|
|
||
| transcription = await demoApi.GetTranscribeAsync(selectedFile); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| error = $"Error: {ex.Message}"; | ||
| } | ||
| finally | ||
| { | ||
| isUploading = false; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,53 @@ | ||||||||||||||||
| using Microsoft.AspNetCore.Components.Forms; | ||||||||||||||||
| using static TransformersSharp.Pipelines.ObjectDetectionPipeline; | ||||||||||||||||
|
|
||||||||||||||||
| namespace TransformersSharpWebDemo.Web; | ||||||||||||||||
|
|
||||||||||||||||
| public class DemoApiClient(HttpClient httpClient) | ||||||||||||||||
| { | ||||||||||||||||
| public async Task<DetectResponse> GetObjectDetectionAsync(string imageUrl, CancellationToken cancellationToken = default) | ||||||||||||||||
| { | ||||||||||||||||
| List<DetectionResult>? detectedObjects = []; | ||||||||||||||||
| var url = imageUrl; | ||||||||||||||||
| DetectRequest detectRequest = new(url); // Replace with actual URL | ||||||||||||||||
| // Extend timeout because this can take a while | ||||||||||||||||
| httpClient.Timeout = TimeSpan.FromMinutes(5); | ||||||||||||||||
| var response = await httpClient.PostAsJsonAsync("/detect", detectRequest, cancellationToken); | ||||||||||||||||
|
Comment on lines
+13
to
+15
|
||||||||||||||||
| // Extend timeout because this can take a while | |
| httpClient.Timeout = TimeSpan.FromMinutes(5); | |
| var response = await httpClient.PostAsJsonAsync("/detect", detectRequest, cancellationToken); | |
| // Use a cancellation token with a timeout for this request | |
| using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); | |
| cts.CancelAfter(TimeSpan.FromMinutes(5)); | |
| var response = await httpClient.PostAsJsonAsync("/detect", detectRequest, cts.Token); |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Inline CSS with hardcoded styles and magic values can hinder maintainability. Consider moving these styles into a CSS class or stylesheet.