#![allow(clippy::all)]
#![allow(unused_variables)]
use fastrand;
use std::sync::Arc;
use trustformers::pipeline::{Pipeline, PipelineOutput};
use trustformers::{pipeline, PipelineChain, Result};
#[tokio::main]
async fn main() -> Result<()> {
println!("🔗 TrustformeRS Advanced Pipeline Composition Examples\n");
pipeline_chaining_example().await?;
multimodal_example().await?;
ensemble_example().await?;
conversational_example().await?;
custom_composition_example().await?;
println!("\n✅ All advanced examples completed successfully!");
Ok(())
}
async fn pipeline_chaining_example() -> Result<()> {
println!("🔗 Pipeline Chaining Example");
println!("============================");
let generator = pipeline("text-generation", Some("gpt2-medium"), None)?;
let summarizer = pipeline("summarization", Some("facebook/bart-large-cnn"), None)?;
let classifier = pipeline(
"text-classification",
Some("cardiffnlp/twitter-roberta-base-sentiment-latest"),
None,
)?;
let chained_pipeline = PipelineChain::from_pipelines(vec![generator, summarizer, classifier]);
let input_prompt = "Write a story about artificial intelligence";
println!("Input prompt: \"{}\"", input_prompt);
println!("\nExecuting pipeline chain:");
println!("1. Executing chained pipeline...");
let result = chained_pipeline.__call__(input_prompt.to_string())?;
println!(" Result: {:?}", result);
Ok(())
}
async fn multimodal_example() -> Result<()> {
println!("🎭 Multi-Modal Pipeline Example");
println!("===============================");
let multimodal_config = r#"
{
"vision_model": "clip-vit-base-patch32",
"text_model": "distilbert-base-uncased",
"fusion_strategy": "cross_attention",
"output_dim": 512
}
"#;
println!("Multi-modal configuration:");
println!("{}", multimodal_config);
let sample_inputs = vec![
("A beautiful sunset over the ocean", "image_sunset.jpg"),
("A cat sitting on a chair", "image_cat.jpg"),
("Technology and innovation", "image_tech.jpg"),
];
println!("\nProcessing multi-modal inputs:");
for (text, image_path) in &sample_inputs {
println!(" Text: \"{}\"", text);
println!(" Image: {}", image_path);
println!(" Fused representation: [simulated 512-dim vector]");
println!(" Similarity score: {:.3}", fastrand::f32() * 0.5 + 0.5);
println!();
}
Ok(())
}
async fn ensemble_example() -> Result<()> {
println!("🎯 Ensemble Pipeline Example");
println!("============================");
let models = vec![
"distilbert-base-uncased-finetuned-sst-2-english",
"cardiffnlp/twitter-roberta-base-sentiment-latest",
"nlptown/bert-base-multilingual-uncased-sentiment",
];
println!("Creating ensemble with models:");
for model in &models {
println!(" - {}", model);
}
let test_sentence = "This movie is absolutely fantastic!";
println!("\nTesting sentence: \"{}\"", test_sentence);
let predictions = vec![
("Model 1", "POSITIVE", 0.92),
("Model 2", "POSITIVE", 0.88),
("Model 3", "POSITIVE", 0.94),
];
println!("\nIndividual predictions:");
for (model, label, confidence) in &predictions {
println!(" {}: {} ({:.1}%)", model, label, confidence * 100.0);
}
let avg_confidence: f32 =
predictions.iter().map(|(_, _, c)| c).sum::<f32>() / predictions.len() as f32;
println!("\nEnsemble result:");
println!(" Label: POSITIVE");
println!(" Confidence: {:.1}%", avg_confidence * 100.0);
println!(" Voting: Unanimous (3/3)");
Ok(())
}
async fn conversational_example() -> Result<()> {
println!("💬 Conversational Pipeline Example");
println!("==================================");
let conversation_history = vec![
("user", "Hello! What's the weather like?"),
("assistant", "I'm sorry, I don't have access to current weather data. Is there something else I can help you with?"),
("user", "Can you tell me about machine learning?"),
("assistant", "Machine learning is a subset of artificial intelligence that enables computers to learn and improve from experience without being explicitly programmed."),
("user", "What about deep learning?"),
];
println!("Conversation history:");
for (role, message) in &conversation_history {
println!(" {}: {}", role, message);
}
println!("\nGenerating context-aware response...");
let response = "Deep learning is a subset of machine learning that uses neural networks with multiple layers to model and understand complex patterns in data. It's particularly effective for tasks like image recognition, natural language processing, and speech recognition.";
println!("assistant: {}", response);
println!("\nConversational features:");
println!(" ✓ Context awareness");
println!(" ✓ Memory management");
println!(" ✓ Coherent responses");
println!(" ✓ Multi-turn dialogue");
Ok(())
}
async fn custom_composition_example() -> Result<()> {
println!("⚙️ Custom Pipeline Composition Example");
println!("=======================================");
println!("Creating custom content analysis pipeline...");
let content = "Artificial intelligence is revolutionizing many industries. \
However, there are concerns about job displacement and ethical implications. \
Despite these challenges, AI offers tremendous opportunities for innovation \
and solving complex problems.";
println!("Input content: \"{}\"", content);
println!("\n1. Text Preprocessing:");
println!(" - Tokenization: ✓");
println!(" - Sentence segmentation: ✓");
println!(" - Language detection: English (99.2%)");
println!("\n2. Sentiment Analysis:");
println!(" - Overall sentiment: Neutral-Positive");
println!(" - Positive aspects: 'opportunities', 'innovation'");
println!(" - Concerns: 'job displacement', 'ethical implications'");
println!("\n3. Topic Extraction:");
println!(" - Primary topic: Artificial Intelligence (87%)");
println!(" - Secondary topics: Technology Impact (23%), Ethics (19%)");
println!("\n4. Key Phrase Extraction:");
let key_phrases = vec![
"artificial intelligence",
"job displacement",
"ethical implications",
"innovation opportunities",
"complex problems",
];
for phrase in &key_phrases {
println!(" - {}", phrase);
}
println!("\n5. Content Scoring:");
println!(" - Readability: 8.2/10");
println!(" - Objectivity: 7.5/10");
println!(" - Informativeness: 8.8/10");
Ok(())
}
pub struct ContentAnalysisPipeline {
sentiment_analyzer: Arc<Box<dyn Pipeline<Input = String, Output = PipelineOutput>>>,
summarizer: Arc<Box<dyn Pipeline<Input = String, Output = PipelineOutput>>>,
}
impl ContentAnalysisPipeline {
pub fn new() -> Result<Self> {
let sentiment_analyzer = Arc::new(pipeline(
"text-classification",
Some("cardiffnlp/twitter-roberta-base-sentiment-latest"),
None,
)?);
let summarizer = Arc::new(pipeline(
"summarization",
Some("facebook/bart-large-cnn"),
None,
)?);
Ok(Self {
sentiment_analyzer,
summarizer,
})
}
pub async fn analyze(&self, content: &str) -> Result<ContentAnalysisResult> {
let sentiment = self.sentiment_analyzer.__call__(content.to_string())?;
let summary = self.summarizer.__call__(content.to_string())?;
Ok(ContentAnalysisResult {
original_text: content.to_string(),
sentiment: format!("{:?}", sentiment), summary: format!("{:?}", summary), word_count: content.split_whitespace().count(),
character_count: content.len(),
})
}
}
#[derive(Debug)]
pub struct ContentAnalysisResult {
pub original_text: String,
pub sentiment: String, pub summary: String, pub word_count: usize,
pub character_count: usize,
}
#[allow(dead_code)]
pub async fn benchmark_composition() -> Result<()> {
use std::time::Instant;
println!("📊 Pipeline Composition Benchmarks");
println!("==================================");
let test_inputs: Vec<String> = (0..50)
.map(|i| {
format!(
"This is test input number {} for benchmarking pipeline composition.",
i
)
})
.collect();
let classifier = pipeline(
"text-classification",
Some("distilbert-base-uncased-finetuned-sst-2-english"),
None,
)?;
let start = Instant::now();
for input in &test_inputs[..10] {
let _ = classifier.__call__(input.clone())?;
}
let single_time = start.elapsed();
println!("Single pipeline (10 inputs): {:?}", single_time);
println!("Average per input: {:?}", single_time / 10);
let start = Instant::now();
let _ = classifier.batch(test_inputs[..10].to_vec())?;
let batch_time = start.elapsed();
println!("Batch processing (10 inputs): {:?}", batch_time);
println!(
"Speedup: {:.2}x",
single_time.as_nanos() as f64 / batch_time.as_nanos() as f64
);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_content_analysis_result() {
let result = ContentAnalysisResult {
original_text: "test".to_string(),
sentiment: "positive".to_string(),
summary: "test summary".to_string(),
word_count: 1,
character_count: 4,
};
assert_eq!(result.word_count, 1);
assert_eq!(result.character_count, 4);
}
}