Skip to content

Commit 2d30e98

Browse files
committed
removed async trait
1 parent de518c3 commit 2d30e98

29 files changed

+704
-606
lines changed

Cargo.toml

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
[package]
22
name = "translators"
3-
version = "0.1.8"
3+
version = "0.1.9"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77
[dependencies]
88
strum = "0.24.1"
99
strum_macros = "0.24.1"
1010
envy = "0.4.1"
11-
reqwest = { version = "0.11", features = ["json"] }
11+
reqwest = { version = "0.11", features = ["json", "blocking"] }
1212
serde = { version = "1.0.159", features = ["derive"] }
1313
serde_json = "1.0.95"
14-
async-trait = "0.1.68"
1514
whatlang = { version = "0.16.2", optional = true }
1615
lingua = { version = "1.4.0", optional = true }
1716
futures = { version = "0.3.28", optional = true }
@@ -28,49 +27,57 @@ md-5 = { version = "0.10.5", optional = true }
2827
base64 = { version = "0.21.0", optional = true }
2928
uuid = { version = "1.3.2", features = ["v4"], optional = true }
3029
sentencepiece = { version = "0.11.1", optional = true }
31-
model-manager = { git = "https://github.com/JustFrederik/model_manager.git", rev = "ab98303", optional = true }
32-
rustyctranslate2 = { git = "https://github.com/JustFrederik/rustyctranslate2", rev = "94a745f", optional = true }
30+
model-manager = { git = "https://github.com/JustFrederik/model_manager.git", rev = "aee991c", optional = true }
31+
rustyctranslate2 = { git = "https://github.com/JustFrederik/rustyctranslate2", rev = "8ac5fa5", optional = true }
32+
#model-manager = { path = "../model_manager", optional = true }
33+
#rustyctranslate2 = { path = "../rustctranslate2", optional = true }
3334
chrono = { version = "0.4.19", optional = true }
3435
sha256 = { version = "1.0.3", optional = true }
35-
md5_alt = { package= 'md5', version = "0.7.0", optional = true }
36+
md5_alt = { package = 'md5', version = "0.7.0", optional = true }
3637
log = "0.4.17"
38+
llm = { git = "https://github.com/rustformers/llm" , branch = "main", optional = true }
39+
rand = { version = "0.8.4", optional = true }
3740

3841
[features]
39-
default = ["whatlang-detector", "online", "retries", "fetch_languages", "generate"]
42+
default = ["whatlang-detector", "online", "fetch_languages", "generate", "offline"]
4043

4144
whatlang-detector = ["dep:whatlang"]
4245
lingua-detector = ["dep:lingua"]
4346
all-detectors = ["whatlang-detector", "lingua-detector"]
4447

45-
retries = ["dep:tokio", "dep:futures", "tokio?/full"]
4648

4749
#Google is always available
4850
deepl = []
4951
mymemory = []
50-
chatgpt = ["dep:chatgpt_rs", "chatgpt_rs?/gpt3"]
52+
chatgpt = ["dep:chatgpt_rs", "chatgpt_rs?/gpt3", "dep:futures"]
5153
libre = []
5254
youdao = ["dep:chrono", "dep:sha256"]
5355
baidu = ["dep:md5_alt"]
5456
papago = []
55-
api = ["chatgpt", "deepl", "libre", "mymemory","baidu", "youdao", "papago"]
57+
api = ["chatgpt", "deepl", "libre", "mymemory", "baidu", "youdao", "papago"]
5658

5759
papago-scrape = ["dep:hmac", "dep:md-5", "dep:base64", "dep:uuid", "uuid?/v4"]
5860
google-scrape = []
5961
youdao-scrape = []
60-
edge-gpt-scrape = ["dep:edge-gpt"]
62+
edge-gpt-scrape = ["dep:edge-gpt", "dep:futures"]
6163
baidu-scrape = ["dep:serde_urlencoded"]
6264
bing-scrape = ["dep:regex", "dep:serde_urlencoded"]
6365
scraper = ["bing-scrape", "papago-scrape", "youdao-scrape", "baidu-scrape", "google-scrape", "edge-gpt-scrape"]
6466

