-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add dataset, docker support and instructions
- Loading branch information
1 parent
0fd0fcf
commit 1ef40e7
Showing
7 changed files
with
170 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
dataset.csv | ||
model.json | ||
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
FROM rust as rust-builder | ||
|
||
WORKDIR /usr/src/app | ||
COPY ./Cargo.toml . | ||
COPY ./Cargo.lock . | ||
|
||
# For caching dependencies and avoid rebuilding them | ||
RUN mkdir ./src && echo 'fn main() { println!("Dummy!"); }' > ./src/main.rs | ||
RUN cargo build --release | ||
RUN rm -rf ./src | ||
COPY ./src ./src | ||
RUN touch -a -m ./src/main.rs | ||
RUN cargo build --release | ||
|
||
FROM debian:buster-slim | ||
COPY --from=rust-builder /usr/src/app/target/release/tg_antispam_rs /usr/local/bin/ | ||
WORKDIR /usr/local/bin | ||
|
||
RUN apt-get update | ||
RUN apt-get install wget -y | ||
RUN wget "https://huggingface.co/datasets/thehamkercat/telegram-spam-ham/raw/main/dataset.csv" | ||
|
||
CMD ["tg_antispam_rs"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 TheHamkerCat | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
## Telegram Antispam API | ||
#### Blazingly fast spam classification API built using [Rocket](https://github.com/SergioBenitez/Rocket) Web Framework. | ||
|
||
![Rust](https://forthebadge.com/images/badges/made-with-rust.svg) | ||
|
||
#### Notes | ||
- The classifier works in aggressive mode, it can sometimes classify non-spam messages/emails as spam (when the input is too small) | ||
- The dataset provided may contain some NSFW texts or personal info, it's not thoroughly checked. | ||
- I've included a docker-based example, but you can run it without docker as well. | ||
- `profanity` in response is only there to maintain backwards compatibility for ARQ users, it always returns `false` | ||
|
||
## Installation: | ||
I would suggest using docker compose for this, but it's upto you! | ||
### With Docker compose | ||
|
||
```sh | ||
$ git clone https://github.com/thehamkercat/telegram-antispam-rs | ||
$ cd telegram-antispam-rs | ||
$ docker-compose build | ||
$ docker-compose up | ||
``` | ||
|
||
### With Cargo | ||
|
||
```sh | ||
$ git clone https://github.com/thehamkercat/telegram-antispam-rs | ||
$ cd telegram-antispam-rs | ||
$ cargo run --release | ||
``` | ||
|
||
## Endpoints: | ||
|
||
```http | ||
POST /spam_check HTTP/1.1 | ||
Host: localhost:8000 | ||
Content-Type: application/json | ||
{ | ||
"text": "subscribe to my youtube channel" | ||
} | ||
HTTP/1.1 200 OK | ||
content-length: 59 | ||
content-type: application/json | ||
{ | ||
"spam": 99, | ||
"ham": 1, | ||
"is_spam": true, | ||
"profanity": false, | ||
"spam_probability": 99 | ||
} | ||
``` | ||
|
||
## Usage example: | ||
### Python | ||
|
||
```python | ||
import requests | ||
|
||
url = "http://localhost:8000/spam_check" | ||
data = {"text": "subscribe to my youtube channel"} | ||
|
||
result = requests.post(url, json=data).json() | ||
|
||
print("Is spam:", result["is_spam"]) | ||
print("Spam probability:", result["spam_probability"]) | ||
``` | ||
|
||
### Rust | ||
|
||
```rust | ||
use std::collections::HashMap; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
struct Resp { | ||
spam: u8, | ||
ham: u8, | ||
is_spam: bool, | ||
spam_probability: u8, | ||
profanity: bool | ||
} | ||
|
||
#[tokio::main] | ||
async fn main(){ | ||
let client = reqwest::Client::new(); | ||
let mut map = HashMap::new(); | ||
map.insert("text", "Hello please subscribe to my youtube channel!"); | ||
|
||
let res = client | ||
.post("http://localhost:8000/spam_check") | ||
.json(&map) | ||
.send() | ||
.await | ||
.unwrap(); | ||
|
||
let text_response = res.text().await.unwrap(); | ||
|
||
let json: Result<Resp, _> = serde_json::from_str(text_response.as_str()); | ||
if json.is_ok() { | ||
println!("{:?}", json.unwrap()); | ||
} | ||
} | ||
|
||
// [dependencies] | ||
// reqwest = { version = "0.11", features = ["json"] } | ||
// serde = {version = "1.0.160", features = ["derive"]} | ||
// serde_json = "1.0.96" | ||
// tokio = { version = "1", features = ["full"] } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
version: '3' | ||
services: | ||
tgrs: | ||
build: . | ||
restart: always | ||
ports: | ||
- '8000:8000' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters