blob/
blob.rs

1use base64::{engine::general_purpose, Engine as _};
2use gemini_rust::{Gemini, GenerationConfig};
3use std::env;
4use std::fs::File;
5use std::io::Read;
6use std::path::Path;
7
8#[tokio::main]
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10    // Get API key from environment variable
11    let api_key = env::var("GEMINI_API_KEY").expect("GEMINI_API_KEY environment variable not set");
12
13    // Image file path (in the same directory)
14    let image_path = Path::new(file!())
15        .parent()
16        .unwrap_or(Path::new("."))
17        .join("image-example.webp"); // Replace with your image filename
18
19    // Read the image file
20    let mut file = File::open(&image_path)?;
21    let mut buffer = Vec::new();
22    file.read_to_end(&mut buffer)?;
23
24    // Convert to base64
25    let data = general_purpose::STANDARD.encode(&buffer);
26
27    println!("Image loaded: {}", image_path.display());
28
29    // Create client
30    let client = Gemini::new(api_key);
31
32    println!("--- Describe Image ---");
33    let response = client
34        .generate_content()
35        .with_inline_data(data, "image/webp")
36        .with_response_mime_type("text/plain")
37        .with_generation_config(GenerationConfig {
38            temperature: Some(0.7),
39            max_output_tokens: Some(400),
40            ..Default::default()
41        })
42        .execute()
43        .await?;
44
45    println!("Response: {}", response.text());
46
47    Ok(())
48}