Skip to content

Commit

Permalink
feat: Updated to support easy image generation for FluxPro.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Aug 24, 2024
1 parent badb517 commit 49c9d64
Show file tree
Hide file tree
Showing 12 changed files with 613 additions and 53 deletions.
118 changes: 112 additions & 6 deletions src/helpers/FixOpenApiSpec/Program.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,90 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.OpenApi;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Extensions;using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;

var path = args[0];
var text = await File.ReadAllTextAsync(path);

text = text
.Replace("\"openapi\":\"3.1.0\"", "\"openapi\":\"3.0.1\"")
.Replace("\"openapi\":\"3.1.0\"", "\"openapi\":\"3.0.1\"")
;

var openApiDocument = new OpenApiStringReader().Read(text, out var diagnostics);

//openApiDocument.Components.Schemas["CreateEmbeddingRequest"]!.Properties["dimensions"].Nullable = true;

openApiDocument.Components.Schemas["prediction_response"] = FromJson(
/* language=json */
"""
{
"completed_at": "2024-08-24T11:52:04.150854Z",
"created_at": "2024-08-24T11:51:46.577000Z",
"data_removed": false,
"error": null,
"id": "0ppyrp3aj5rge0chggxb4szz48",
"input": {
"seed": 321972,
"steps": 25,
"prompt": "a female, slavian, young adult, fit body, wavy acid orange hair, wearing open swimsuit, sea in the background.",
"guidance": 3.5,
"interval": 3,
"aspect_ratio": "16:9",
"safety_tolerance": 5
},
"logs": "Using seed: 321972\nRunning prediction... \nGenerating image...",
"metrics": {
"image_count": 1,
"predict_time": 17.565933287,
"total_time": 17.573854
},
"output": "https://replicate.delivery/czjl/UVvZ7pAzOk7zLlZhKB2nUx9veCCVSDk4VlfwJ7KxaDmkt3VTA/output.webp",
"started_at": "2024-08-24T11:51:46.584921Z",
"status": "succeeded",
"urls": {
"get": "https://api.replicate.com/v1/predictions/0ppyrp3aj5rge0chggxb4szz48",
"cancel": "https://api.replicate.com/v1/predictions/0ppyrp3aj5rge0chggxb4szz48/cancel"
},
"version": "d848511ad960c3a099e2a5b04f783ebf8e8a44c625b8fa7d8f03b72ebee1c954"
}
""");
openApiDocument.Paths["/models/{model_owner}/{model_name}/predictions"]
.Operations[OperationType.Post].Responses["200"] = new OpenApiResponse
{
Description = "Successful",
Content = new Dictionary<string, OpenApiMediaType>
{
["application/json"] = new()
{
Schema = new OpenApiSchema
{
Reference = new OpenApiReference
{
Type = ReferenceType.Schema,
Id = "prediction_response",
},
},
},
},
};
openApiDocument.Paths["/predictions/{prediction_id}"]
.Operations[OperationType.Get].Responses["200"] = new OpenApiResponse
{
Description = "Successful",
Content = new Dictionary<string, OpenApiMediaType>
{
["application/json"] = new()
{
Schema = new OpenApiSchema
{
Reference = new OpenApiReference
{
Type = ReferenceType.Schema,
Id = "prediction_response",
},
},
},
},
};

text = openApiDocument.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0);
_ = new OpenApiStringReader().Read(text, out diagnostics);

Expand All @@ -23,8 +94,43 @@
{
Console.WriteLine(error.Message);
}

// Return Exit code 1
Environment.Exit(1);
}

await File.WriteAllTextAsync(path, text);
await File.WriteAllTextAsync(path, text);

