-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.R
74 lines (44 loc) · 2.21 KB
/
main.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
install.packages(c('magrittr','glue'))
install.packages('remotes')
install.packages('reticulate')
#remotes::install_github('rstudio/reticulate')
reticulate::install_miniconda()
reticulate::conda_create('r-reticulate', python_version = '3.9')
reticulate::py_install('git+https://github.com/huggingface/transformers.git protobuf',pip = TRUE)
reticulate::py_install('sentencepiece torch==1.7.1+cpu torchvision==0.8.2+cpu torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html',pip = TRUE)
library(magrittr)
# import
args = commandArgs(trailingOnly=TRUE)
user = grep('predict',args,value = T)
# [1] "--issue_comment_body" " /predict az_AZ en_XX Təşkilatçıların maddi rifahı naminə sayı 100 min izləyiciyə çatdıra bilmərik”, - deyə Fransanın mədəniyyət naziri bildirib." "--issue_number"
# [4] "2" "--issue_user" "henry090"
print(args)
args[4] -> issue_
gsub(trimws(user),pattern = '/predict ..... .....', replacement = '') %>% trimws() -> body__
substring(trimws(user),9,21) %>% trimws() %>% strsplit(.,' ') %>% unlist() -> from_to
args[6] -> user_
reticulate::py_run_string(
glue::glue('
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
article_az = "{body__}"
model = MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-50-many-to-many-mmt")
tokenizer = MBart50TokenizerFast.from_pretrained("facebook/mbart-large-50-many-to-many-mmt")
# translate Azerbaijani to English
tokenizer.src_lang = "{from_to[1]}"
encoded_az = tokenizer(article_az, return_tensors="pt")
generated_tokens = model.generate(
**encoded_az,
forced_bos_token_id=tokenizer.lang_code_to_id["{from_to[2]}"]
)
output = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)
f= open("output.txt","w+")
f.write(", ".join(map(str, output)))
f.close()
')
)
prob=paste(readLines("output.txt"), collapse=" ")
reply_message = glue::glue("Hey @{user_}!<br>This was your input: **{body__}**.<br> <br> The is the prediction: **{prob}**")
reply_message = glue::glue('print(f"::set-output name=issue_comment_reply::{reply_message}")')
fileConn<-file("hello.py")
writeLines(reply_message, fileConn)
close(fileConn)