Files
aha/tests/test_hunyuan_ocr.rs
T

93 lines
3.2 KiB
Rust
Raw Normal View History

2025-12-03 17:21:01 +08:00
use std::{pin::pin, time::Instant};
use aha::models::{GenerateModel, hunyuan_ocr::generate::HunyuanOCRGenerateModel};
2026-03-31 12:30:12 +08:00
use aha::params::chat::ChatCompletionParameters;
2025-12-03 17:21:01 +08:00
use anyhow::Result;
use rocket::futures::StreamExt;
#[test]
fn hunyuan_ocr_generate() -> Result<()> {
2026-03-01 12:44:14 +08:00
// RUST_BACKTRACE=1 cargo test -F cuda --test test_hunyuan_ocr hunyuan_ocr_generate -r -- --nocapture
2025-12-03 17:21:01 +08:00
let message = r#"
{
2026-03-31 18:45:01 +08:00
"model": "Tencent-Hunyuan/HunyuanOCR",
2025-12-03 17:21:01 +08:00
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"image_url":
{
"url": "file://./assets/img/ocr_test1.png"
}
},
{
"type": "text",
2025-12-10 00:05:46 +08:00
"text": "识别图片中的文字"
2025-12-03 17:21:01 +08:00
}
]
}
]
}
"#;
2026-01-08 00:04:24 +08:00
let save_dir =
aha::utils::get_default_save_dir().ok_or(anyhow::anyhow!("Failed to get save dir"))?;
let model_path = format!("{}/Tencent-Hunyuan/HunyuanOCR/", save_dir);
2025-12-03 17:21:01 +08:00
let mes: ChatCompletionParameters = serde_json::from_str(message)?;
let i_start = Instant::now();
2026-01-08 00:04:24 +08:00
let mut model = HunyuanOCRGenerateModel::init(&model_path, None, None)?;
2025-12-03 17:21:01 +08:00
let i_duration = i_start.elapsed();
println!("Time elapsed in load model is: {:?}", i_duration);
let res = model.generate(mes)?;
println!("generate: \n {:?}", res);
2026-03-14 20:23:07 +08:00
if let Some(usage) = &res.usage {
2026-04-02 22:22:52 +08:00
println!("usage: \n {:?}", usage);
2025-12-10 16:44:20 +08:00
}
2025-12-03 17:21:01 +08:00
Ok(())
}
#[tokio::test]
async fn hunyuan_ocr_stream() -> Result<()> {
2026-05-29 22:10:40 +08:00
// test with cuda: RUST_BACKTRACE=1 cargo test -F cuda --test test_hunyuan_ocr hunyuan_ocr_stream -r -- --nocapture
2025-12-03 17:21:01 +08:00
let message = r#"
{
2026-03-31 18:45:01 +08:00
"model": "Tencent-Hunyuan/HunyuanOCR",
2025-12-03 17:21:01 +08:00
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"image_url":
{
"url": "file://./assets/img/ocr_test1.png"
}
},
{
"type": "text",
"text": "检测并识别图片中的文字,将文本坐标格式化输出。"
}
]
}
]
}
"#;
2026-01-08 00:04:24 +08:00
let save_dir =
aha::utils::get_default_save_dir().ok_or(anyhow::anyhow!("Failed to get save dir"))?;
let model_path = format!("{}/Tencent-Hunyuan/HunyuanOCR/", save_dir);
2025-12-03 17:21:01 +08:00
let mes: ChatCompletionParameters = serde_json::from_str(message)?;
let i_start = Instant::now();
2026-01-08 00:04:24 +08:00
let mut model = HunyuanOCRGenerateModel::init(&model_path, None, None)?;
2025-12-03 17:21:01 +08:00
let i_duration = i_start.elapsed();
println!("Time elapsed in load model is: {:?}", i_duration);
let mut stream = pin!(model.generate_stream(mes)?);
while let Some(item) = stream.next().await {
println!("generate: \n {:?}", item);
}
let i_duration = i_start.elapsed();
println!("Time elapsed in generate is: {:?}", i_duration);
Ok(())
}