Skip to content

Commit

Permalink
port identify command
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacherr committed Aug 2, 2024
1 parent 1d615b4 commit 4a03a01
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 2 deletions.
1 change: 1 addition & 0 deletions assyst-common/src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct Authentication {
pub top_gg_webhook_port: u16,
pub filer_key: String,
pub notsoapi: String,
pub rapidapi_token: String,
}

#[derive(Deserialize)]
Expand Down
31 changes: 31 additions & 0 deletions assyst-core/src/command/fun/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::arguments::ImageUrl;
use super::CommandCtxt;
use crate::command::{Availability, Category};
use crate::rest::audio_identification::identify_song_notsoidentify;
use crate::rest::identify::identify_image;

pub mod colour;
pub mod translation;
Expand Down Expand Up @@ -47,3 +48,33 @@ pub async fn findsong(ctxt: CommandCtxt<'_>, input: ImageUrl) -> anyhow::Result<

Ok(())
}

#[command(
description = "AI identify an image",
cooldown = Duration::from_secs(2),
access = Availability::Public,
category = Category::Fun,
usage = "[image]",
examples = ["https://link.to.my/image.png"],
send_processing = true
)]
pub async fn identify(ctxt: CommandCtxt<'_>, input: ImageUrl) -> anyhow::Result<()> {
let result = identify_image(&ctxt.assyst().reqwest_client, &input.0)
.await
.context("Failed to identify image")?;

if let Some(d) = result.description {
let formatted = d
.captions
.iter()
.map(|x| format!("I think it's {} ({:.1}% confidence)", x.text, (x.confidence * 100.0)))
.collect::<Vec<_>>()
.join("\n");

ctxt.reply(formatted).await?;
} else {
ctxt.reply("I really can't describe the picture :flushed:").await?;
}

Ok(())
}
1 change: 1 addition & 0 deletions assyst-core/src/command/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ macro_rules! declare_commands {

declare_commands!(
fun::colour::colour_command,
fun::identify_command,
fun::findsong_command,
fun::translation::bad_translate_command,
fun::translation::translate_command,
Expand Down
37 changes: 37 additions & 0 deletions assyst-core/src/rest/identify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use assyst_common::config::CONFIG;
use reqwest::Client;
use serde::{Deserialize, Serialize};

const IDENTIFY_ROUTE: &str = "https://microsoft-computer-vision3.p.rapidapi.com/analyze?language=en&descriptionExclude=Celebrities&visualFeatures=Description&details=Celebrities";

#[derive(Serialize)]
pub struct IdentifyBody<'a> {
pub url: &'a str,
}

#[derive(Deserialize)]
pub struct IdentifyResponse {
pub description: Option<IdentifyDescription>,
}

#[derive(Deserialize)]
pub struct IdentifyDescription {
pub captions: Vec<IdentifyCaption>,
}

#[derive(Deserialize)]
pub struct IdentifyCaption {
pub text: String,
pub confidence: f32,
}

pub async fn identify_image(client: &Client, url: &str) -> reqwest::Result<IdentifyResponse> {
client
.post(IDENTIFY_ROUTE)
.header("x-rapidapi-key", &CONFIG.authentication.rapidapi_token)
.json(&IdentifyBody { url })
.send()
.await?
.json()
.await
}
1 change: 1 addition & 0 deletions assyst-core/src/rest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod charinfo;
pub mod cooltext;
pub mod eval;
pub mod filer;
pub mod identify;
pub mod patreon;
pub mod r34;
pub mod rest_cache_handler;
Expand Down
4 changes: 2 additions & 2 deletions assyst-core/src/rest/patreon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use serde::{Deserialize, Serialize};
use serde_json::from_str;
use tokio::sync::Mutex;

pub const REFRESH_ROUTE: &str = "https://www.patreon.com/api/oauth2/token";
pub const ROUTE: &str = "https://api.patreon.com/api/oauth2/v2/campaigns/4568373/members?include=user,currently_entitled_tiers&fields%5Buser%5D=social_connections,full_name&fields%5Bmember%5D=is_follower,last_charge_date,last_charge_status,lifetime_support_cents,currently_entitled_amount_cents,patron_status&page%5Bsize%5D=10000";
const REFRESH_ROUTE: &str = "https://www.patreon.com/api/oauth2/token";
const ROUTE: &str = "https://api.patreon.com/api/oauth2/v2/campaigns/4568373/members?include=user,currently_entitled_tiers&fields%5Buser%5D=social_connections,full_name&fields%5Bmember%5D=is_follower,last_charge_date,last_charge_status,lifetime_support_cents,currently_entitled_amount_cents,patron_status&page%5Bsize%5D=10000";

pub const TIER_4_AMOUNT: usize = 2000;
pub const TIER_3_AMOUNT: usize = 1000;
Expand Down
2 changes: 2 additions & 0 deletions config.template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ top_gg_webhook_port = 3000
filer_key = ""
# Authentication key for NotSoAPI, for audio identification.
notsoapi = ""
# RapidAPI token for the `identify` command.
rapidapi_token = ""

# Assyst database information.
[database]
Expand Down

0 comments on commit 4a03a01

Please sign in to comment.