add url crate to handle local path

This commit is contained in:
jhqxxx
2025-12-11 19:33:30 +08:00
parent 42c95da0e3
commit 6430338e95
4 changed files with 39 additions and 16 deletions
Generated
+1
View File
@@ -43,6 +43,7 @@ dependencies = [
"serde_json", "serde_json",
"tokenizers", "tokenizers",
"tokio", "tokio",
"url",
"uuid", "uuid",
] ]
+1
View File
@@ -30,6 +30,7 @@ hound = "3.5.1"
clap = { version = "4.5.51", features = ["derive"] } clap = { version = "4.5.51", features = ["derive"] }
modelscope = "0.1.0" modelscope = "0.1.0"
dirs = "6.0.0" dirs = "6.0.0"
url = "2.5.7"
[features] [features]
flash-attn=["candle-flash-attn"] flash-attn=["candle-flash-attn"]
+12 -2
View File
@@ -1,10 +1,10 @@
use std::collections::HashSet;
use std::io::Cursor; use std::io::Cursor;
use std::{collections::HashSet, path::PathBuf};
use aha_openai_dive::v1::resources::chat::{ use aha_openai_dive::v1::resources::chat::{
ChatCompletionParameters, ChatMessage, ChatMessageContent, ChatMessageContentPart, ChatCompletionParameters, ChatMessage, ChatMessageContent, ChatMessageContentPart,
}; };
use anyhow::{Ok, Result, anyhow}; use anyhow::{Result, anyhow};
use base64::{Engine, engine::general_purpose}; use base64::{Engine, engine::general_purpose};
use candle_core::{DType, Device, Tensor}; use candle_core::{DType, Device, Tensor};
use image::{DynamicImage, ImageBuffer, ImageReader, Rgb, RgbImage, imageops}; use image::{DynamicImage, ImageBuffer, ImageReader, Rgb, RgbImage, imageops};
@@ -48,8 +48,18 @@ pub fn get_image(file: &str) -> Result<DynamicImage> {
img = Some(load_image_from_url(file)?); img = Some(load_image_from_url(file)?);
} }
if file.starts_with("file://") { if file.starts_with("file://") {
// let mut path = file.to_owned();
// path = path.split_off(7);
let path = url::Url::parse(file)?;
let path = path.to_file_path();
let path = match path {
Ok(path) => path,
Err(_) => {
let mut path = file.to_owned(); let mut path = file.to_owned();
path = path.split_off(7); path = path.split_off(7);
PathBuf::from(path)
}
};
img = Some( img = Some(
ImageReader::open(path) ImageReader::open(path)
.map_err(|e| anyhow!(format!("Failed to open file: {}", e)))? .map_err(|e| anyhow!(format!("Failed to open file: {}", e)))?
+22 -11
View File
@@ -1,20 +1,31 @@
use std::time::Instant; use std::{path::PathBuf, str::FromStr};
use aha::utils::tensor_utils::interpolate_bilinear;
use anyhow::Result; use anyhow::Result;
use candle_core::Tensor;
#[test] #[test]
fn messy_test() -> Result<()> { fn messy_test() -> Result<()> {
// RUST_BACKTRACE=1 cargo test -F cuda messy_test -r -- --nocapture // RUST_BACKTRACE=1 cargo test -F cuda messy_test -r -- --nocapture
let device = &candle_core::Device::Cpu; let path_str = "file://./assets/img/ocr_test1.png";
let t = Tensor::arange(0.0f32, 40.0, device)?.broadcast_as((1, 1, 40, 40))?; let path = url::Url::from_str(path_str)?;
println!("t: {}", t); let path = path.to_file_path();
let i_start = Instant::now(); let path = match path {
let t_inter = interpolate_bilinear(&t, (20, 20), Some(false))?; Ok(path) => path,
let i_duration = i_start.elapsed(); Err(_) => {
println!("Time elapsed in interpolate_bilinear is: {:?}", i_duration); let mut path = path_str.to_owned();
println!("t_inter: {}", t_inter); path = path.split_off(7);
PathBuf::from(path)
}
};
println!("to file path: {:?}", path);
// let device = &candle_core::Device::Cpu;
// let t = Tensor::arange(0.0f32, 40.0, device)?.broadcast_as((1, 1, 40, 40))?;
// println!("t: {}", t);
// let i_start = Instant::now();
// let t_inter = interpolate_bilinear(&t, (20, 20), Some(false))?;
// let i_duration = i_start.elapsed();
// println!("Time elapsed in interpolate_bilinear is: {:?}", i_duration);
// println!("t_inter: {}", t_inter);
// let x: Vec<u32> = (0..5).flat_map(|_| 0u32..10).collect(); // let x: Vec<u32> = (0..5).flat_map(|_| 0u32..10).collect();
// let id: Vec<u32> = (0..5).flat_map(|h| vec![h; 10]).collect(); // let id: Vec<u32> = (0..5).flat_map(|h| vec![h; 10]).collect();
// println!("x: {:?}", id); // println!("x: {:?}", id);