mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-16 14:52:31 +00:00
add pricing tests
This commit is contained in:
1 parent
b100f33548
commit
3f09515c84
3 files changed
+346
-376
No files matched your search
+8
-35
@@ -53,10 +53,7 @@ const MAX_EDGE: u32 = 3840;
|
||||
/// Given an aspect ratio and a resolution tier, compute a concrete
|
||||
/// `CustomImageSize`. The returned dimensions are guaranteed to satisfy all
|
||||
/// GPT Image 2 constraints.
|
||||
pub fn compute_custom_image_size(
|
||||
aspect: GptImage2AspectRatio,
|
||||
resolution: GptImage2Resolution,
|
||||
) -> CustomImageSize {
|
||||
pub fn compute_custom_image_size(aspect: GptImage2AspectRatio, resolution: GptImage2Resolution) -> CustomImageSize {
|
||||
// Aspect ratio as (w_ratio, h_ratio)
|
||||
let (w_ratio, h_ratio): (u32, u32) = match aspect {
|
||||
GptImage2AspectRatio::Square | GptImage2AspectRatio::SquareHd => (1, 1),
|
||||
@@ -149,33 +146,12 @@ mod tests {
|
||||
let max_edge = size.width.max(size.height);
|
||||
let min_edge = size.width.min(size.height);
|
||||
|
||||
assert!(
|
||||
size.width % 16 == 0,
|
||||
"width {} not multiple of 16 for {:?} {:?}", size.width, aspect, resolution
|
||||
);
|
||||
assert!(
|
||||
size.height % 16 == 0,
|
||||
"height {} not multiple of 16 for {:?} {:?}", size.height, aspect, resolution
|
||||
);
|
||||
assert!(
|
||||
max_edge <= MAX_EDGE,
|
||||
"max edge {} exceeds 3840 for {:?} {:?}", max_edge, aspect, resolution
|
||||
);
|
||||
assert!(
|
||||
max_edge as f64 / min_edge as f64 <= 3.0,
|
||||
"ratio {:.2} exceeds 3:1 for {:?} {:?} ({}x{})",
|
||||
max_edge as f64 / min_edge as f64, aspect, resolution, size.width, size.height
|
||||
);
|
||||
assert!(
|
||||
pixels >= MIN_PIXELS,
|
||||
"pixels {} < 655360 for {:?} {:?} ({}x{})",
|
||||
pixels, aspect, resolution, size.width, size.height
|
||||
);
|
||||
assert!(
|
||||
pixels <= MAX_PIXELS,
|
||||
"pixels {} > 8294400 for {:?} {:?} ({}x{})",
|
||||
pixels, aspect, resolution, size.width, size.height
|
||||
);
|
||||
assert!(size.width % 16 == 0, "width {} not multiple of 16 for {:?} {:?}", size.width, aspect, resolution);
|
||||
assert!(size.height % 16 == 0, "height {} not multiple of 16 for {:?} {:?}", size.height, aspect, resolution);
|
||||
assert!(max_edge <= MAX_EDGE, "max edge {} exceeds 3840 for {:?} {:?}", max_edge, aspect, resolution);
|
||||
assert!(max_edge as f64 / min_edge as f64 <= 3.0, "ratio {:.2} exceeds 3:1 for {:?} {:?} ({}x{})", max_edge as f64 / min_edge as f64, aspect, resolution, size.width, size.height);
|
||||
assert!(pixels >= MIN_PIXELS, "pixels {} < 655360 for {:?} {:?} ({}x{})", pixels, aspect, resolution, size.width, size.height);
|
||||
assert!(pixels <= MAX_PIXELS, "pixels {} > 8294400 for {:?} {:?} ({}x{})", pixels, aspect, resolution, size.width, size.height);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -187,10 +163,7 @@ mod tests {
|
||||
for &resolution in ALL_RESOLUTIONS {
|
||||
let size = compute_custom_image_size(aspect, resolution);
|
||||
let pixels = size.width as u64 * size.height as u64;
|
||||
println!(
|
||||
"{:?} + {:?} => {}x{} ({} pixels)",
|
||||
aspect, resolution, size.width, size.height, pixels
|
||||
);
|
||||
println!("{:?} + {:?} => {}x{} ({} pixels)", aspect, resolution, size.width, size.height, pixels);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+170
-182
@@ -1,46 +1,104 @@
|
||||
use crate::requests::api::image::edit::gpt_image_2_edit_image::api::{
|
||||
GptImage2EditImageNumImages, GptImage2EditImageQuality,
|
||||
GptImage2EditImageRequest, GptImage2EditImageSize,
|
||||
};
|
||||
use crate::requests::api::image::common::gpt_image_2_resolution::{compute_custom_image_size, GptImage2AspectRatio, GptImage2Resolution};
|
||||
use crate::requests::api::image::edit::gpt_image_2_edit_image::api::{GptImage2EditImageNumImages, GptImage2EditImageQuality, GptImage2EditImageRequest, GptImage2EditImageSize};
|
||||
use crate::requests::traits::fal_request_cost_calculator_trait::{FalRequestCostCalculator, UsdCents};
|
||||
|
||||
const PRICING_TABLE: &[GptImage2PriceRow] = &[GptImage2PriceRow { width: 1024, height: 768, low: 11, medium: 43, high: 151 }, GptImage2PriceRow { width: 1024, height: 1024, low: 15, medium: 61, high: 219 }, GptImage2PriceRow { width: 1024, height: 1536, low: 18, medium: 54, high: 178 }, GptImage2PriceRow { width: 1920, height: 1080, low: 17, medium: 53, high: 158 }, GptImage2PriceRow { width: 2560, height: 1440, low: 19, medium: 68, high: 234 }, GptImage2PriceRow { width: 3840, height: 2160, low: 24, medium: 113, high: 413 }];
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
struct GptImage2PriceRow {
|
||||
width: u32,
|
||||
height: u32,
|
||||
low: u64,
|
||||
medium: u64,
|
||||
high: u64,
|
||||
}
|
||||
|
||||
impl GptImage2PriceRow {
|
||||
fn price_in_tenths_of_a_cent(self, quality: GptImage2EditImageQuality) -> u64 {
|
||||
match quality {
|
||||
GptImage2EditImageQuality::Low => self.low,
|
||||
GptImage2EditImageQuality::Medium => self.medium,
|
||||
GptImage2EditImageQuality::High => self.high,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FalRequestCostCalculator for GptImage2EditImageRequest {
|
||||
fn calculate_cost_in_cents(&self) -> UsdCents {
|
||||
// Cost table (per image) by approximate pixel dimensions:
|
||||
//
|
||||
// landscape_4_3 (~1024x768): low=$0.01, medium=$0.04, high=$0.15
|
||||
// square (~1024x1024): low=$0.01, medium=$0.06, high=$0.22
|
||||
// portrait_4_3 (~768x1024): low=$0.01, medium=$0.04, high=$0.15
|
||||
// landscape_16_9(~1920x1080): low=$0.01, medium=$0.04, high=$0.16
|
||||
// portrait_16_9 (~1080x1920): low=$0.01, medium=$0.04, high=$0.16
|
||||
// square_hd (~2048x2048): low=$0.01, medium=$0.06, high=$0.23
|
||||
// auto (varies): estimated as square_hd (worst case)
|
||||
let use_quality = self.quality.unwrap_or(GptImage2EditImageQuality::High);
|
||||
let use_size = self.image_size.unwrap_or(GptImage2EditImageSize::Square);
|
||||
|
||||
use GptImage2EditImageQuality::*;
|
||||
use GptImage2EditImageSize::*;
|
||||
|
||||
let base_cost = match (use_quality, use_size) {
|
||||
(Low, _) => 1,
|
||||
(Medium, Landscape4x3 | Portrait4x3 | Landscape16x9 | Portrait16x9) => 4,
|
||||
(Medium, Square) => 6,
|
||||
(Medium, SquareHd) => 6,
|
||||
(Medium, Auto) => 6,
|
||||
(High, Landscape4x3 | Portrait4x3) => 15,
|
||||
(High, Landscape16x9 | Portrait16x9) => 16,
|
||||
(High, Square) => 22,
|
||||
(High, SquareHd) => 23,
|
||||
(High, Auto) => 23,
|
||||
let quality = self.quality.unwrap_or(GptImage2EditImageQuality::High);
|
||||
let price = match dimensions_for_request(self) {
|
||||
Some((width, height)) => closest_price_row(width, height).price_in_tenths_of_a_cent(quality),
|
||||
None => max_price_for_quality(quality),
|
||||
};
|
||||
let total_tenths_of_a_cent = price * num_images(self.num_images);
|
||||
|
||||
let cost = match self.num_images {
|
||||
GptImage2EditImageNumImages::One => base_cost,
|
||||
GptImage2EditImageNumImages::Two => base_cost * 2,
|
||||
GptImage2EditImageNumImages::Three => base_cost * 3,
|
||||
GptImage2EditImageNumImages::Four => base_cost * 4,
|
||||
};
|
||||
cost as UsdCents
|
||||
total_tenths_of_a_cent.div_ceil(10) as UsdCents
|
||||
}
|
||||
}
|
||||
|
||||
fn dimensions_for_request(request: &GptImage2EditImageRequest) -> Option<(u32, u32)> {
|
||||
match (request.image_size, request.resolution) {
|
||||
(Some(GptImage2EditImageSize::Auto), _) => None,
|
||||
(Some(size), Some(resolution)) => {
|
||||
let aspect = size_to_aspect(size);
|
||||
let custom = compute_custom_image_size(aspect, resolution);
|
||||
Some((custom.width, custom.height))
|
||||
},
|
||||
(None, Some(resolution)) => {
|
||||
let custom = compute_custom_image_size(GptImage2AspectRatio::Square, resolution);
|
||||
Some((custom.width, custom.height))
|
||||
},
|
||||
(Some(size), None) => Some(preset_dimensions(size)),
|
||||
(None, None) => Some(preset_dimensions(GptImage2EditImageSize::Square)),
|
||||
}
|
||||
}
|
||||
|
||||
fn preset_dimensions(size: GptImage2EditImageSize) -> (u32, u32) {
|
||||
match size {
|
||||
GptImage2EditImageSize::SquareHd => (2048, 2048),
|
||||
GptImage2EditImageSize::Square => (1024, 1024),
|
||||
GptImage2EditImageSize::Portrait4x3 => (768, 1024),
|
||||
GptImage2EditImageSize::Portrait16x9 => (1080, 1920),
|
||||
GptImage2EditImageSize::Landscape4x3 => (1024, 768),
|
||||
GptImage2EditImageSize::Landscape16x9 => (1920, 1080),
|
||||
GptImage2EditImageSize::Auto => (3840, 2160),
|
||||
}
|
||||
}
|
||||
|
||||
fn size_to_aspect(size: GptImage2EditImageSize) -> GptImage2AspectRatio {
|
||||
match size {
|
||||
GptImage2EditImageSize::Square => GptImage2AspectRatio::Square,
|
||||
GptImage2EditImageSize::SquareHd => GptImage2AspectRatio::SquareHd,
|
||||
GptImage2EditImageSize::Landscape4x3 => GptImage2AspectRatio::Landscape4x3,
|
||||
GptImage2EditImageSize::Landscape16x9 => GptImage2AspectRatio::Landscape16x9,
|
||||
GptImage2EditImageSize::Portrait4x3 => GptImage2AspectRatio::Portrait4x3,
|
||||
GptImage2EditImageSize::Portrait16x9 => GptImage2AspectRatio::Portrait16x9,
|
||||
GptImage2EditImageSize::Auto => GptImage2AspectRatio::Square,
|
||||
}
|
||||
}
|
||||
|
||||
fn closest_price_row(width: u32, height: u32) -> GptImage2PriceRow {
|
||||
let pixels = width as u64 * height as u64;
|
||||
PRICING_TABLE
|
||||
.iter()
|
||||
.copied()
|
||||
.min_by_key(|row| {
|
||||
let row_pixels = row.width as u64 * row.height as u64;
|
||||
row_pixels.abs_diff(pixels)
|
||||
})
|
||||
.expect("GPT Image 2 pricing table cannot be empty")
|
||||
}
|
||||
|
||||
fn max_price_for_quality(quality: GptImage2EditImageQuality) -> u64 {
|
||||
PRICING_TABLE.iter().copied().map(|row| row.price_in_tenths_of_a_cent(quality)).max().expect("GPT Image 2 pricing table cannot be empty")
|
||||
}
|
||||
|
||||
fn num_images(num_images: GptImage2EditImageNumImages) -> u64 {
|
||||
match num_images {
|
||||
GptImage2EditImageNumImages::One => 1,
|
||||
GptImage2EditImageNumImages::Two => 2,
|
||||
GptImage2EditImageNumImages::Three => 3,
|
||||
GptImage2EditImageNumImages::Four => 4,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,163 +106,93 @@ impl FalRequestCostCalculator for GptImage2EditImageRequest {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn make_request(
|
||||
num_images: GptImage2EditImageNumImages,
|
||||
quality: Option<GptImage2EditImageQuality>,
|
||||
image_size: Option<GptImage2EditImageSize>,
|
||||
) -> GptImage2EditImageRequest {
|
||||
GptImage2EditImageRequest {
|
||||
prompt: "test".to_string(),
|
||||
image_urls: vec!["https://example.com/image.png".to_string()],
|
||||
num_images,
|
||||
mask_url: None,
|
||||
image_size,
|
||||
quality,
|
||||
resolution: None,
|
||||
output_format: None,
|
||||
const QUALITIES: &[(GptImage2EditImageQuality, u64, u64, u64, u64, u64, u64)] = &[(GptImage2EditImageQuality::Low, 11, 15, 18, 17, 19, 24), (GptImage2EditImageQuality::Medium, 43, 61, 54, 53, 68, 113), (GptImage2EditImageQuality::High, 151, 219, 178, 158, 234, 413)];
|
||||
|
||||
const PRESET_CASES: &[(GptImage2EditImageSize, u64)] = &[(GptImage2EditImageSize::Landscape4x3, 0), (GptImage2EditImageSize::Portrait4x3, 0), (GptImage2EditImageSize::Square, 1), (GptImage2EditImageSize::Landscape16x9, 3), (GptImage2EditImageSize::Portrait16x9, 3), (GptImage2EditImageSize::SquareHd, 4)];
|
||||
|
||||
const RESOLUTION_CASES: &[(GptImage2EditImageSize, GptImage2Resolution, u64)] = &[
|
||||
(GptImage2EditImageSize::Landscape4x3, GptImage2Resolution::OneK, 0),
|
||||
(GptImage2EditImageSize::Landscape4x3, GptImage2Resolution::TwoK, 4),
|
||||
(GptImage2EditImageSize::Landscape4x3, GptImage2Resolution::ThreeK, 5),
|
||||
(GptImage2EditImageSize::Landscape4x3, GptImage2Resolution::FourK, 5),
|
||||
(GptImage2EditImageSize::Landscape16x9, GptImage2Resolution::OneK, 0),
|
||||
(GptImage2EditImageSize::Landscape16x9, GptImage2Resolution::TwoK, 3),
|
||||
(GptImage2EditImageSize::Landscape16x9, GptImage2Resolution::ThreeK, 4),
|
||||
(GptImage2EditImageSize::Landscape16x9, GptImage2Resolution::FourK, 5),
|
||||
(GptImage2EditImageSize::Portrait4x3, GptImage2Resolution::OneK, 0),
|
||||
(GptImage2EditImageSize::Portrait4x3, GptImage2Resolution::TwoK, 4),
|
||||
(GptImage2EditImageSize::Portrait4x3, GptImage2Resolution::ThreeK, 5),
|
||||
(GptImage2EditImageSize::Portrait4x3, GptImage2Resolution::FourK, 5),
|
||||
(GptImage2EditImageSize::Portrait16x9, GptImage2Resolution::OneK, 0),
|
||||
(GptImage2EditImageSize::Portrait16x9, GptImage2Resolution::TwoK, 3),
|
||||
(GptImage2EditImageSize::Portrait16x9, GptImage2Resolution::ThreeK, 4),
|
||||
(GptImage2EditImageSize::Portrait16x9, GptImage2Resolution::FourK, 5),
|
||||
(GptImage2EditImageSize::Square, GptImage2Resolution::OneK, 1),
|
||||
(GptImage2EditImageSize::Square, GptImage2Resolution::TwoK, 4),
|
||||
(GptImage2EditImageSize::Square, GptImage2Resolution::ThreeK, 5),
|
||||
(GptImage2EditImageSize::Square, GptImage2Resolution::FourK, 5),
|
||||
(GptImage2EditImageSize::SquareHd, GptImage2Resolution::OneK, 1),
|
||||
(GptImage2EditImageSize::SquareHd, GptImage2Resolution::TwoK, 4),
|
||||
(GptImage2EditImageSize::SquareHd, GptImage2Resolution::ThreeK, 5),
|
||||
(GptImage2EditImageSize::SquareHd, GptImage2Resolution::FourK, 5),
|
||||
];
|
||||
|
||||
fn make_request(num_images: GptImage2EditImageNumImages, quality: Option<GptImage2EditImageQuality>, image_size: Option<GptImage2EditImageSize>, resolution: Option<GptImage2Resolution>) -> GptImage2EditImageRequest {
|
||||
GptImage2EditImageRequest { prompt: "test".to_string(), image_urls: vec!["https://example.com/image.png".to_string()], num_images, mask_url: None, image_size, quality, resolution, output_format: None }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_defaults_to_high_square() {
|
||||
assert_eq!(make_request(GptImage2EditImageNumImages::One, None, None, None).calculate_cost_in_cents(), 22);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_rounds_final_request_total_up_to_whole_cents() {
|
||||
assert_eq!(make_request(GptImage2EditImageNumImages::Four, Some(GptImage2EditImageQuality::Low), Some(GptImage2EditImageSize::Landscape4x3), None,).calculate_cost_in_cents(), 5,);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preset_pricing_matches_published_table() {
|
||||
for &(quality, landscape_4x3, square, portrait, landscape_16x9, square_hd, four_k) in QUALITIES {
|
||||
let expected_by_row = [landscape_4x3, square, portrait, landscape_16x9, square_hd, four_k];
|
||||
|
||||
for &(size, row_index) in PRESET_CASES {
|
||||
let expected = expected_by_row[row_index as usize].div_ceil(10);
|
||||
assert_eq!(make_request(GptImage2EditImageNumImages::One, Some(quality), Some(size), None).calculate_cost_in_cents(), expected, "quality={quality:?} size={size:?}",);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod default_tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn exact_published_rows_match_all_quality_prices() {
|
||||
for &(quality, landscape_4x3, square, portrait, landscape_16x9, square_hd, four_k) in QUALITIES {
|
||||
let cases = [(1024, 768, landscape_4x3), (1024, 1024, square), (1024, 1536, portrait), (1920, 1080, landscape_16x9), (2560, 1440, square_hd), (3840, 2160, four_k)];
|
||||
|
||||
#[test]
|
||||
fn cost_defaults_one_image() {
|
||||
// Default quality=High, size=Square => 22 cents
|
||||
assert_eq!(
|
||||
make_request(GptImage2EditImageNumImages::One, None, None)
|
||||
.calculate_cost_in_cents(), 22);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_defaults_four_images() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2EditImageNumImages::Four, None, None)
|
||||
.calculate_cost_in_cents(), 88);
|
||||
for (width, height, expected) in cases {
|
||||
assert_eq!(closest_price_row(width, height).price_in_tenths_of_a_cent(quality), expected, "quality={quality:?} dimensions={width}x{height}",);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod low_quality_tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn resolution_pricing_covers_aspect_size_resolution_combinations() {
|
||||
for &(quality, landscape_4x3, square, portrait, landscape_16x9, square_hd, four_k) in QUALITIES {
|
||||
let expected_by_row = [landscape_4x3, square, portrait, landscape_16x9, square_hd, four_k];
|
||||
|
||||
#[test]
|
||||
fn cost_low_any_size_one_image() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2EditImageNumImages::One, Some(GptImage2EditImageQuality::Low), Some(GptImage2EditImageSize::SquareHd))
|
||||
.calculate_cost_in_cents(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_low_any_size_four_images() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2EditImageNumImages::Four, Some(GptImage2EditImageQuality::Low), Some(GptImage2EditImageSize::Auto))
|
||||
.calculate_cost_in_cents(), 4);
|
||||
for &(size, resolution, row_index) in RESOLUTION_CASES {
|
||||
let expected = expected_by_row[row_index as usize].div_ceil(10);
|
||||
assert_eq!(make_request(GptImage2EditImageNumImages::One, Some(quality), Some(size), Some(resolution)).calculate_cost_in_cents(), expected, "quality={quality:?} size={size:?} resolution={resolution:?}",);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod medium_quality_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn cost_medium_landscape_4x3() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2EditImageNumImages::One, Some(GptImage2EditImageQuality::Medium), Some(GptImage2EditImageSize::Landscape4x3))
|
||||
.calculate_cost_in_cents(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_medium_portrait_16x9() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2EditImageNumImages::One, Some(GptImage2EditImageQuality::Medium), Some(GptImage2EditImageSize::Portrait16x9))
|
||||
.calculate_cost_in_cents(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_medium_square() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2EditImageNumImages::One, Some(GptImage2EditImageQuality::Medium), Some(GptImage2EditImageSize::Square))
|
||||
.calculate_cost_in_cents(), 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_medium_square_hd() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2EditImageNumImages::One, Some(GptImage2EditImageQuality::Medium), Some(GptImage2EditImageSize::SquareHd))
|
||||
.calculate_cost_in_cents(), 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_medium_auto() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2EditImageNumImages::One, Some(GptImage2EditImageQuality::Medium), Some(GptImage2EditImageSize::Auto))
|
||||
.calculate_cost_in_cents(), 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_medium_landscape_4x3_three_images() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2EditImageNumImages::Three, Some(GptImage2EditImageQuality::Medium), Some(GptImage2EditImageSize::Landscape4x3))
|
||||
.calculate_cost_in_cents(), 12);
|
||||
}
|
||||
#[test]
|
||||
fn auto_uses_highest_published_price_for_quality() {
|
||||
assert_eq!(make_request(GptImage2EditImageNumImages::Two, Some(GptImage2EditImageQuality::High), Some(GptImage2EditImageSize::Auto), Some(GptImage2Resolution::OneK),).calculate_cost_in_cents(), 83,);
|
||||
}
|
||||
|
||||
mod high_quality_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn cost_high_landscape_4x3() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2EditImageNumImages::One, Some(GptImage2EditImageQuality::High), Some(GptImage2EditImageSize::Landscape4x3))
|
||||
.calculate_cost_in_cents(), 15);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_high_portrait_4x3() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2EditImageNumImages::One, Some(GptImage2EditImageQuality::High), Some(GptImage2EditImageSize::Portrait4x3))
|
||||
.calculate_cost_in_cents(), 15);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_high_landscape_16x9() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2EditImageNumImages::One, Some(GptImage2EditImageQuality::High), Some(GptImage2EditImageSize::Landscape16x9))
|
||||
.calculate_cost_in_cents(), 16);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_high_portrait_16x9() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2EditImageNumImages::One, Some(GptImage2EditImageQuality::High), Some(GptImage2EditImageSize::Portrait16x9))
|
||||
.calculate_cost_in_cents(), 16);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_high_square() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2EditImageNumImages::One, Some(GptImage2EditImageQuality::High), Some(GptImage2EditImageSize::Square))
|
||||
.calculate_cost_in_cents(), 22);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_high_square_hd() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2EditImageNumImages::One, Some(GptImage2EditImageQuality::High), Some(GptImage2EditImageSize::SquareHd))
|
||||
.calculate_cost_in_cents(), 23);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_high_auto() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2EditImageNumImages::One, Some(GptImage2EditImageQuality::High), Some(GptImage2EditImageSize::Auto))
|
||||
.calculate_cost_in_cents(), 23);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_high_square_hd_two_images() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2EditImageNumImages::Two, Some(GptImage2EditImageQuality::High), Some(GptImage2EditImageSize::SquareHd))
|
||||
.calculate_cost_in_cents(), 46);
|
||||
}
|
||||
#[test]
|
||||
fn resolution_without_image_size_defaults_to_square_aspect() {
|
||||
assert_eq!(make_request(GptImage2EditImageNumImages::Two, Some(GptImage2EditImageQuality::High), None, Some(GptImage2Resolution::FourK),).calculate_cost_in_cents(), 83,);
|
||||
}
|
||||
}
|
||||
+168
-159
@@ -1,44 +1,95 @@
|
||||
use crate::requests::api::image::text::gpt_image_2_text_to_image::api::{
|
||||
GptImage2TextToImageNumImages, GptImage2TextToImageQuality,
|
||||
GptImage2TextToImageRequest, GptImage2TextToImageSize,
|
||||
};
|
||||
use crate::requests::api::image::common::gpt_image_2_resolution::{compute_custom_image_size, GptImage2AspectRatio, GptImage2Resolution};
|
||||
use crate::requests::api::image::text::gpt_image_2_text_to_image::api::{GptImage2TextToImageNumImages, GptImage2TextToImageQuality, GptImage2TextToImageRequest, GptImage2TextToImageSize};
|
||||
use crate::requests::traits::fal_request_cost_calculator_trait::{FalRequestCostCalculator, UsdCents};
|
||||
|
||||
const PRICING_TABLE: &[GptImage2PriceRow] = &[GptImage2PriceRow { width: 1024, height: 768, low: 11, medium: 43, high: 151 }, GptImage2PriceRow { width: 1024, height: 1024, low: 15, medium: 61, high: 219 }, GptImage2PriceRow { width: 1024, height: 1536, low: 18, medium: 54, high: 178 }, GptImage2PriceRow { width: 1920, height: 1080, low: 17, medium: 53, high: 158 }, GptImage2PriceRow { width: 2560, height: 1440, low: 19, medium: 68, high: 234 }, GptImage2PriceRow { width: 3840, height: 2160, low: 24, medium: 113, high: 413 }];
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
struct GptImage2PriceRow {
|
||||
width: u32,
|
||||
height: u32,
|
||||
low: u64,
|
||||
medium: u64,
|
||||
high: u64,
|
||||
}
|
||||
|
||||
impl GptImage2PriceRow {
|
||||
fn price_in_tenths_of_a_cent(self, quality: GptImage2TextToImageQuality) -> u64 {
|
||||
match quality {
|
||||
GptImage2TextToImageQuality::Low => self.low,
|
||||
GptImage2TextToImageQuality::Medium => self.medium,
|
||||
GptImage2TextToImageQuality::High => self.high,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FalRequestCostCalculator for GptImage2TextToImageRequest {
|
||||
fn calculate_cost_in_cents(&self) -> UsdCents {
|
||||
// Cost table (per image) by approximate pixel dimensions:
|
||||
//
|
||||
// landscape_4_3 (~1024x768): low=$0.01, medium=$0.04, high=$0.15
|
||||
// square (~1024x1024): low=$0.01, medium=$0.06, high=$0.22
|
||||
// portrait_4_3 (~768x1024): low=$0.01, medium=$0.04, high=$0.15
|
||||
// landscape_16_9(~1920x1080): low=$0.01, medium=$0.04, high=$0.16
|
||||
// portrait_16_9 (~1080x1920): low=$0.01, medium=$0.04, high=$0.16
|
||||
// square_hd (~2048x2048): low=$0.01, medium=$0.06, high=$0.23
|
||||
// auto (varies): estimated as square
|
||||
let use_quality = self.quality.unwrap_or(GptImage2TextToImageQuality::High);
|
||||
let use_size = self.image_size.unwrap_or(GptImage2TextToImageSize::Square);
|
||||
let quality = self.quality.unwrap_or(GptImage2TextToImageQuality::High);
|
||||
let (width, height) = dimensions_for_request(self);
|
||||
let price = closest_price_row(width, height).price_in_tenths_of_a_cent(quality);
|
||||
let total_tenths_of_a_cent = price * num_images(self.num_images);
|
||||
|
||||
use GptImage2TextToImageQuality::*;
|
||||
use GptImage2TextToImageSize::*;
|
||||
total_tenths_of_a_cent.div_ceil(10) as UsdCents
|
||||
}
|
||||
}
|
||||
|
||||
let base_cost = match (use_quality, use_size) {
|
||||
(Low, _) => 1,
|
||||
(Medium, Landscape4x3 | Portrait4x3 | Landscape16x9 | Portrait16x9) => 4,
|
||||
(Medium, Square) => 6,
|
||||
(Medium, SquareHd) => 6,
|
||||
(High, Landscape4x3 | Portrait4x3) => 15,
|
||||
(High, Landscape16x9 | Portrait16x9) => 16,
|
||||
(High, Square) => 22,
|
||||
(High, SquareHd) => 23,
|
||||
};
|
||||
fn dimensions_for_request(request: &GptImage2TextToImageRequest) -> (u32, u32) {
|
||||
match (request.image_size, request.resolution) {
|
||||
(Some(size), Some(resolution)) => {
|
||||
let aspect = size_to_aspect(size);
|
||||
let custom = compute_custom_image_size(aspect, resolution);
|
||||
(custom.width, custom.height)
|
||||
},
|
||||
(None, Some(resolution)) => {
|
||||
let custom = compute_custom_image_size(GptImage2AspectRatio::Square, resolution);
|
||||
(custom.width, custom.height)
|
||||
},
|
||||
(Some(size), None) => preset_dimensions(size),
|
||||
(None, None) => preset_dimensions(GptImage2TextToImageSize::Square),
|
||||
}
|
||||
}
|
||||
|
||||
let cost = match self.num_images {
|
||||
GptImage2TextToImageNumImages::One => base_cost,
|
||||
GptImage2TextToImageNumImages::Two => base_cost * 2,
|
||||
GptImage2TextToImageNumImages::Three => base_cost * 3,
|
||||
GptImage2TextToImageNumImages::Four => base_cost * 4,
|
||||
};
|
||||
cost as UsdCents
|
||||
fn preset_dimensions(size: GptImage2TextToImageSize) -> (u32, u32) {
|
||||
match size {
|
||||
GptImage2TextToImageSize::SquareHd => (2048, 2048),
|
||||
GptImage2TextToImageSize::Square => (1024, 1024),
|
||||
GptImage2TextToImageSize::Portrait4x3 => (768, 1024),
|
||||
GptImage2TextToImageSize::Portrait16x9 => (1080, 1920),
|
||||
GptImage2TextToImageSize::Landscape4x3 => (1024, 768),
|
||||
GptImage2TextToImageSize::Landscape16x9 => (1920, 1080),
|
||||
}
|
||||
}
|
||||
|
||||
fn size_to_aspect(size: GptImage2TextToImageSize) -> GptImage2AspectRatio {
|
||||
match size {
|
||||
GptImage2TextToImageSize::Square => GptImage2AspectRatio::Square,
|
||||
GptImage2TextToImageSize::SquareHd => GptImage2AspectRatio::SquareHd,
|
||||
GptImage2TextToImageSize::Landscape4x3 => GptImage2AspectRatio::Landscape4x3,
|
||||
GptImage2TextToImageSize::Landscape16x9 => GptImage2AspectRatio::Landscape16x9,
|
||||
GptImage2TextToImageSize::Portrait4x3 => GptImage2AspectRatio::Portrait4x3,
|
||||
GptImage2TextToImageSize::Portrait16x9 => GptImage2AspectRatio::Portrait16x9,
|
||||
}
|
||||
}
|
||||
|
||||
fn closest_price_row(width: u32, height: u32) -> GptImage2PriceRow {
|
||||
let pixels = width as u64 * height as u64;
|
||||
PRICING_TABLE
|
||||
.iter()
|
||||
.copied()
|
||||
.min_by_key(|row| {
|
||||
let row_pixels = row.width as u64 * row.height as u64;
|
||||
row_pixels.abs_diff(pixels)
|
||||
})
|
||||
.expect("GPT Image 2 pricing table cannot be empty")
|
||||
}
|
||||
|
||||
fn num_images(num_images: GptImage2TextToImageNumImages) -> u64 {
|
||||
match num_images {
|
||||
GptImage2TextToImageNumImages::One => 1,
|
||||
GptImage2TextToImageNumImages::Two => 2,
|
||||
GptImage2TextToImageNumImages::Three => 3,
|
||||
GptImage2TextToImageNumImages::Four => 4,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,141 +97,99 @@ impl FalRequestCostCalculator for GptImage2TextToImageRequest {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn make_request(
|
||||
num_images: GptImage2TextToImageNumImages,
|
||||
quality: Option<GptImage2TextToImageQuality>,
|
||||
image_size: Option<GptImage2TextToImageSize>,
|
||||
) -> GptImage2TextToImageRequest {
|
||||
GptImage2TextToImageRequest {
|
||||
prompt: "test".to_string(),
|
||||
num_images,
|
||||
image_size,
|
||||
quality,
|
||||
resolution: None,
|
||||
output_format: None,
|
||||
const QUALITIES: &[(GptImage2TextToImageQuality, u64, u64, u64, u64, u64, u64)] = &[
|
||||
(GptImage2TextToImageQuality::Low, 11, 15, 18, 17, 19, 24),
|
||||
(GptImage2TextToImageQuality::Medium, 43, 61, 54, 53, 68, 113),
|
||||
(GptImage2TextToImageQuality::High, 151, 219, 178, 158, 234, 413)
|
||||
];
|
||||
|
||||
const PRESET_CASES: &[(GptImage2TextToImageSize, u64)] = &[
|
||||
(GptImage2TextToImageSize::Landscape4x3, 0),
|
||||
(GptImage2TextToImageSize::Portrait4x3, 0),
|
||||
(GptImage2TextToImageSize::Square, 1),
|
||||
(GptImage2TextToImageSize::Landscape16x9, 3),
|
||||
(GptImage2TextToImageSize::Portrait16x9, 3),
|
||||
(GptImage2TextToImageSize::SquareHd, 4),
|
||||
];
|
||||
|
||||
const RESOLUTION_CASES: &[(GptImage2TextToImageSize, GptImage2Resolution, u64)] = &[
|
||||
(GptImage2TextToImageSize::Landscape4x3, GptImage2Resolution::OneK, 0),
|
||||
(GptImage2TextToImageSize::Landscape4x3, GptImage2Resolution::TwoK, 4),
|
||||
(GptImage2TextToImageSize::Landscape4x3, GptImage2Resolution::ThreeK, 5),
|
||||
(GptImage2TextToImageSize::Landscape4x3, GptImage2Resolution::FourK, 5),
|
||||
(GptImage2TextToImageSize::Landscape16x9, GptImage2Resolution::OneK, 0),
|
||||
(GptImage2TextToImageSize::Landscape16x9, GptImage2Resolution::TwoK, 3),
|
||||
(GptImage2TextToImageSize::Landscape16x9, GptImage2Resolution::ThreeK, 4),
|
||||
(GptImage2TextToImageSize::Landscape16x9, GptImage2Resolution::FourK, 5),
|
||||
(GptImage2TextToImageSize::Portrait4x3, GptImage2Resolution::OneK, 0),
|
||||
(GptImage2TextToImageSize::Portrait4x3, GptImage2Resolution::TwoK, 4),
|
||||
(GptImage2TextToImageSize::Portrait4x3, GptImage2Resolution::ThreeK, 5),
|
||||
(GptImage2TextToImageSize::Portrait4x3, GptImage2Resolution::FourK, 5),
|
||||
(GptImage2TextToImageSize::Portrait16x9, GptImage2Resolution::OneK, 0),
|
||||
(GptImage2TextToImageSize::Portrait16x9, GptImage2Resolution::TwoK, 3),
|
||||
(GptImage2TextToImageSize::Portrait16x9, GptImage2Resolution::ThreeK, 4),
|
||||
(GptImage2TextToImageSize::Portrait16x9, GptImage2Resolution::FourK, 5),
|
||||
(GptImage2TextToImageSize::Square, GptImage2Resolution::OneK, 1),
|
||||
(GptImage2TextToImageSize::Square, GptImage2Resolution::TwoK, 4),
|
||||
(GptImage2TextToImageSize::Square, GptImage2Resolution::ThreeK, 5),
|
||||
(GptImage2TextToImageSize::Square, GptImage2Resolution::FourK, 5),
|
||||
(GptImage2TextToImageSize::SquareHd, GptImage2Resolution::OneK, 1),
|
||||
(GptImage2TextToImageSize::SquareHd, GptImage2Resolution::TwoK, 4),
|
||||
(GptImage2TextToImageSize::SquareHd, GptImage2Resolution::ThreeK, 5),
|
||||
(GptImage2TextToImageSize::SquareHd, GptImage2Resolution::FourK, 5),
|
||||
];
|
||||
|
||||
fn make_request(num_images: GptImage2TextToImageNumImages, quality: Option<GptImage2TextToImageQuality>, image_size: Option<GptImage2TextToImageSize>, resolution: Option<GptImage2Resolution>) -> GptImage2TextToImageRequest {
|
||||
GptImage2TextToImageRequest { prompt: "test".to_string(), num_images, image_size, quality, resolution, output_format: None }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_defaults_to_high_square() {
|
||||
assert_eq!(make_request(GptImage2TextToImageNumImages::One, None, None, None).calculate_cost_in_cents(), 22);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_rounds_final_request_total_up_to_whole_cents() {
|
||||
assert_eq!(make_request(GptImage2TextToImageNumImages::Four, Some(GptImage2TextToImageQuality::Low), Some(GptImage2TextToImageSize::Landscape4x3), None,).calculate_cost_in_cents(), 5,);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preset_pricing_matches_published_table() {
|
||||
for &(quality, landscape_4x3, square, portrait, landscape_16x9, square_hd, four_k) in QUALITIES {
|
||||
let expected_by_row = [landscape_4x3, square, portrait, landscape_16x9, square_hd, four_k];
|
||||
|
||||
for &(size, row_index) in PRESET_CASES {
|
||||
let expected = expected_by_row[row_index as usize].div_ceil(10);
|
||||
assert_eq!(make_request(GptImage2TextToImageNumImages::One, Some(quality), Some(size), None).calculate_cost_in_cents(), expected, "quality={quality:?} size={size:?}",);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod default_tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn exact_published_rows_match_all_quality_prices() {
|
||||
for &(quality, landscape_4x3, square, portrait, landscape_16x9, square_hd, four_k) in QUALITIES {
|
||||
let cases = [(1024, 768, landscape_4x3), (1024, 1024, square), (1024, 1536, portrait), (1920, 1080, landscape_16x9), (2560, 1440, square_hd), (3840, 2160, four_k)];
|
||||
|
||||
#[test]
|
||||
fn cost_defaults_one_image() {
|
||||
// Default quality=High, size=Square => 22 cents
|
||||
assert_eq!(
|
||||
make_request(GptImage2TextToImageNumImages::One, None, None)
|
||||
.calculate_cost_in_cents(), 22);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_defaults_four_images() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2TextToImageNumImages::Four, None, None)
|
||||
.calculate_cost_in_cents(), 88);
|
||||
for (width, height, expected) in cases {
|
||||
assert_eq!(closest_price_row(width, height).price_in_tenths_of_a_cent(quality), expected, "quality={quality:?} dimensions={width}x{height}",);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod low_quality_tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn resolution_pricing_covers_aspect_size_resolution_combinations() {
|
||||
for &(quality, landscape_4x3, square, portrait, landscape_16x9, square_hd, four_k) in QUALITIES {
|
||||
let expected_by_row = [landscape_4x3, square, portrait, landscape_16x9, square_hd, four_k];
|
||||
|
||||
#[test]
|
||||
fn cost_low_any_size_one_image() {
|
||||
// Low quality is always 1 cent per image regardless of size
|
||||
assert_eq!(
|
||||
make_request(GptImage2TextToImageNumImages::One, Some(GptImage2TextToImageQuality::Low), Some(GptImage2TextToImageSize::SquareHd))
|
||||
.calculate_cost_in_cents(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_low_any_size_four_images() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2TextToImageNumImages::Four, Some(GptImage2TextToImageQuality::Low), Some(GptImage2TextToImageSize::Square))
|
||||
.calculate_cost_in_cents(), 4);
|
||||
for &(size, resolution, row_index) in RESOLUTION_CASES {
|
||||
let expected = expected_by_row[row_index as usize].div_ceil(10);
|
||||
assert_eq!(make_request(GptImage2TextToImageNumImages::One, Some(quality), Some(size), Some(resolution)).calculate_cost_in_cents(), expected, "quality={quality:?} size={size:?} resolution={resolution:?}",);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod medium_quality_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn cost_medium_landscape_4x3() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2TextToImageNumImages::One, Some(GptImage2TextToImageQuality::Medium), Some(GptImage2TextToImageSize::Landscape4x3))
|
||||
.calculate_cost_in_cents(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_medium_square() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2TextToImageNumImages::One, Some(GptImage2TextToImageQuality::Medium), Some(GptImage2TextToImageSize::Square))
|
||||
.calculate_cost_in_cents(), 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_medium_square_hd() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2TextToImageNumImages::One, Some(GptImage2TextToImageQuality::Medium), Some(GptImage2TextToImageSize::SquareHd))
|
||||
.calculate_cost_in_cents(), 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_medium_portrait_16x9_two_images() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2TextToImageNumImages::Two, Some(GptImage2TextToImageQuality::Medium), Some(GptImage2TextToImageSize::Portrait16x9))
|
||||
.calculate_cost_in_cents(), 8);
|
||||
}
|
||||
}
|
||||
|
||||
mod high_quality_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn cost_high_landscape_4x3() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2TextToImageNumImages::One, Some(GptImage2TextToImageQuality::High), Some(GptImage2TextToImageSize::Landscape4x3))
|
||||
.calculate_cost_in_cents(), 15);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_high_portrait_4x3() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2TextToImageNumImages::One, Some(GptImage2TextToImageQuality::High), Some(GptImage2TextToImageSize::Portrait4x3))
|
||||
.calculate_cost_in_cents(), 15);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_high_landscape_16x9() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2TextToImageNumImages::One, Some(GptImage2TextToImageQuality::High), Some(GptImage2TextToImageSize::Landscape16x9))
|
||||
.calculate_cost_in_cents(), 16);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_high_portrait_16x9() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2TextToImageNumImages::One, Some(GptImage2TextToImageQuality::High), Some(GptImage2TextToImageSize::Portrait16x9))
|
||||
.calculate_cost_in_cents(), 16);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_high_square() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2TextToImageNumImages::One, Some(GptImage2TextToImageQuality::High), Some(GptImage2TextToImageSize::Square))
|
||||
.calculate_cost_in_cents(), 22);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_high_square_hd() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2TextToImageNumImages::One, Some(GptImage2TextToImageQuality::High), Some(GptImage2TextToImageSize::SquareHd))
|
||||
.calculate_cost_in_cents(), 23);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_high_square_hd_three_images() {
|
||||
assert_eq!(
|
||||
make_request(GptImage2TextToImageNumImages::Three, Some(GptImage2TextToImageQuality::High), Some(GptImage2TextToImageSize::SquareHd))
|
||||
.calculate_cost_in_cents(), 69);
|
||||
}
|
||||
#[test]
|
||||
fn resolution_without_image_size_defaults_to_square_aspect() {
|
||||
assert_eq!(make_request(GptImage2TextToImageNumImages::Two, Some(GptImage2TextToImageQuality::High), None, Some(GptImage2Resolution::FourK),).calculate_cost_in_cents(), 83,);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user