6567
online = ["api", "scraper"]
6668

67-
offline_req = ["dep:sentencepiece", "dep:model-manager", "dep:rustyctranslate2"]
68-
jparacrawl = ["offline_req"]
69-
sugoi = ["offline_req", "dep:regex"]
70-
m2m100 = ["offline_req"]
71-
nllb = ["offline_req"]
69+
ctranslate_req = ["dep:sentencepiece", "dep:model-manager", "dep:rustyctranslate2"]
70+
jparacrawl = ["ctranslate_req"]
71+
sugoi = ["ctranslate_req", "dep:regex"]
72+
m2m100 = ["ctranslate_req", "dep:rand"]
73+
nllb = ["ctranslate_req", "dep:rand"]
74+
bloom = ["dep:llm", "dep:rand"]
75+
gpt2 = ["dep:llm", "dep:rand"]
76+
gptj = ["dep:llm", "dep:rand"]
77+
gptneox = ["dep:llm", "dep:rand"]
78+
llama = ["dep:llm", "dep:rand"]
7279

73-
offline = ["jparacrawl", "sugoi", "m2m100", "nllb"]
80+
offline = ["jparacrawl", "sugoi", "m2m100", "nllb", "bloom", "gpt2", "gptj", "gptneox", "llama"]
7481

7582
all-translators = ["online", "offline"]
7683

src/lib.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,55 @@ pub mod detector;
22
pub mod error;
33
#[cfg(feature = "generate")]
44
pub mod generator;
5-
mod languages;
6-
#[cfg(feature = "offline_req")]
7-
mod model_register;
5+
pub mod languages;
6+
#[cfg(feature = "ctranslate_req")]
7+
pub mod model_register;
88
pub mod translators;
99

