m1guelpf / hello-world-rust

The first Rust-powered model on Replicate.

  • Public
  • 35 runs
  • GitHub
  • License

Run time and cost

This model costs approximately $0.00044 to run on Replicate, or 2272 runs per $1, but this varies depending on your inputs. It is also open source and you can run it on your own computer with Docker.

This model runs on CPU hardware. Predictions typically complete within 5 seconds.

Readme

This is a very simple model that takes a string as input and returns a string with “hello” prepended to the input.

It runs fully on Rust (no Python or Go used anywhere), which is pretty cool I’d say. Here’s the source:

use cog_rust::Cog;
use anyhow::Result;
use schemars::JsonSchema;
use async_trait::async_trait;

#[derive(serde::Deserialize, JsonSchema)]
struct ModelRequest {
    /// Text to prefix with 'hello '
    text: String,
}

struct ExampleModel {
    prefix: String,
}

#[async_trait]
impl Cog for ExampleModel {
    type Request = ModelRequest;
    type Response = String;

    async fn setup() -> Result<Self> {
        Ok(Self {
            prefix: "hello".to_string(),
        })
    }

    fn predict(&self, input: Self::Request) -> Result<Self::Response> {
        Ok(format!("{} {}", self.prefix, input.text))
    }
}

cog_rust::start!(ExampleModel);