matrix-spam-ml

git clone git://archive.git.mtrnord.blog/MTRNord/matrix-spam-ml.git
Log | Files | Refs | Submodules | README | LICENSE

commit 5c4db680e9f82aedced3078605dd33fcb8c56bf2
parent 64b9f77b9e03f58782caf018d54ae9f0f76cc714
Author: MTRNord <mtrnord1@gmail.com>
Date:   Wed, 28 Sep 2022 12:23:16 +0200

Fix readme float and generate json_schema

Diffstat:
MCargo.lock | 42++++++++++++++++++++++++++++++++++++++++++
MREADME.md | 2+-
Mcrates/model_server/Cargo.toml | 3+++
Acrates/model_server/schemas/prediction.json | 19+++++++++++++++++++
Acrates/model_server/schemas/submit_data.json | 18++++++++++++++++++
Acrates/model_server/schemas/submit_review.json | 14++++++++++++++
Acrates/model_server/schemas/test_data.json | 14++++++++++++++
Mcrates/model_server/src/main.rs | 39+++++++++++++++++++++++++++++++++++++++
8 files changed, 150 insertions(+), 1 deletion(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -341,6 +341,12 @@ dependencies = [ ] [[package]] +name = "dyn-clone" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f94fa09c2aeea5b8839e414b7b841bf429fd25b9c522116ac97ee87856d88b2" + +[[package]] name = "eyre" version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -643,6 +649,7 @@ dependencies = [ "axum-auth", "color-eyre", "once_cell", + "schemars", "serde", "serde_json", "tensorflow", @@ -916,6 +923,30 @@ dependencies = [ ] [[package]] +name = "schemars" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1847b767a3d62d95cbf3d8a9f0e421cf57a0d8aa4f411d4b16525afb0284d4ed" +dependencies = [ + "dyn-clone", + "schemars_derive", + "serde", + "serde_json", +] + +[[package]] +name = "schemars_derive" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af4d7e1b012cb3d9129567661a63755ea4b8a7386d339dc945ae187e403c6743" +dependencies = [ + "proc-macro2", + "quote", + "serde_derive_internals", + "syn", +] + +[[package]] name = "scopeguard" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -948,6 +979,17 @@ dependencies = [ ] [[package]] +name = "serde_derive_internals" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85bf8229e7920a9f636479437026331ce11aa132b4dde37d121944a44d6e5f3c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] name = "serde_json" version = "1.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/README.md b/README.md @@ -41,7 +41,7 @@ It will return a JSON response with the following format: ```json5 { "input_data": "This is a message to be classified", - "score": 0.9999999999999999, # Note that this is a float + "score": 1.1349515e-24, # Note that this is a float } ``` diff --git a/crates/model_server/Cargo.toml b/crates/model_server/Cargo.toml @@ -17,3 +17,6 @@ tensorflow = {version = "0.19.1", features = ["tensorflow_gpu"]} tokio = {version = "1.0", features = ["full"]} tracing = "0.1" tracing-subscriber = {version = "0.3", features = ["env-filter"]} + +[dev-dependencies] +schemars = "0.8.10" diff --git a/crates/model_server/schemas/prediction.json b/crates/model_server/schemas/prediction.json @@ -0,0 +1,18 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Prediction", + "type": "object", + "required": [ + "input_data", + "score" + ], + "properties": { + "input_data": { + "type": "string" + }, + "score": { + "type": "number", + "format": "float" + } + } +} +\ No newline at end of file diff --git a/crates/model_server/schemas/submit_data.json b/crates/model_server/schemas/submit_data.json @@ -0,0 +1,17 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "SubmitData", + "type": "object", + "required": [ + "input_data", + "spam" + ], + "properties": { + "input_data": { + "type": "string" + }, + "spam": { + "type": "boolean" + } + } +} +\ No newline at end of file diff --git a/crates/model_server/schemas/submit_review.json b/crates/model_server/schemas/submit_review.json @@ -0,0 +1,13 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "SubmitReview", + "type": "object", + "required": [ + "input_data" + ], + "properties": { + "input_data": { + "type": "string" + } + } +} +\ No newline at end of file diff --git a/crates/model_server/schemas/test_data.json b/crates/model_server/schemas/test_data.json @@ -0,0 +1,13 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "TestData", + "type": "object", + "required": [ + "input_data" + ], + "properties": { + "input_data": { + "type": "string" + } + } +} +\ No newline at end of file diff --git a/crates/model_server/src/main.rs b/crates/model_server/src/main.rs @@ -143,23 +143,62 @@ async fn submit_for_review( } #[derive(Deserialize, Serialize)] +#[cfg_attr(test, derive(schemars::JsonSchema))] struct TestData { input_data: String, } #[derive(Deserialize, Serialize)] +#[cfg_attr(test, derive(schemars::JsonSchema))] struct Prediction { input_data: String, score: f32, } #[derive(Deserialize, Serialize)] +#[cfg_attr(test, derive(schemars::JsonSchema))] struct SubmitData { input_data: String, spam: bool, } #[derive(Deserialize, Serialize)] +#[cfg_attr(test, derive(schemars::JsonSchema))] struct SubmitReview { input_data: String, } + +#[cfg(test)] +mod test { + use crate::{Prediction, SubmitData, SubmitReview, TestData}; + + #[test] + fn generate_schema() { + let test_data_schema = schemars::schema_for!(TestData); + let prediction_schema = schemars::schema_for!(Prediction); + let submit_data_schema = schemars::schema_for!(SubmitData); + let submit_review_schema = schemars::schema_for!(SubmitReview); + + std::fs::create_dir_all("./schemas").unwrap(); + std::fs::write( + "./schemas/test_data.json", + serde_json::to_string_pretty(&test_data_schema).unwrap(), + ) + .unwrap(); + std::fs::write( + "./schemas/prediction.json", + serde_json::to_string_pretty(&prediction_schema).unwrap(), + ) + .unwrap(); + std::fs::write( + "./schemas/submit_data.json", + serde_json::to_string_pretty(&submit_data_schema).unwrap(), + ) + .unwrap(); + std::fs::write( + "./schemas/submit_review.json", + serde_json::to_string_pretty(&submit_review_schema).unwrap(), + ) + .unwrap(); + } +}