ABOUT ME

-

  • Rust: 카카오 Karlo API wrapper
    컴퓨터/Rust 2023. 3. 24. 17:00
    728x90
    반응형

    Karlo

     

    소개

    카카오브레인  ‘카카오디벨로퍼스’에서 카카오브레인의 한층 더 발전된 AI 이미지 모델 ‘Karlo(이하 칼로)’를 API가 있다.

    Generative AI Karlo는 사용자가 입력한 문장을 기반으로 사실적인 이미지와 다양한 작품을 생성할 수 있는 AI 모델이다.

    1억 8천만 장 규모의 이미지와 이미지를 설명하는 텍스트와의 관계를 학습했다고 한다.

    총 3가지 기능이 있다.

    • 사용자가 입력한 단어나 문장을 기반으로 관련된 이미지와 다양한 작품을 생성 (Image gen)
    • 사용자가 업로드한 이미지를 기반으로 관련된 이미지와 다양한 작품을 생성 (Variation gen)
    • 이미지에 그리기 도구로 편집할 부분을 마스킹하고, 입력 단어나 문장(공백 가능)을 기반으로 지운 부분을 채워 새로운 작품을 생성 (Inpaint gen)

     

    여기서 직접 확인해 볼 수 있으니 @확인

     

    Generative AI_Karlo

    Our innovation is to contribute our world-leading technology on large-scale AI models and Digital Human to the tech community, and to create services that improve the value and quality of life.

    kakaobrain.com

     

    Karlo-rs

     

    GitHub - Alfex4936/karlo-rs: 카카오 생성형 인공지능(AI) Karlo API Rust wrapper

    카카오 생성형 인공지능(AI) Karlo API Rust wrapper. Contribute to Alfex4936/karlo-rs development by creating an account on GitHub.

    github.com

    Rust에서 써보려고 wrapper를 만들어 보았다.

    이미지는 base64 기반으로 인코딩/디코딩되어 있어서 base64 라이브러리 사용과 async로 처리했다.

     

    Base64 인코딩 String을 이미지로 변환 하는 함수

    rust
    fn string_to_image(base64_string: &str) -> RgbaImage { let img_data = general_purpose::STANDARD.decode(base64_string).unwrap(); let img = ImageReader::new(Cursor::new(img_data)) .with_guessed_format() .expect("Failed to guess image format") .decode() .expect("Failed to decode image") .to_rgba8(); img }

     

    이미지를 Base64 인코딩된 String로 만드는 함수

    rust
    fn image_to_base64_string(img: &DynamicImage) -> String { let mut buffer = Vec::new(); let mut cursor = Cursor::new(&mut buffer); img.write_to(&mut cursor, image::ImageOutputFormat::Png) .expect("Failed to write image to buffer"); general_purpose::STANDARD.encode(buffer) }

     

    예제

    카카오 디벨로퍼에서 REST_API를 얻은 다음 테스트 해보자.

    "A newyork home, steampunk, snowy"라는 문구로 2개의 이미지를 제작하게 하고

    첫 번째 사진으로 variation을 하나 만들어보게 시켰다.

    rust
    use dotenv::dotenv; use karlo_rs; use std::env; use tokio; #[tokio::main] async fn main() { dotenv().ok(); let api_key = env::var("API_KEY").expect("API_KEY not set in .env file"); // Generate images based on prompt text let text = "A newyork home, steampunk, snowy"; let output_name = "sample_img/output"; // will be png let batch_size = Some(2); // or Some(value) where value is between 1 and 8 match karlo_rs::generate_image(text, output_name, &api_key, batch_size).await { Ok(_) => println!("Image saved to {}", output_name), Err(e) => eprintln!("Error: {}", e), } // Generate images based on input image. let input_path = "sample_img/output_1.png"; let output_name = "sample_img/output_variation"; // will be png let batch_size = None; // or Some(value) where value is between 1 and 8 match karlo_rs::generate_variations(input_path, output_name, &api_key, batch_size).await { Ok(_) => println!("Variation image saved to {}", output_name), Err(e) => eprintln!("Error: {}", e), } }

     

    예제 실행 결과

    Generated image 1
    Generated image 2
    Variation 1

    728x90

    '컴퓨터 > Rust' 카테고리의 다른 글

    댓글