Skip to content

Commit

Permalink
✨ add support for generated documents (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianardee authored Mar 26, 2024
1 parent 32df61c commit 2360c0f
Show file tree
Hide file tree
Showing 37 changed files with 1,359 additions and 265 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v2
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v2
uses: actions/setup-dotnet@v4
with:
dotnet-version: '7.0.x'

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish-nuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ jobs:

steps:
- name: Check out Git repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v2
uses: actions/setup-dotnet@v4
with:
dotnet-version: '7.0.x'
- name: Install dependencies
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-sample-codes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ jobs:
- "net8.0"
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Set up .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: "8.0.x"

Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ jobs:
- "net8.0"
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Set up .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: "8.0.x"

Expand Down Expand Up @@ -59,12 +59,12 @@ jobs:
- "net8.0"
runs-on: "windows-latest"
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Set up .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: "8.0.x"

Expand Down
6 changes: 4 additions & 2 deletions docs/code_samples/default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using Mindee;
using Mindee.Input;
using Mindee.Http;
using Mindee.Parsing;
using Mindee.Product.Generated;

string apiKey = "my-api-key";
string filePath = "/path/to/the/file.ext";
Expand All @@ -14,14 +15,15 @@ MindeeClient mindeeClient = new MindeeClient(apiKey);
var inputSource = new LocalInputSource(filePath);

// Set the endpoint configuration
CustomEndpoint myEndpoint = new CustomEndpoint(
CustomEndpoint endpoint = new CustomEndpoint(
endpointName: "my-endpoint",
accountName: "my-account",
version: "my-version"
);

// Call the API and parse the input
var response = await mindeeClient
.ParseAsync(inputSource, myEndpoint);
.ParseAsync<GeneratedV1>(inputSource, endpoint);

// Print a summary of the predictions
System.Console.WriteLine(response.Document.ToString());
Expand Down
31 changes: 31 additions & 0 deletions docs/code_samples/default_async.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Mindee;
using Mindee.Input;
using Mindee.Http;
using Mindee.Product.Generated;

string apiKey = "my-api-key";
string filePath = "/path/to/the/file.ext";

// Construct a new client
MindeeClient mindeeClient = new MindeeClient(apiKey);

// Load an input source as a path string
// Other input types can be used, as mentioned in the docs
var inputSource = new LocalInputSource(filePath);

// Set the endpoint configuration
CustomEndpoint endpoint = new CustomEndpoint(
endpointName: "my-endpoint",
accountName: "my-account",
version: "my-version"
);

// Call the product asynchronously with auto-polling
var response = await mindeeClient
.EnqueueAndParseAsync<GeneratedV1>(inputSource, endpoint);

// Print a summary of all the predictions
System.Console.WriteLine(response.Document.ToString());

// Print only the document-level predictions
// System.Console.WriteLine(response.Document.Inference.Prediction.ToString());
15 changes: 4 additions & 11 deletions src/Mindee/Geometry/Polygon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ public Polygon(List<List<double>> coordinates)
foreach (var point in coordinates)
{
if (point.Count != 2)
{
throw new InvalidOperationException("A point must have 2 coordinates.");
}

Add(new Point(point.First(), point.Last()));
}
}
Expand Down Expand Up @@ -96,15 +95,9 @@ public bool IsPointInY(Point point)
/// </summary>
public override string ToString()
{
StringBuilder result = new StringBuilder("[\n");
for (int i = 0; i < this.Count; i++)
{
result.Append($" {this[i]}");
if (i >= this.Count - 1) result.Append('\n');
else result.Append(",\n");
}
result.Append(']');
return result.ToString();
if (this.Count > 0)
return $"Polygon with {this.Count} points.";
return "";
}
}
}
38 changes: 37 additions & 1 deletion src/Mindee/Http/CustomEndpoint.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

