izzilis/src/main.rs

68 lines
2.1 KiB
Rust
Raw Normal View History

2021-06-25 20:48:06 +01:00
use std::{error::Error, process, thread, time::Duration};
use chrono::Local;
2021-06-25 20:48:06 +01:00
use rand::Rng;
use crate::publish::ConsolePublisher;
mod bot;
mod config;
mod generator;
mod model;
mod publish;
mod selection;
const CONFIG_PATH: &str = "bot_config.json";
fn main() -> Result<(), Box<dyn Error>> {
let cfg = match config::Config::from(CONFIG_PATH.to_string()) {
Ok(cfg) => cfg,
Err(_) => {
println!(
"Failed reading config at [{}], writing default",
CONFIG_PATH
);
match config::Config::default().save(CONFIG_PATH.to_string()) {
Some(err) => println!("Failed writing file to {}: {}", CONFIG_PATH, err),
None => (),
}
process::exit(1);
}
};
let gpt_model = model::GPTSampleModel::new(
cfg.python_path(),
cfg.gpt_code_path(),
vec![
"generate_unconditional_samples.py".to_string(),
"--model_name".to_string(),
cfg.model_name(),
"--temperature".to_string(),
cfg.temperature(),
"--top_k".to_string(),
cfg.top_k(),
"--nsamples".to_string(),
"1".to_string(),
],
);
// let publisher = publish::FediversePublisher::new(cfg.fediverse_base_url())?;
let publisher = ConsolePublisher::new();
let gen = generator::Generator::new(gpt_model);
let console_selector = selection::ConsoleSelector::new();
let mut bot = bot::IzzilisBot::new(gen, publisher, console_selector);
bot.generate_samples();
2021-06-25 20:48:06 +01:00
let cfg_interval = cfg.interval_seconds();
loop {
let wait_seconds = rand::thread_rng().gen_range(cfg_interval.min()..cfg_interval.max());
let wait_time = Duration::from_secs(wait_seconds);
let now = Local::now();
println!("[{}] Next post is in [{}] seconds", now, wait_seconds);
2021-06-25 20:48:06 +01:00
thread::sleep(wait_time);
match bot.publish() {
Some(err) => println!("Got error from publish: [{}]; continuing", err),
2021-06-25 20:48:06 +01:00
None => println!("publish() call successful"),
}
}
}