setup-api
This commit is contained in:
parent
8cb8e524dd
commit
43b9edf213
3 changed files with 3126 additions and 0 deletions
2916
Cargo.lock
generated
Normal file
2916
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
17
Cargo.toml
Normal file
17
Cargo.toml
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
[package]
|
||||||
|
name = "whisper-api"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
axum = { version = "0.7.9", features = ["multipart"]}
|
||||||
|
tokio = { version = "1.42.0", features = ["rt-multi-thread"] }
|
||||||
|
tracing-subscriber = "0.3.19"
|
||||||
|
chrono = {version = "0.4.39", features = ["serde"] }
|
||||||
|
serde = { version = "1.0.216", features = ["derive"] }
|
||||||
|
serde_json = "1.0.133"
|
||||||
|
serde_with = "3.11.0"
|
||||||
|
lapin = "2.5.0"
|
||||||
|
confy = "0.6.1"
|
||||||
|
tokio-executor-trait = "2.1.3"
|
||||||
|
tokio-reactor-trait = "1.1.0"
|
||||||
193
src/main.rs
Normal file
193
src/main.rs
Normal file
|
|
@ -0,0 +1,193 @@
|
||||||
|
use crate::chrono::serde::ts_milliseconds;
|
||||||
|
use axum::extract::Multipart;
|
||||||
|
use axum::handler::Handler;
|
||||||
|
use axum::http::StatusCode;
|
||||||
|
use axum::response::IntoResponse;
|
||||||
|
use axum::routing::post;
|
||||||
|
use axum::{Extension, Router};
|
||||||
|
use chrono;
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use lapin::options::BasicPublishOptions;
|
||||||
|
use lapin::{BasicProperties, Channel, Connection, ConnectionProperties};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_with::BoolFromInt;
|
||||||
|
use std::env;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::option::Option;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
tracing_subscriber::fmt::init();
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
// let cfg: AppConfig = confy::load_path(Path::new(&args[1])).expect("Couldn't read config");
|
||||||
|
let cfg: AppConfig = confy::load_path("./config.toml").expect("Couldn't read config");
|
||||||
|
|
||||||
|
let options = ConnectionProperties::default()
|
||||||
|
.with_executor(tokio_executor_trait::Tokio::current())
|
||||||
|
.with_reactor(tokio_reactor_trait::Tokio);
|
||||||
|
let connection = Connection::connect(&cfg.rabbit_mq_config.connection_string, options).await.unwrap();
|
||||||
|
let channel = connection.create_channel().await.unwrap();
|
||||||
|
|
||||||
|
let app = Router::new()
|
||||||
|
.route("/call-upload", post(upload_call))
|
||||||
|
.layer(Extension(cfg.clone()))
|
||||||
|
.layer(Extension(channel));
|
||||||
|
|
||||||
|
let listener = tokio::net::TcpListener::bind(&cfg.web_server_config.listener).await.unwrap();
|
||||||
|
axum::serve(listener, app).await.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn upload_call(Extension(cfg): Extension<AppConfig>, Extension(channel): Extension<Channel>, mut multipart: Multipart) -> axum::response::Response {
|
||||||
|
let mut call_metadata: Option<Call> = None;
|
||||||
|
let mut call_file: Option<PathBuf> = None;
|
||||||
|
|
||||||
|
while let Some(field) = multipart.next_field().await.unwrap() {
|
||||||
|
if field.name().unwrap() == "call_json" {
|
||||||
|
let call: Call = match serde_json::from_str(&field.text().await.unwrap()) {
|
||||||
|
Ok(call) => call,
|
||||||
|
Err(error) => {
|
||||||
|
println!("{}", error);
|
||||||
|
return (StatusCode::BAD_REQUEST, "Failed to parse json").into_response();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
call_metadata = Some(call);
|
||||||
|
} else if field.name().unwrap() == "call_audio" {
|
||||||
|
let file_name = field.file_name().unwrap();
|
||||||
|
let file_path = Path::new(&cfg.file_storage_path).join(&file_name);
|
||||||
|
let data = field.bytes().await.unwrap();
|
||||||
|
let mut file = match File::create(&file_path) {
|
||||||
|
Ok(file) => file,
|
||||||
|
Err(error) => {
|
||||||
|
println!("{}", format!("Could not create file {}, {}", &file_path.display(), error).as_str());
|
||||||
|
return StatusCode::INTERNAL_SERVER_ERROR.into_response();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match file.write_all(&data) {
|
||||||
|
Ok(_) => {
|
||||||
|
call_file = Some(file_path)
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
println!("{}", format!("Could not write to file {}, {}", &file_path.display(), error).as_str());
|
||||||
|
return StatusCode::INTERNAL_SERVER_ERROR.into_response();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let call_metadata: Call = match call_metadata {
|
||||||
|
Some(call_metadata) => call_metadata,
|
||||||
|
None => return (StatusCode::UNPROCESSABLE_ENTITY, "call_json is required").into_response()
|
||||||
|
};
|
||||||
|
let call_file: PathBuf = match call_file {
|
||||||
|
Some(call_file) => call_file,
|
||||||
|
None => return (StatusCode::UNPROCESSABLE_ENTITY, "call_audio is required").into_response()
|
||||||
|
};
|
||||||
|
if call_metadata.call_length < 2 {
|
||||||
|
return (StatusCode::UNPROCESSABLE_ENTITY, "Call too short").into_response()
|
||||||
|
}
|
||||||
|
|
||||||
|
let transcription_request = TranscriptionRequest {
|
||||||
|
audio_file_path: call_file.to_str().unwrap().parse().unwrap(),
|
||||||
|
call_metadata
|
||||||
|
};
|
||||||
|
channel.basic_publish("", "transcribe", BasicPublishOptions::default(), &*serde_json::to_vec(&transcription_request).unwrap(), BasicProperties::default()).await.expect("idk it broke");
|
||||||
|
|
||||||
|
StatusCode::CREATED.into_response()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Default, Debug)]
|
||||||
|
struct TranscriptionRequest {
|
||||||
|
audio_file_path: String,
|
||||||
|
call_metadata: Call
|
||||||
|
}
|
||||||
|
|
||||||
|
#[serde_with::serde_as]
|
||||||
|
#[derive(Serialize, Deserialize, Default, Debug)]
|
||||||
|
struct Call {
|
||||||
|
freq: u32,
|
||||||
|
freq_error: i32,
|
||||||
|
signal: i32,
|
||||||
|
noise: i32,
|
||||||
|
source_num: u32,
|
||||||
|
recorder_num: u32,
|
||||||
|
tdma_slot: u32,
|
||||||
|
#[serde_as(as = "BoolFromInt")]
|
||||||
|
phase2_tdma: bool,
|
||||||
|
#[serde(with = "ts_milliseconds")]
|
||||||
|
start_time: DateTime<Utc>,
|
||||||
|
#[serde(with = "ts_milliseconds")]
|
||||||
|
stop_time: DateTime<Utc>,
|
||||||
|
#[serde_as(as = "BoolFromInt")]
|
||||||
|
emergency: bool,
|
||||||
|
priority: i32,
|
||||||
|
#[serde_as(as = "BoolFromInt")]
|
||||||
|
mode: bool,
|
||||||
|
#[serde_as(as = "BoolFromInt")]
|
||||||
|
duplex: bool,
|
||||||
|
#[serde_as(as = "BoolFromInt")]
|
||||||
|
encrypted: bool,
|
||||||
|
call_length: i32,
|
||||||
|
talkgroup: u64,
|
||||||
|
talkgroup_tag: String,
|
||||||
|
talkgroup_description: String,
|
||||||
|
talkgroup_group_tag: String,
|
||||||
|
talkgroup_group: String,
|
||||||
|
audio_type: String,
|
||||||
|
short_name: String,
|
||||||
|
#[serde(rename = "freqList")]
|
||||||
|
freq_list: Vec<CallFrequency>,
|
||||||
|
#[serde(rename = "srcList")]
|
||||||
|
src_list: Vec<CallSource>,
|
||||||
|
patched_talkgroups: Option<Vec<u32>>
|
||||||
|
}
|
||||||
|
|
||||||
|
#[serde_with::serde_as]
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
struct CallSource {
|
||||||
|
src: i64,
|
||||||
|
#[serde(with = "ts_milliseconds")]
|
||||||
|
time: DateTime<Utc>,
|
||||||
|
pos: f64,
|
||||||
|
#[serde_as(as = "BoolFromInt")]
|
||||||
|
emergency: bool,
|
||||||
|
signal_system: String,
|
||||||
|
tag: String
|
||||||
|
}
|
||||||
|
|
||||||
|
#[serde_with::serde_as]
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
struct CallFrequency {
|
||||||
|
freq: f64,
|
||||||
|
#[serde(with = "ts_milliseconds")]
|
||||||
|
time: DateTime<Utc>,
|
||||||
|
pos: f64,
|
||||||
|
len: f64,
|
||||||
|
error_count: i32,
|
||||||
|
spike_count: i32
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
struct AppConfig {
|
||||||
|
rabbit_mq_config: RabbitMqConfig,
|
||||||
|
web_server_config: WebServerConfig,
|
||||||
|
file_storage_path: String
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for AppConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
panic!("Could not find config file")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
struct RabbitMqConfig {
|
||||||
|
connection_string: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
struct WebServerConfig {
|
||||||
|
listener: String
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue