Skip to content

Commit d24c657

Browse files
committed
feat: add wanx background image generation api support
1 parent 96ba00d commit d24c657

26 files changed

+754
-1
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// The input of background generation task.
5+
/// </summary>
6+
public class BackgroundGenerationInput
7+
{
8+
/// <summary>
9+
/// The image url to generation background on.
10+
/// </summary>
11+
public required string BaseImageUrl { get; set; }
12+
13+
/// <summary>
14+
/// The reference image url for.
15+
/// </summary>
16+
public string? RefImageUrl { get; set; }
17+
18+
/// <summary>
19+
/// The prompt the background would generation from.
20+
/// </summary>
21+
public string? RefPrompt { get; set; }
22+
23+
/// <summary>
24+
/// The negative prompt.
25+
/// </summary>
26+
public string? NegRefPrompt { get; set; }
27+
28+
/// <summary>
29+
/// The title to be put in generated image, max length is 8.
30+
/// </summary>
31+
public string? Title { get; set; }
32+
33+
/// <summary>
34+
/// Valid when <see cref="Title"/> is set, max length is 10.
35+
/// </summary>
36+
public string? SubTitle { get; set; }
37+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// The output of background generation task.
5+
/// </summary>
6+
public record BackgroundGenerationOutput : DashScopeTaskOutput
7+
{
8+
/// <summary>
9+
/// The generated image urls.
10+
/// </summary>
11+
public List<BackgroundGenerationResult>? Results { get; set; }
12+
13+
/// <summary>
14+
/// The generated result of texts.
15+
/// </summary>
16+
public BackgroundGenerationTextResult? TextResults { get; set; }
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// The parameters of background generation task.
5+
/// </summary>
6+
public class BackgroundGenerationParameters
7+
{
8+
/// <summary>
9+
/// The number of images to be generated.
10+
/// </summary>
11+
public int? N { get; set; }
12+
13+
/// <summary>
14+
/// Range at [0, 999], controls the distance from generated image to reference image.
15+
/// </summary>
16+
public int? NoiseLevel { get; set; }
17+
18+
/// <summary>
19+
/// Range at [0,1]. When RefImageUrl and RefPrompt are both set, controls the percentage of ref prompt weight.
20+
/// </summary>
21+
public float? RefPromptWeight { get; set; }
22+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// The result of background generation task.
5+
/// </summary>
6+
/// <param name="Url">The url of generated image.</param>
7+
public record BackgroundGenerationResult(string Url);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// The results of background generation task.
5+
/// </summary>
6+
/// <param name="Urls">The urls of images that put texts on.</param>
7+
/// <param name="Params">The generated texts for image.</param>
8+
public record BackgroundGenerationTextResult(
9+
List<BackgroundGenerationTextResultUrl> Urls,
10+
List<BackgroundGenerationTextResultParams>? Params);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// The gradient of background generation text.
5+
/// </summary>
6+
/// <param name="Type">Type of gradient. e.g. <c>linear</c></param>
7+
/// <param name="GradientUnits">Unit of gradient. e.g. <c>pixels</c></param>
8+
/// <param name="ColorStops">Color stops of gradient.</param>
9+
public record BackgroundGenerationTextResultGradient(
10+
string Type,
11+
string GradientUnits,
12+
List<BackgroundGenerationTextResultGradientColorStop> ColorStops)
13+
{
14+
/// <summary>
15+
/// Coords of each stop, use "x1", "y1", "x2", "y2"... to access the coords.
16+
/// </summary>
17+
public Dictionary<string, int>? Coords { get; set; }
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// The color stop of gradient in background generation result.
5+
/// </summary>
6+
/// <param name="Color">The color of current stop.</param>
7+
/// <param name="Offset">The position of current stop.</param>
8+
public record BackgroundGenerationTextResultGradientColorStop(string Color, float Offset);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.Text.Json.Serialization;
2+
using Cnblogs.DashScope.Sdk.Internals;
3+
4+
namespace Cnblogs.DashScope.Sdk;
5+
6+
/// <summary>
7+
/// Represents one layer of background generation text result.
8+
/// </summary>
9+
/// <param name="Idx">The if of the layer.</param>
10+
/// <param name="Type">The type of layer, can be "text" or "text_mask".</param>
11+
/// <param name="Top">The top position of the layer.</param>
12+
/// <param name="Left">The left position of the layer.</param>
13+
/// <param name="Width">The width of the layer.</param>
14+
/// <param name="Height">The height of the layer.</param>
15+
/// <param name="SubType">Available when type is "text". The type of text, can ba "Title" or "SubTitle".</param>
16+
/// <param name="Content">Available when type is "text". The content of the text.</param>
17+
/// <param name="FontFamily">Available when type is "text". The font family of the text.</param>
18+
/// <param name="FontSize">Available when type is "text". The font size of the text.</param>
19+
/// <param name="FontWeight">Available when type is "text". The font weight of the text. e.g. Medium.</param>
20+
/// <param name="FontColor">Available when type is "text". The color of the text in HEX format.</param>
21+
/// <param name="Direction">Available when type is "text". The direction of the text. e.g. vertical or horizontal.</param>
22+
/// <param name="Alignment">Available when type is "text". The alignment of the text, can be center, left or right.</param>
23+
/// <param name="LineHeight">Available when type is "text". The line height of the text.</param>
24+
/// <param name="FontItalic">Available when type is "text". Is the text needs to be italic.</param>
25+
/// <param name="FontLineThrough">Available when type is "text". Is the text has line through.</param>
26+
/// <param name="FontUnderLine">Available when type is "text". Is the text has underline.</param>
27+
/// <param name="TextShadow">Available when type is "text". The text shadow of the text. e.g. 2px 2px #80808080</param>
28+
/// <param name="TextStroke">Available when type is "text". The stroke of the text. e.g. 1px #fffffff0</param>
29+
/// <param name="Opacity">The opacity of the layer.</param>
30+
/// <param name="Radius">Available when type is "text_mask". The border radius of the mask.</param>
31+
/// <param name="BoxShadow">Available when type is "text_mask". The box shadow of the mask.</param>
32+
/// <param name="Color">Available when type is "text_mask". Color of the mask.</param>
33+
/// <param name="Gradient">Available when type is "text_mask". The gradient of the mask.</param>
34+
public record BackgroundGenerationTextResultLayer(
35+
int Idx,
36+
string Type,
37+
int Top,
38+
int Left,
39+
int Width,
40+
int Height,
41+
string? SubType = null,
42+
string? Content = null,
43+
string? FontFamily = null,
44+
int? FontSize = null,
45+
string? FontWeight = null,
46+
string? FontColor = null,
47+
string? Direction = null,
48+
string? Alignment = null,
49+
float? LineHeight = null,
50+
bool? FontItalic = null,
51+
bool? FontLineThrough = null,
52+
bool? FontUnderLine = null,
53+
[property: JsonConverter(typeof(DashScopeZeroAsNullConvertor))]
54+
string? TextShadow = null,
55+
string? TextStroke = null,
56+
float? Opacity = null,
57+
int? Radius = null,
58+
string? BoxShadow = null,
59+
string? Color = null,
60+
BackgroundGenerationTextResultGradient? Gradient = null);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// The generated styles of text in one image of background generation task.
5+
/// </summary>
6+
/// <param name="SampleIdx">The id of image.</param>
7+
/// <param name="Layers">The layers of styled text.</param>
8+
public record BackgroundGenerationTextResultParams(int SampleIdx, List<BackgroundGenerationTextResultLayer> Layers);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// The generated background text url.
5+
/// </summary>
6+
/// <param name="Url">The url of generated text.</param>
7+
public record BackgroundGenerationTextResultUrl(string Url);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Cnblogs.DashScope.Sdk;
2+
3+
/// <summary>
4+
/// The usage of background generation task.
5+
/// </summary>
6+
/// <param name="ImageCount">The count of generated image.</param>
7+
public record BackgroundGenerationUsage(int ImageCount);

src/Cnblogs.DashScope.Sdk/DashScopeClientCore.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@ public async Task<ModelResponse<ImageGenerationOutput, ImageGenerationUsage>> Cr
190190
!;
191191
}
192192

193+
/// <inheritdoc />
194+
public async Task<ModelResponse<BackgroundGenerationOutput, BackgroundGenerationUsage>> CreateBackgroundGenerationTaskAsync(ModelRequest<BackgroundGenerationInput, BackgroundGenerationParameters> input, CancellationToken cancellationToken = default)
195+
{
196+
var request = BuildRequest(HttpMethod.Post, ApiLinks.BackgroundGeneration, input, isTask: true);
197+
return (await SendAsync<ModelResponse<BackgroundGenerationOutput, BackgroundGenerationUsage>>(
198+
request,
199+
cancellationToken))!;
200+
}
201+
193202
private static HttpRequestMessage BuildSseRequest<TPayload>(HttpMethod method, string url, TPayload payload)
194203
where TPayload : class
195204
{

src/Cnblogs.DashScope.Sdk/IDashScopeClient.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,15 @@ Task<ModelResponse<TokenizationOutput, TokenizationUsage>> TokenizeAsync(
138138
Task<ModelResponse<ImageGenerationOutput, ImageGenerationUsage>> CreateImageGenerationTaskAsync(
139139
ModelRequest<ImageGenerationInput> input,
140140
CancellationToken cancellationToken = default);
141+
142+
/// <summary>
143+
/// Create a background image generation task.
144+
/// </summary>
145+
/// <param name="input">The input of the task.</param>
146+
/// <param name="cancellationToken">The cancellation token to use.</param>
147+
/// <returns></returns>
148+
public Task<ModelResponse<BackgroundGenerationOutput, BackgroundGenerationUsage>>
149+
CreateBackgroundGenerationTaskAsync(
150+
ModelRequest<BackgroundGenerationInput, BackgroundGenerationParameters> input,
151+
CancellationToken cancellationToken = default);
141152
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
4+
namespace Cnblogs.DashScope.Sdk.Internals;
5+
6+
internal class DashScopeZeroAsNullConvertor : JsonConverter<string>
7+
{
8+
/// <inheritdoc />
9+
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
10+
{
11+
if (reader.TokenType is JsonTokenType.Null or JsonTokenType.None)
12+
{
13+
return null;
14+
}
15+
16+
if (reader.TokenType is JsonTokenType.String)
17+
{
18+
return reader.GetString();
19+
}
20+
21+
if (reader.TokenType is JsonTokenType.Number)
22+
{
23+
return reader.GetInt32() == 0 ? null : throw new JsonException("Invalid number for string");
24+
}
25+
26+
throw new JsonException("Invalid type for string");
27+
}
28+
29+
/// <inheritdoc />
30+
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
31+
{
32+
JsonSerializer.Serialize(writer, value, options);
33+
}
34+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Cnblogs.DashScope.Sdk.Wanx;
2+
3+
/// <summary>
4+
/// Models for background generation model.
5+
/// </summary>
6+
public enum WanxBackgroundGenerationModel
7+
{
8+
/// <summary>
9+
/// wanx-background-generation-v2
10+
/// </summary>
11+
WanxBackgroundGenerationV2 = 1
12+
}

src/Cnblogs.DashScope.Sdk/Wanx/WanxGenerationApi.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,74 @@ public static Task<DashScopeTask<ImageGenerationOutput, ImageGenerationUsage>>
128128
{
129129
return dashScopeClient.GetTaskAsync<ImageGenerationOutput, ImageGenerationUsage>(taskId, cancellationToken);
130130
}
131+
132+
/// <summary>
133+
/// Create wanx background generation task.
134+
/// </summary>
135+
/// <param name="dashScopeClient">The <see cref="IDashScopeClient"/>.</param>
136+
/// <param name="model">The model to use.</param>
137+
/// <param name="input">The input that generate background images from.</param>
138+
/// <param name="parameters">Optional parameters for generation.</param>
139+
/// <param name="cancellationToken">The cancellation token to use.</param>
140+
/// <returns></returns>
141+
public static async Task<DashScopeTaskOutput> CreateWanxBackgroundGenerationTaskAsync(
142+
this IDashScopeClient dashScopeClient,
143+
WanxBackgroundGenerationModel model,
144+
BackgroundGenerationInput input,
145+
BackgroundGenerationParameters? parameters = null,
146+
CancellationToken cancellationToken = default)
147+
{
148+
var response = await dashScopeClient.CreateBackgroundGenerationTaskAsync(
149+
new()
150+
{
151+
Model = model.GetModelName(),
152+
Input = input,
153+
Parameters = parameters
154+
},
155+
cancellationToken);
156+
return response.Output;
157+
}
158+
159+
/// <summary>
160+
/// Create wanx background generation task.
161+
/// </summary>
162+
/// <param name="dashScopeClient">The <see cref="IDashScopeClient"/>.</param>
163+
/// <param name="model">The model to use.</param>
164+
/// <param name="input">The input that generate background images from.</param>
165+
/// <param name="parameters">Optional parameters for generation.</param>
166+
/// <param name="cancellationToken">The cancellation token to use.</param>
167+
/// <returns></returns>
168+
public static async Task<DashScopeTaskOutput> CreateWanxBackgroundGenerationTaskAsync(
169+
this IDashScopeClient dashScopeClient,
170+
string model,
171+
BackgroundGenerationInput input,
172+
BackgroundGenerationParameters? parameters = null,
173+
CancellationToken cancellationToken = default)
174+
{
175+
var response = await dashScopeClient.CreateBackgroundGenerationTaskAsync(
176+
new()
177+
{
178+
Model = model,
179+
Input = input,
180+
Parameters = parameters
181+
},
182+
cancellationToken);
183+
return response.Output;
184+
}
185+
186+
/// <summary>
187+
/// Query wanx background generation task status.
188+
/// </summary>
189+
/// <param name="client">The <see cref="IDashScopeClient"/>.</param>
190+
/// <param name="taskId">The task id to query.</param>
191+
/// <param name="cancellationToken">The cancellation token to use.</param>
192+
/// <returns></returns>
193+
public static Task<DashScopeTask<BackgroundGenerationOutput, BackgroundGenerationUsage>>
194+
GetWanxBackgroundGenerationTaskAsync(
195+
this IDashScopeClient client,
196+
string taskId,
197+
CancellationToken cancellationToken = default)
198+
{
199+
return client.GetTaskAsync<BackgroundGenerationOutput, BackgroundGenerationUsage>(taskId, cancellationToken);
200+
}
131201
}

src/Cnblogs.DashScope.Sdk/Wanx/WanxModelNames.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,13 @@ public static string GetModelName(this WanxStyleRepaintModel model)
2121
_ => ThrowHelper.UnknownModelName(nameof(model), model)
2222
};
2323
}
24+
25+
public static string GetModelName(this WanxBackgroundGenerationModel model)
26+
{
27+
return model switch
28+
{
29+
WanxBackgroundGenerationModel.WanxBackgroundGenerationV2 => "wanx-background-generation-v2",
30+
_ => ThrowHelper.UnknownModelName(nameof(model), model)
31+
};
32+
}
2433
}

0 commit comments

Comments
 (0)