Skip to content

Commit

Permalink
v0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Mon-ius committed Apr 24, 2024
1 parent 1a2ed16 commit a111366
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 52 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ name: ci
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CARGO_TERM_COLOR: always
CARGO_LOCA: "rimc-cli/Cargo.toml"
CARGO_BIN: "rimc"
CARGO_LOCA: "rim-cli/Cargo.toml"
CARGO_BIN: "rim"

permissions:
contents: write
Expand All @@ -15,7 +15,7 @@ on:
branches:
- "main"
paths:
- "rimc-cli/Cargo.toml"
- "rim-cli/Cargo.toml"

jobs:
pre:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ target/
Cargo.lock
**/*.rs.bk
*.pdb
config.toml
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["rimc", "rimc-cli"]
members = ["rim", "rim-cli"]
resolver = "2"

[profile.release]
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# Rimc
# Rim

[![CI Status](https://github.com/AUTOM77/Rimc/workflows/ci/badge.svg)](https://github.com/AUTOM77/Rimc/actions?query=workflow:ci)
[![Code Size](https://img.shields.io/github/languages/code-size/AUTOM77/Rimc)](.)
[![CI Status](https://github.com/AUTOM77/Rim/workflows/ci/badge.svg)](https://github.com/AUTOM77/Rim/actions?query=workflow:ci)
[![Code Size](https://img.shields.io/github/languages/code-size/AUTOM77/Rim)](.)
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](./LICENSE)
[![Open Issues](https://img.shields.io/github/issues/AUTOM77/Rimc)](https://github.com/AUTOM77/Rimc/issues)
[![Open Issues](https://img.shields.io/github/issues/AUTOM77/Rim)](https://github.com/AUTOM77/Rim/issues)

> Rimc, a Rust based Multi-Modal Hyper Caption Tool in Parallel
> Rim, a Rust based Multi-Modal Hyper Caption Tool in Parallel
### Usage

1. **Single Image/Video Captioning:**

```bash
rimc -f ${file_path} -c `config.toml`
rim -f ${file_path} -c `config.toml`
```
Rimc generates a `*.txt` file containing the caption for a single image or video.
Rim generates a `*.txt` file containing the caption for a single image or video.

2. **Batch Image/Video Captioning:**

```bash
rimc -d ${dir_path} -c `config.toml`
rim -d ${dir_path} -c `config.toml`
```

For a directory of images or videos, `Rimc` generates a corresponding list of `*.txt` caption files.
For a directory of images or videos, `Rim` generates a corresponding list of `*.txt` caption files.

3. Sample `config.toml` can be found in [config.toml](./config.toml)

Expand Down
9 changes: 5 additions & 4 deletions rimc-cli/Cargo.toml → rim-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[package]
name = "rimc-cli"
version = "0.1.0"
name = "rim-cli"
version = "0.1.1"
edition = "2021"

[dependencies]
librimc = { path = "../rimc" }
librim = { path = "../rim" }
clap = { version= "4.5.4", features=["derive"] }
toml = { version = "0.8.12"}

[[bin]]
name = "rimc"
name = "rim"
path = "src/cli.rs"
40 changes: 40 additions & 0 deletions rim-cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use clap::{Args, Parser};
use librim::client::RimClient;

mod conf;

#[derive(Args)]
#[group(required = true, multiple = false)]
struct Opts {
#[arg(short = 'f', long, name = "FILE")]
file: Option<String>,

#[arg(short = 'd', long, name = "DIR")]
dir: Option<String>,
}

#[derive(Parser)]
struct Cli {
#[command(flatten)]
opt: Opts,

#[arg(short = 'c', long, name = "CONFIG")]
config: String,
}

fn main() {
let cli = Cli::parse();

let (prompt, gemini_keys, _) = conf::load(&cli.config).expect("Failed to decode TOML config");

let client = RimClient::build("gemini", prompt, gemini_keys);

let _ = client.log_prompt();

// let opt = &cli.opt;
// if let Some(file_path) = opt.file.as_deref() {
// let _ = single_cap(file_path, cli.config);
// } else if let Some(dir_path) = opt.dir.as_deref() {
// let _ = batch_cap(dir_path, cli.config);
// }
}
40 changes: 40 additions & 0 deletions rim-cli/src/conf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use toml::Value;
use std::fs;

pub fn load(path: &str) -> Result<(String, Vec<String>, Vec<String>), Box<dyn std::error::Error>> {
let toml_str = fs::read_to_string(path)?;
let toml_value: Value = toml::from_str(&toml_str)?;

let prompt = toml_value
.get("prompt")
.ok_or("Missing 'prompt' key in TOML")?
.get("value")
.ok_or("Missing 'value' key ")?
.as_str()
.ok_or("Invalid type for 'prompt'")?
.to_string();

let gemini_keys = toml_value
.get("gemini")
.ok_or("Missing 'gemini' table in TOML")?
.get("keys")
.ok_or("Missing 'keys' key in 'gemini' table")?
.as_array()
.ok_or("Invalid type for 'gemini.keys'")?
.iter()
.map(|value| value.as_str().unwrap().to_string()) // Assuming keys are strings
.collect();

let gpt4v_keys = toml_value
.get("gpt4v")
.ok_or("Missing 'gpt4v' table in TOML")?
.get("keys")
.ok_or("Missing 'keys' key in 'gpt4v' table")?
.as_array()
.ok_or("Invalid type for 'gpt4v.keys'")?
.iter()
.map(|value| value.as_str().unwrap().to_string()) // Assuming keys are strings
.collect();

Ok((prompt, gemini_keys, gpt4v_keys))
}
4 changes: 2 additions & 2 deletions rimc/Cargo.toml → rim/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "librimc"
name = "librim"
version = "0.1.0"
edition = "2021"

[dependencies]
tokio = { version = "1.37.0", features = ["full"] }

[lib]
name = "librimc"
name = "librim"
path = "src/lib.rs"
49 changes: 49 additions & 0 deletions rim/src/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use super::model::google::Gemini;
use super::model::openai::GPT;

pub trait LLM {
fn generate_caption(&self) -> Result<String, Box<dyn std::error::Error>>;
fn log_prompt(&self) -> &String;
}

impl LLM for Gemini {
fn generate_caption(&self) -> Result<String, Box<dyn std::error::Error>> {
Ok("Gemini Caption".to_string())
}

fn log_prompt(&self) -> &String{
&self.get_prompt()
}
}

impl LLM for GPT {
fn generate_caption(&self) -> Result<String, Box<dyn std::error::Error>> {
Ok("GPT4V Caption".to_string())
}
fn log_prompt(&self) -> &String{
&self.get_prompt()
}
}

pub struct RimClient {
client: Box<dyn LLM>,
}

impl RimClient {
pub fn build(client_type: &str, prompt: String, keys: Vec<String>) -> Self {
let client: Box<dyn LLM> = match client_type {
"gemini" => Box::new(Gemini::build(prompt, keys)),
"gpt" => Box::new(GPT::build(prompt, keys)),
_ => panic!("Invalid client type"),
};
Self { client }
}

pub fn generate_caption(&self) -> Result<String, Box<dyn std::error::Error>> {
self.client.generate_caption()
}

pub fn log_prompt(&self) {
println!("Prompt: {}", self.client.log_prompt());
}
}
3 changes: 3 additions & 0 deletions rimc/src/lib.rs → rim/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub mod model;
pub mod client;

pub fn single_cap(f: &str, conf: String) {
println!("Processing file: {} with {}", f, conf);
}
Expand Down
13 changes: 13 additions & 0 deletions rim/src/model/google.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub struct Gemini {
prompt: String,
keys: Vec<String>,
}

impl Gemini {
pub fn build(prompt: String, keys: Vec<String>) -> Self {
Self { prompt, keys }
}
pub fn get_prompt(&self) -> &String{
&self.prompt
}
}
2 changes: 2 additions & 0 deletions rim/src/model/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod google;
pub mod openai;
13 changes: 13 additions & 0 deletions rim/src/model/openai.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub struct GPT {
prompt: String,
keys: Vec<String>,
}

impl GPT {
pub fn build(prompt: String, keys: Vec<String>) -> Self {
Self { prompt, keys }
}
pub fn get_prompt(&self) -> &String{
&self.prompt
}
}
33 changes: 0 additions & 33 deletions rimc-cli/src/cli.rs

This file was deleted.

0 comments on commit a111366

Please sign in to comment.