static OpenApiSchema FromJson([StringSyntax(StringSyntaxAttribute.Json)] string json)
{
var schema = new OpenApiSchema();

var element = System.Text.Json.JsonDocument.Parse(json).RootElement;
schema.Type = element.ValueKind switch
{
System.Text.Json.JsonValueKind.Array => "array",
System.Text.Json.JsonValueKind.False => "boolean",
System.Text.Json.JsonValueKind.True => "boolean",
System.Text.Json.JsonValueKind.Number => "number",
System.Text.Json.JsonValueKind.String => "string",
System.Text.Json.JsonValueKind.Object => "object",
System.Text.Json.JsonValueKind.Null => "null",
_ => throw new NotSupportedException(),
};
schema.Format = element.ValueKind switch
{
System.Text.Json.JsonValueKind.Number => element.TryGetInt64(out var _) ? "int64" : "number",
System.Text.Json.JsonValueKind.String => "string",
_ => null,
};
schema.Properties = new Dictionary<string, OpenApiSchema>();
if (element.ValueKind == System.Text.Json.JsonValueKind.Object)
{
foreach (var property in element.EnumerateObject())
{
schema.Properties[property.Name] = FromJson(property.Value.GetRawText());
}
}

return schema;
}
8 changes: 8 additions & 0 deletions src/helpers/FixOpenApiSpec/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"Localizer": {
"commandName": "Project",
"commandLineArgs": "../../../../../../src/libs/Replicate/openapi.yaml"
}
}
}
90 changes: 57 additions & 33 deletions src/libs/Replicate/Generated/JsonSerializerContextTypes.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,134 +69,158 @@ public sealed partial class JsonSerializerContextTypes
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsCreateRequest? Type14 { get; set; }
public global::Replicate.PredictionResponse? Type14 { get; set; }
/// <summary>
///
/// </summary>
public int? Type15 { get; set; }
public object? Type15 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsUpdateRequest? Type16 { get; set; }
public global::Replicate.PredictionResponseInput? Type16 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.ModelsCreateRequest? Type17 { get; set; }
public double? Type17 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.ModelsCreateRequestVisibility? Type18 { get; set; }
public global::Replicate.PredictionResponseMetrics? Type18 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.AccountGetResponse? Type19 { get; set; }
public global::Replicate.PredictionResponseUrls? Type19 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.AccountGetResponseType? Type20 { get; set; }
public global::Replicate.DeploymentsCreateRequest? Type20 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsListResponse? Type21 { get; set; }
public int? Type21 { get; set; }
/// <summary>
///
/// </summary>
public global::System.Collections.Generic.IList<global::Replicate.DeploymentsListResponseResult>? Type22 { get; set; }
public global::Replicate.DeploymentsUpdateRequest? Type22 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsListResponseResult? Type23 { get; set; }
public global::Replicate.ModelsCreateRequest? Type23 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsListResponseResultCurrentRelease? Type24 { get; set; }
public global::Replicate.ModelsCreateRequestVisibility? Type24 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsListResponseResultCurrentReleaseConfiguration? Type25 { get; set; }
public global::Replicate.AccountGetResponse? Type25 { get; set; }
/// <summary>
///
/// </summary>
public global::System.DateTime? Type26 { get; set; }
public global::Replicate.AccountGetResponseType? Type26 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsListResponseResultCurrentReleaseCreatedBy? Type27 { get; set; }
public global::Replicate.DeploymentsListResponse? Type27 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsListResponseResultCurrentReleaseCreatedByType? Type28 { get; set; }
public global::System.Collections.Generic.IList<global::Replicate.DeploymentsListResponseResult>? Type28 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsCreateResponse? Type29 { get; set; }
public global::Replicate.DeploymentsListResponseResult? Type29 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsCreateResponseCurrentRelease? Type30 { get; set; }
public global::Replicate.DeploymentsListResponseResultCurrentRelease? Type30 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsCreateResponseCurrentReleaseConfiguration? Type31 { get; set; }
public global::Replicate.DeploymentsListResponseResultCurrentReleaseConfiguration? Type31 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsCreateResponseCurrentReleaseCreatedBy? Type32 { get; set; }
public global::System.DateTime? Type32 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsCreateResponseCurrentReleaseCreatedByType? Type33 { get; set; }
public global::Replicate.DeploymentsListResponseResultCurrentReleaseCreatedBy? Type33 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsGetResponse? Type34 { get; set; }
public global::Replicate.DeploymentsListResponseResultCurrentReleaseCreatedByType? Type34 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsGetResponseCurrentRelease? Type35 { get; set; }
public global::Replicate.DeploymentsCreateResponse? Type35 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsGetResponseCurrentReleaseConfiguration? Type36 { get; set; }
public global::Replicate.DeploymentsCreateResponseCurrentRelease? Type36 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsGetResponseCurrentReleaseCreatedBy? Type37 { get; set; }
public global::Replicate.DeploymentsCreateResponseCurrentReleaseConfiguration? Type37 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsGetResponseCurrentReleaseCreatedByType? Type38 { get; set; }
public global::Replicate.DeploymentsCreateResponseCurrentReleaseCreatedBy? Type38 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsUpdateResponse? Type39 { get; set; }
public global::Replicate.DeploymentsCreateResponseCurrentReleaseCreatedByType? Type39 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsUpdateResponseCurrentRelease? Type40 { get; set; }
public global::Replicate.DeploymentsGetResponse? Type40 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsUpdateResponseCurrentReleaseConfiguration? Type41 { get; set; }
public global::Replicate.DeploymentsGetResponseCurrentRelease? Type41 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsUpdateResponseCurrentReleaseCreatedBy? Type42 { get; set; }
public global::Replicate.DeploymentsGetResponseCurrentReleaseConfiguration? Type42 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsUpdateResponseCurrentReleaseCreatedByType? Type43 { get; set; }
public global::Replicate.DeploymentsGetResponseCurrentReleaseCreatedBy? Type43 { get; set; }
/// <summary>
///
/// </summary>
public global::System.Collections.Generic.IList<global::Replicate.HardwareListResponseItem>? Type44 { get; set; }
public global::Replicate.DeploymentsGetResponseCurrentReleaseCreatedByType? Type44 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.HardwareListResponseItem? Type45 { get; set; }
public global::Replicate.DeploymentsUpdateResponse? Type45 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.WebhooksDefaultSecretGetResponse? Type46 { get; set; }
public global::Replicate.DeploymentsUpdateResponseCurrentRelease? Type46 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsUpdateResponseCurrentReleaseConfiguration? Type47 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsUpdateResponseCurrentReleaseCreatedBy? Type48 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.DeploymentsUpdateResponseCurrentReleaseCreatedByType? Type49 { get; set; }
/// <summary>
///
/// </summary>
public global::System.Collections.Generic.IList<global::Replicate.HardwareListResponseItem>? Type50 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.HardwareListResponseItem? Type51 { get; set; }
/// <summary>
///
/// </summary>
public global::Replicate.WebhooksDefaultSecretGetResponse? Type52 { get; set; }
}
}
Loading

0 comments on commit 49c9d64

Please sign in to comment.