use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use adk_core::{Content, GenerateContentConfig, Llm, LlmRequest, Part};
use adk_model::GeminiModel;
use adk_model::gemini::{GeminiFile, GeminiRequestOptions, VideoMetadata, video_part_annotations};
use adk_model::pricing::PricingCatalog;
use async_trait::async_trait;
use serde_json::Value;
use tokio::sync::Mutex;
pub use adk_model::gemini::MediaResolution;
use super::answer::{ModelAnswer, add_usage, ask_schema, generate, parse_time};
use crate::backend::{BackendDescriptor, BackendId, Locality, PriceRef};
use crate::cloud::extract_json;
use crate::cx::VisionCx;
use crate::digest::Digest;
use crate::egress::{ReleasedClip, ReleasedImage};
use crate::error::{Result, VisionError};
use crate::geometry::{CoordFrame, RawRegion};
use crate::traits::{
VideoAnalysis, VideoAnalyzer, VideoEvent, VideoInput, VideoPlan, VideoRequest,
};
pub const GEMINI_INLINE_LIMIT: usize = 20 * 1024 * 1024;
const GEMINI_HOST: &str = "generativelanguage.googleapis.com";
const UPLOAD_REUSE: Duration = Duration::from_secs(47 * 3600);
pub struct GeminiVideoAnalyzer {
model: Arc<GeminiModel>,
descriptor: BackendDescriptor,
media_resolution: Option<MediaResolution>,
inline_limit: usize,
max_output_tokens: u32,
poll: Duration,
processing_timeout: Duration,
uploads: Mutex<HashMap<Digest, (GeminiFile, Instant)>>,
}
impl std::fmt::Debug for GeminiVideoAnalyzer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("GeminiVideoAnalyzer")
.field("descriptor", &self.descriptor)
.field("media_resolution", &self.media_resolution)
.field("inline_limit", &self.inline_limit)
.finish_non_exhaustive()
}
}
impl GeminiVideoAnalyzer {
pub fn new(model: Arc<GeminiModel>) -> Self {
let name = model.name().to_string();
let mut hosts = adk_core::DataDestination::all_hosts(&model.data_destinations());
if hosts.is_empty() {
hosts.push(GEMINI_HOST.into());
}
let descriptor = BackendDescriptor::new(
name.clone(),
Locality::Remote { hosts },
CoordFrame::SentPixels,
)
.with_provider("gemini")
.with_price(PriceRef::Tokens {
vendor: adk_core::vendor_for_model(&name).to_string(),
model: name,
});
Self {
model,
descriptor,
media_resolution: None,
inline_limit: GEMINI_INLINE_LIMIT,
max_output_tokens: 2_048,
poll: Duration::from_secs(2),
processing_timeout: Duration::from_secs(600),
uploads: Mutex::new(HashMap::new()),
}
}
pub fn with_hosts<H: Into<String>>(mut self, hosts: impl IntoIterator<Item = H>) -> Self {
self.descriptor.locality =
Locality::Remote { hosts: hosts.into_iter().map(Into::into).collect() };
self
}
pub fn with_id(mut self, id: impl Into<String>) -> Self {
self.descriptor.id = BackendId::new(id);
self
}
pub fn with_media_resolution(mut self, resolution: MediaResolution) -> Self {
self.media_resolution = Some(resolution);
self
}
pub fn with_inline_limit(mut self, bytes: usize) -> Self {
self.inline_limit = bytes.min(GEMINI_INLINE_LIMIT);
self
}
pub fn with_max_output_tokens(mut self, tokens: u32) -> Self {
self.max_output_tokens = tokens;
self
}
pub fn with_processing_wait(mut self, interval: Duration, timeout: Duration) -> Self {
self.poll = interval;
self.processing_timeout = timeout;
self
}
pub fn video_tokens(duration_ms: u64, fps: f32, resolution: Option<MediaResolution>) -> u64 {
let per_frame = match resolution {
Some(MediaResolution::Low) => 66,
_ => 258,
};
let fps = if fps.is_finite() && fps > 0.0 { f64::from(fps) } else { 1.0 };
let frames = (duration_ms as f64 * fps / 1_000.0).ceil() as u64;
frames * per_frame + duration_ms.div_ceil(1_000) * 32
}
fn prompt(question: &str, duration_ms: u64) -> String {
format!(
"Question about the video above, which is {} long: {question}\n\nAnswer with JSON \
only: {{\"answer\": <the answer in words>, \"events\": [{{\"start\": \"m:ss.s\", \
\"end\": \"m:ss.s\", \"text\": <what happens>}}]}}, with times from the start of \
the video. List an event only for what the question asks about; if it does not \
happen, say so in the answer and give no events.",
super::timestamp(duration_ms)
)
}
async fn part(
&self,
cx: &VisionCx,
clip: &ReleasedClip,
metadata: &VideoMetadata,
) -> Result<Part> {
let annotations = Some(video_part_annotations(metadata));
let mime_type = clip.mime_type().to_string();
if clip.bytes().len() <= self.inline_limit {
return Ok(Part::InlineData {
mime_type,
data: clip.bytes().to_vec(),
uri: None,
annotations,
});
}
let digest = clip.receipt().egress;
let mut uploads = self.uploads.lock().await;
uploads.retain(|_, (_, at)| at.elapsed() < UPLOAD_REUSE);
let file = match uploads.get(&digest) {
Some((file, _)) => file.clone(),
None => {
cx.check()?;
let backend = self.descriptor.id.to_string();
let unavailable = |e: adk_core::AdkError| VisionError::Unavailable {
backend: backend.clone(),
message: e.to_string(),
};
let uploaded = self
.model
.upload_file(clip.bytes().to_vec(), &mime_type, Some(&digest.to_string()))
.await
.map_err(unavailable)?;
tracing::info!(vision.backend = %backend, gemini.file = %uploaded.name, clip.bytes = clip.bytes().len(), "clip uploaded through the File API");
let ready = tokio::select! {
() = cx.cancellation().cancelled() => return Err(VisionError::Cancelled),
ready = self.model.wait_for_file(&uploaded.name, self.poll, self.processing_timeout) => ready.map_err(unavailable)?,
};
uploads.insert(digest, (ready.clone(), Instant::now()));
ready
}
};
Ok(Part::FileData { mime_type, file_uri: file.uri, annotations })
}
}
#[async_trait]
impl VideoAnalyzer<ReleasedImage> for GeminiVideoAnalyzer {
fn descriptor(&self) -> &BackendDescriptor {
&self.descriptor
}
fn frames(&self, plan: &VideoPlan) -> u64 {
match &plan.clip {
Some(clip) => clip.sampled_frames(plan.sampling.fps),
None => plan.frames(),
}
}
fn max_cost_micro_usd(&self, plan: &VideoPlan, request: &VideoRequest) -> Option<u64> {
let PriceRef::Tokens { vendor, model } = self.descriptor.price.as_ref()? else {
return None;
};
let price = PricingCatalog::standard().lookup(Some(vendor), model)?;
let question = match request {
VideoRequest::Ask { question } => question.len() as u64,
_ => 0,
};
let input = Self::video_tokens(plan.duration_ms, plan.sampling.fps, self.media_resolution)
+ question
+ 1_024;
let today = chrono::Utc::now().date_naive();
let usd = price.max_cost_usd(input, u64::from(self.max_output_tokens), today);
adk_core::usd_to_micro_usd(usd).map(|m| m.saturating_add(1))
}
async fn analyze(
&self,
cx: &VisionCx,
video: &VideoInput<ReleasedImage>,
request: &VideoRequest,
) -> Result<VideoAnalysis<RawRegion>> {
let VideoRequest::Ask { question } = request else {
return Err(VisionError::Unsupported(format!(
"`{}` answers `Ask`; track with `sam-3.1` video",
self.descriptor.id
)));
};
let Some(clip) = video.clip.as_ref() else {
return Err(VisionError::Unsupported(format!(
"`{}` reads clips; ask about sampled frames with a `FramesAnalyzer`",
self.descriptor.id
)));
};
if !clip.is_for(&self.descriptor.id) {
return Err(VisionError::NotReleased {
backend: format!(
"{} (the clip was released to `{}`)",
self.descriptor.id,
clip.receipt().backend
),
});
}
let info = clip.info();
let fps = f64::from(video.sampling.fps);
let metadata = if fps.is_finite() && fps > 0.0 {
VideoMetadata::default().with_fps(fps)
} else {
VideoMetadata::default()
};
let part = self.part(cx, clip, &metadata).await?;
let mut config = GenerateContentConfig {
max_output_tokens: i32::try_from(self.max_output_tokens).ok(),
response_schema: Some(ask_schema("time")),
..GenerateContentConfig::default()
};
if let Some(resolution) = self.media_resolution {
GeminiRequestOptions::default()
.with_media_resolution(resolution)
.insert_into(&mut config);
}
let parts = vec![part, Part::Text { text: Self::prompt(question, info.duration_ms) }];
let llm_request = LlmRequest {
config: Some(config),
..LlmRequest::new(self.model.name(), vec![Content { role: "user".into(), parts }])
};
let plan = VideoPlan {
clip: Some(info.clone()),
frame_sizes: Vec::new(),
duration_ms: info.duration_ms,
sampling: video.sampling,
};
let bound = self.max_cost_micro_usd(&plan, request);
let backend = self.descriptor.id.to_string();
let llm: Arc<dyn Llm> = self.model.clone();
let ModelAnswer { text, usage } = generate(cx, &llm, bound, llm_request, &backend).await?;
let mut analysis = VideoAnalysis::default();
add_usage(&mut analysis.usage, &self.descriptor, usage.as_ref());
analysis.usage.frames = info.sampled_frames(video.sampling.fps);
let root = extract_json(&text).ok_or_else(|| VisionError::Parse {
backend: backend.clone(),
message: format!("the answer holds no JSON: {text}"),
})?;
analysis.answer = root.get("answer").and_then(Value::as_str).map(str::to_string);
for event in root.get("events").and_then(Value::as_array).into_iter().flatten() {
let Some(start) = event.get("start").and_then(parse_time) else { continue };
let start = start.min(info.duration_ms);
let end = event
.get("end")
.and_then(parse_time)
.unwrap_or(start)
.clamp(start, info.duration_ms);
let text = event.get("text").and_then(Value::as_str).unwrap_or_default();
analysis.events.push(VideoEvent::new(start, end, text));
}
analysis.events.sort_by_key(|e| e.start_ms);
Ok(analysis)
}
}