mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-06 10:11:31 +00:00
rembg request
This commit is contained in:
1 parent
4dea85ef91
commit
c1ace07f67
6 files changed
+104
-1
No files matched your search
@@ -0,0 +1 @@
|
||||
pub mod rembg_remove_background;
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
use crate::error::fal_error_plus::FalErrorPlus;
|
||||
use crate::requests::api::image::background::rembg_remove_background::raw_request::{
|
||||
RembgRemoveBackgroundInput, RembgRemoveBackgroundOutput,
|
||||
};
|
||||
use crate::requests::traits::fal_endpoint_trait::FalEndpoint;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RembgRemoveBackgroundRequest {
|
||||
/// URL of the image to remove the background from.
|
||||
pub image_url: String,
|
||||
}
|
||||
|
||||
impl FalEndpoint for RembgRemoveBackgroundRequest {
|
||||
const ENDPOINT: &str = "fal-ai/imageutils/rembg";
|
||||
|
||||
type RawRequest = RembgRemoveBackgroundInput;
|
||||
type RawResponse = RembgRemoveBackgroundOutput;
|
||||
|
||||
fn to_raw_request(&self) -> Result<Self::RawRequest, FalErrorPlus> {
|
||||
Ok(Self::RawRequest {
|
||||
image_url: self.image_url.clone(),
|
||||
crop_to_bbox: None,
|
||||
sync_mode: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::creds::fal_api_key::FalApiKey;
|
||||
use crate::requests::traits::fal_endpoint_trait::FalEndpoint;
|
||||
use errors::AnyhowResult;
|
||||
use std::fs::read_to_string;
|
||||
use test_data::web::image_urls::ERNEST_SCARED_STUPID_IMAGE_URL;
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore] // manually test — requires real API key, incurs costs
|
||||
async fn test_remove_background() -> AnyhowResult<()> {
|
||||
let secret = read_to_string("/Users/bt/Artcraft/credentials/fal_api_key.txt")?;
|
||||
let api_key = FalApiKey::from_str(&secret);
|
||||
|
||||
let request = RembgRemoveBackgroundRequest {
|
||||
image_url: ERNEST_SCARED_STUPID_IMAGE_URL.to_string(),
|
||||
};
|
||||
|
||||
let result = request.send_queue_request(&api_key).await?;
|
||||
println!("Request ID: {:?}", result.request_id);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// NB: Pricing tests are in cost.rs
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
use crate::requests::api::image::background::rembg_remove_background::api::RembgRemoveBackgroundRequest;
|
||||
use crate::requests::traits::fal_request_cost_calculator_trait::{FalRequestCostCalculator, UsdCents};
|
||||
|
||||
impl FalRequestCostCalculator for RembgRemoveBackgroundRequest {
|
||||
fn calculate_cost_in_cents(&self) -> UsdCents {
|
||||
// Pricing: $0.004 per image (less than 1 cent).
|
||||
// We round up to 0 cents for now
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn cost_is_one_cent() {
|
||||
let request = RembgRemoveBackgroundRequest {
|
||||
image_url: "https://example.com/image.jpg".to_string(),
|
||||
};
|
||||
assert_eq!(request.calculate_cost_in_cents(), 0);
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
pub mod api;
|
||||
mod raw_request;
|
||||
mod cost;
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Default)]
|
||||
pub struct RembgRemoveBackgroundInput {
|
||||
pub image_url: String,
|
||||
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub crop_to_bbox: Option<bool>,
|
||||
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub sync_mode: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct RembgRemoveBackgroundFile {
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct RembgRemoveBackgroundOutput {
|
||||
pub image: RembgRemoveBackgroundFile,
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
pub mod angle;
|
||||
pub mod angle;
|
||||
pub mod background;
|
||||
Reference in new issue
Block a user