namespace Mindee.Http
{
/// <summary>
Expand All @@ -22,7 +24,7 @@ public sealed class CustomEndpoint
public string AccountName { get; }

/// <summary>
///
/// Default constructor.
/// </summary>
/// <param name="endpointName">The name of the product associated to the expected model.</param>
/// <param name="accountName">The name of the account which owns the API. Useful when using custom builder.</param>
Expand All @@ -36,5 +38,39 @@ string endpointName
AccountName = accountName;
Version = version;
}

/// <summary>
/// Get the base URL for the endpoint.
/// </summary>
/// <returns></returns>
public string GetBaseUrl()
{
return $"{AccountName}/{EndpointName}/v{Version}";
}

/// <summary>
/// Get an endpoint for a given prediction model.
/// </summary>
/// <typeparam name="TModel"></typeparam>
/// <returns></returns>
/// <exception cref="NotSupportedException"></exception>
public static CustomEndpoint GetEndpoint<TModel>()
{
if (!Attribute.IsDefined(typeof(TModel), typeof(EndpointAttribute)))
{
throw new NotSupportedException(
$"The type {typeof(TModel)} is not supported as a prediction model. " +
"The endpoint attribute is missing. " +
"Please refer to the documentation or contact support.");
}

EndpointAttribute endpointAttribute = (EndpointAttribute)Attribute.GetCustomAttribute(
element: typeof(TModel), typeof(EndpointAttribute));

return new CustomEndpoint(
endpointAttribute.EndpointName,
endpointAttribute.AccountName,
endpointAttribute.Version);
}
}
}
30 changes: 16 additions & 14 deletions src/Mindee/Http/IHttpApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,44 @@
namespace Mindee.Http
{
/// <summary>
/// Make predictions and returned a result of it.
/// Communicate with the Mindee API.
/// <p>
/// You may use this interface to make your own custom class.
/// However, we may introduce breaking changes in minor versions as needed.
/// </p>
/// </summary>
public interface IHttpApi
{
/// <summary>
/// Do a prediction according parameters.
/// </summary>
/// <typeparam name="TModel">Result expected type.</typeparam>
/// <param name="predictParameter"><see cref="PredictParameter"/></param>
Task<PredictResponse<TModel>> PredictPostAsync<TModel>(
PredictParameter predictParameter)
where TModel : class, new();

/// <summary>
/// Do a prediction according parameters for custom model defined in the Studio.
/// </summary>
/// <typeparam name="TModel">Result expected type.</typeparam>
/// <param name="endpoint"><see cref="CustomEndpoint"/></param>
/// <param name="predictParameter"><see cref="PredictParameter"/></param>
Task<PredictResponse<TModel>> PredictPostAsync<TModel>(
CustomEndpoint endpoint,
PredictParameter predictParameter)
PredictParameter predictParameter
, CustomEndpoint endpoint = null)
where TModel : class, new();

/// <summary>
/// Enqueue a prediction according parameters.
/// </summary>
/// <param name="predictParameter"><see cref="PredictParameter"/></param>
/// <param name="endpoint"><see cref="CustomEndpoint"/></param>
/// <typeparam name="TModel">Document type.</typeparam>
Task<AsyncPredictResponse<TModel>> PredictAsyncPostAsync<TModel>(PredictParameter predictParameter)
Task<AsyncPredictResponse<TModel>> PredictAsyncPostAsync<TModel>(
PredictParameter predictParameter
, CustomEndpoint endpoint = null)
where TModel : class, new();

/// <summary>
/// Get a document which was predicted.
/// </summary>
Task<AsyncPredictResponse<TModel>> DocumentQueueGetAsync<TModel>(string jobId)
/// <param name="jobId">The job ID as returned by the predict_async route.</param>
/// <param name="endpoint"><see cref="CustomEndpoint"/></param>
Task<AsyncPredictResponse<TModel>> DocumentQueueGetAsync<TModel>(
string jobId
, CustomEndpoint endpoint = null)
where TModel : class, new();
}
}
Loading

0 comments on commit 2360c0f

Please sign in to comment.