use std::time::Duration;
use base64::{Engine as _, engine::general_purpose::STANDARD};
use image::DynamicImage;
use reqwest::blocking::Client;
use serde_json::{Value, json};
use crate::ocr::{OcrBackend, OcrConfig, OcrPageResult};
const DEFAULT_PROMPT: &str = "Text Recognition:";
const DEFAULT_TIMEOUT_SECS: u64 = 120;
const DEFAULT_MAX_TOKENS: u32 = 4096;
pub struct LlamaServerBackend {
client: Client,
endpoint: String,
prompt: String,
max_tokens: u32,
}
#[derive(bon::Builder)]
#[builder(on(String, into))]
pub struct LlamaServerConfig {
endpoint: String,
#[builder(default = DEFAULT_PROMPT.to_string())]
prompt: String,
#[builder(default = Duration::from_secs(DEFAULT_TIMEOUT_SECS))]
timeout: Duration,
#[builder(default = DEFAULT_MAX_TOKENS)]
max_tokens: u32,
}
impl LlamaServerBackend {
pub fn new(endpoint: impl Into<String>) -> Self {
Self::from_config(LlamaServerConfig::builder().endpoint(endpoint).build())
}
pub fn from_config(config: LlamaServerConfig) -> Self {
let client = Client::builder()
.timeout(config.timeout)
.build()
.expect("reqwest client construction is infallible under normal conditions");
Self {
client,
endpoint: config.endpoint,
prompt: config.prompt,
max_tokens: config.max_tokens,
}
}
fn encode_and_send(
&self,
jpeg_bytes: &[u8],
page_index: u32,
) -> Result<OcrPageResult, Box<dyn std::error::Error + Send + Sync>> {
let prefix = "data:image/jpeg;base64,";
let mut data_uri = String::with_capacity(prefix.len() + (jpeg_bytes.len() * 4 / 3) + 4);
data_uri.push_str(prefix);
STANDARD.encode_string(jpeg_bytes, &mut data_uri);
let payload = json!({
"messages": [{
"role": "user",
"content": [
{ "type": "image_url", "image_url": { "url": data_uri } },
{ "type": "text", "text": self.prompt }
]
}],
"max_tokens": self.max_tokens,
"temperature": 0.0 });
let response = self.client.post(&self.endpoint).json(&payload).send()?;
if !response.status().is_success() {
let status = response.status();
let body = response.text().unwrap_or_default();
return Err(format!("llama-server returned {status}: {body}").into());
}
let json_resp: Value = response.json()?;
let text = json_resp["choices"][0]["message"]["content"]
.as_str()
.ok_or("missing choices[0].message.content in llama-server response")?
.to_string();
Ok(OcrPageResult { page_index, text })
}
}
impl OcrBackend for LlamaServerBackend {
fn run(
&self,
image: &DynamicImage,
page_index: u32,
_config: &OcrConfig,
) -> Result<OcrPageResult, Box<dyn std::error::Error + Send + Sync>> {
let rgb8 = image
.as_rgb8()
.map(std::borrow::Cow::Borrowed)
.unwrap_or_else(|| std::borrow::Cow::Owned(image.to_rgb8()));
let jpeg = turbojpeg::compress_image(&*rgb8, 85, turbojpeg::Subsamp::Sub2x2)
.map_err(|e| format!("turbojpeg compression failed: {e}"))?;
self.encode_and_send(&jpeg, page_index)
}
}