1010
#[cfg(test)]
1111
mod tests {
12-
#[cfg(not(feature = "offline_req"))]
12+
#[cfg(not(feature = "ctranslate_req"))]
1313
use std::collections::HashMap;
1414

1515
use dotenv::dotenv;
16-
#[cfg(feature = "offline_req")]
16+
#[cfg(feature = "ctranslate_req")]
1717
use model_manager::model_manager::ModelManager;
18-
use reqwest::Client;
18+
use reqwest::blocking::Client;
1919

2020
use crate::detector;
2121
use crate::detector::Detectors;
2222
use crate::generator::Records;
2323
use crate::languages::Language;
24-
#[cfg(feature = "offline_req")]
24+
#[cfg(feature = "ctranslate_req")]
2525
use crate::model_register::register;
26-
#[cfg(not(feature = "offline_req"))]
26+
#[cfg(not(feature = "ctranslate_req"))]
2727
use crate::translators::chainer::TranslatorInfo;
28-
#[cfg(not(feature = "offline_req"))]
28+
#[cfg(not(feature = "ctranslate_req"))]
2929
use crate::translators::chainer::TranslatorSelectorInfo;
30-
#[cfg(not(feature = "offline_req"))]
30+
#[cfg(not(feature = "ctranslate_req"))]
3131
use crate::translators::context::Context;
3232
use crate::translators::dev::{get_csv_errors, get_languages};
33-
#[cfg(feature = "offline_req")]
33+
#[cfg(feature = "ctranslate_req")]
3434
use crate::translators::offline::ctranslate2::model_management::{
3535
CTranslateModels, ModelLifetime, TokenizerModels,
3636
};
37-
#[cfg(feature = "offline_req")]
37+
#[cfg(feature = "ctranslate_req")]
3838
use crate::translators::offline::ctranslate2::Device;
39-
#[cfg(feature = "offline_req")]
39+
#[cfg(feature = "ctranslate_req")]
4040
use crate::translators::offline::m2m100::{M2M100ModelType, M2M100Translator};
41-
#[cfg(feature = "offline_req")]
41+
#[cfg(feature = "ctranslate_req")]
4242
use crate::translators::offline::ModelFormat;
4343
use crate::translators::scrape::papago::PapagoTranslator;
4444
use crate::translators::tokens::Tokens;
45-
#[cfg(feature = "offline_req")]
45+
#[cfg(feature = "ctranslate_req")]
4646
use crate::translators::translator_structure::TranslatorCTranslate;
4747
use crate::translators::translator_structure::TranslatorLanguages;
4848
use crate::translators::Translator;
49-
#[cfg(not(feature = "offline_req"))]
49+
#[cfg(not(feature = "ctranslate_req"))]
5050
use crate::translators::Translators;
5151

5252
#[tokio::test]
53-
#[cfg(feature = "offline_req")]
53+
#[cfg(feature = "ctranslate_req")]
5454
async fn translate_offline() {
5555
let time = std::time::Instant::now();
5656
let mut mm = ModelManager::new().unwrap();
@@ -80,7 +80,7 @@ mod tests {
8080
}
8181

8282
#[tokio::test]
83-
#[cfg(feature = "offline_req")]
83+
#[cfg(feature = "ctranslate_req")]
8484
async fn models() {
8585
//TODO: better downloader https://github.com/mattgathu/duma/tree/master
8686
let mut mm = ModelManager::new().unwrap();
@@ -105,10 +105,11 @@ mod tests {
105105
print!("{:?}", res);
106106
}
107107

108-
#[tokio::test]
109-
#[cfg(not(feature = "offline_req"))]
110-
async fn translate() {
108+
#[test]
109+
#[cfg(not(feature = "ctranslate_req"))]
110+
fn translate() {
111111
dotenv().ok();
112+
112113
let mut hashmap = HashMap::new();
113114
hashmap.insert(Language::Chinese, Translator::Papago);
114115
let selector = TranslatorSelectorInfo::Selective(
@@ -125,20 +126,18 @@ mod tests {
125126
Some(3),
126127
Detectors::Whatlang,
127128
)
128-
.await
129129
.unwrap();
130+
130131
let chatgpt_context = Context::ChatGPT("This is a text about ...".to_string());
131132
let translation = v
132-
.translate("Hello world".to_string(), None, &[chatgpt_context])
133-
.await
134-
.unwrap();
133+
.translate("Dies ist ein kurzer test der dazu da ist um zu überprüfen ob der übersetzer funtioniert.".to_string(), None, &[chatgpt_context])
134+
.unwrap();
135135
let translations = v
136136
.translate_vec(
137137
vec!["Hello world".to_string(), "This is a test".to_string()],
138138
None,
139139
&[],
140140
)
141-
.await
142141
.unwrap();
143142
println!("{:?}, {:?}", translation, translations);
144143
}
@@ -152,9 +151,8 @@ mod tests {
152151
#[tokio::test]
153152
async fn add_line() {
154153
dotenv().ok();
155-
let vv = PapagoTranslator::get_languages(&Client::new(), &Tokens::get_env().unwrap())
156-
.await
157-
.unwrap();
154+
let vv =
155+
PapagoTranslator::get_languages(&Client::new(), &Tokens::get_env().unwrap()).unwrap();
158156
//println!("{:?}", vv);
159157
let mut v = Records::new().unwrap();
160158
v.add_line("nllb", &vv);

src/main.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// use llm::{ModelArchitecture, VocabularySource};
2+
// use std::convert::Infallible;
3+
// use std::io::Write;
4+
//
5+
// fn main() {
6+
// // load a GGML model from disk
7+
// let llama = llm::load_dynamic(
8+
// ModelArchitecture::Llama,
9+
// std::path::Path::new("/Users/frederik/Downloads/gpt4-x-vicuna-13B.ggmlv3.q4_1.bin"),
10+
// VocabularySource::Model,
11+
// llm::ModelParameters::default(),
12+
// llm::load_progress_callback_stdout,
13+
// )
14+
// .unwrap_or_else(|err| panic!("Failed to load model: {err}"));
15+
//
16+
// // use the model to generate text from a prompt
17+
// let prompt = "A chat between Human and Assistant.\n### Assistant: Hello I am a profressional translator which translates sentences from mangas \n\
18+
// ### Human: Please translate the following sentences for me in the same structure as they are given to japanese. Each translation is numbered with the same number and in a new line.\n\n1. This is a example sentance\n2. Another sentence to translate.\n###Assistant: 1.";
19+
// let mut session = llama.start_session(Default::default());
20+
// let mut buf = String::new();
21+
// let res = session.infer(
22+
// llama.as_ref(),
23+
// &mut rand::thread_rng(),
24+
// &llm::InferenceRequest {
25+
// prompt: prompt.into(),
26+
// parameters: &llm::InferenceParameters::default(),
27+
// play_back_previous_tokens: false,
28+
// maximum_token_count: None,
29+
// },
30+
// &mut Default::default(),
31+
// inference_callback(String::from("###Human"), &mut buf),
32+
// );
33+
//
34+
// println!("res: {}", buf);
35+
// match res {
36+
// Ok(result) => println!("\n\nInference stats:\n{result}"),
37+
// Err(err) => println!("\n{err}"),
38+
// }
39+
// }
40+
//
41+
// fn inference_callback(
42+
// stop_sequence: String,
43+
// buf: &mut String,
44+
// ) -> impl FnMut(llm::InferenceResponse) -> Result<llm::InferenceFeedback, Infallible> + '_ {
45+
// move |resp| match resp {
46+
// llm::InferenceResponse::InferredToken(t) => {
47+
// let mut reverse_buf = buf.clone();
48+
// reverse_buf.push_str(t.as_str());
49+
// if stop_sequence.as_str().eq(reverse_buf.as_str()) {
50+
// buf.clear();
51+
// return Ok(llm::InferenceFeedback::Halt);
52+
// } else if stop_sequence.as_str().starts_with(reverse_buf.as_str()) {
53+
// buf.push_str(t.as_str());
54+
// return Ok(llm::InferenceFeedback::Continue);
55+
// }
56+
//
57+
// if buf.is_empty() {
58+
// print_token(t)
59+
// } else {
60+
// print_token(reverse_buf)
61+
// }
62+
// }
63+
// llm::InferenceResponse::EotToken => Ok(llm::InferenceFeedback::Halt),
64+
// _ => Ok(llm::InferenceFeedback::Continue),
65+
// }
66+
// }
67+
//
68+
// fn print_token(t: String) -> Result<llm::InferenceFeedback, Infallible> {
69+
// print!("{t}");
70+
// std::io::stdout().flush().unwrap();
71+
//
72+
// Ok(llm::InferenceFeedback::Continue)
73+
// }
74+
75+
fn main() {}

src/translators/api/baidu.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use crate::languages::Language;
55
use crate::translators::translator_structure::{
66
TranslationOutput, TranslationVecOutput, TranslatorNoContext,
77
};
8-
use async_trait::async_trait;
9-
use reqwest::Client;
8+
use reqwest::blocking::Client;
109
use serde::{Deserialize, Serialize};
1110
use serde_json::Value;
1211
use std::str::FromStr;
@@ -18,9 +17,8 @@ pub struct BaiduApiTranslator {
1817
key: String,
1918
}
2019

21-
#[async_trait]
2220
impl TranslatorNoContext for BaiduApiTranslator {
23-
async fn translate(
21+
fn translate(
2422
&self,
2523
client: &Client,
2624
query: &str,
@@ -41,10 +39,8 @@ impl TranslatorNoContext for BaiduApiTranslator {
4139
.post(&self.url)
4240
.form(&form)
4341
.send()
44-
.await
4542
.map_err(Error::fetch)?
4643
.json()
47-
.await
4844
.map_err(Error::fetch)?;
4945
let resp = match resp {
5046
Response::Ok(v) => v,
@@ -61,14 +57,14 @@ impl TranslatorNoContext for BaiduApiTranslator {
6157
})
6258
}
6359

64-
async fn translate_vec(
60+
fn translate_vec(
6561
&self,
6662
client: &Client,
6763
query: &[String],
6864
from: Option<Language>,
6965
to: &Language,
7066
) -> Result<TranslationVecOutput, Error> {
71-
let v = self.translate(client, &query.join("\n"), from, to).await?;
67+
let v = self.translate(client, &query.join("\n"), from, to)?;
7268
Ok(TranslationVecOutput {
7369
text: v.text.split('\n').map(|v| v.to_string()).collect(),
7470
lang: v.lang,

0 commit comments

Comments
 (0)