Keras
harpomaxx commited on
Commit
659e0b9
1 Parent(s): a3bfe7e

add dga-detector sample code

Browse files
Files changed (1) hide show
  1. dga-detector.R +25 -0
dga-detector.R ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Code for using the DGA detector model
2
+
3
+ library(keras)
4
+ library(plumber)
5
+ library(reticulate)
6
+
7
+ hfhub <- reticulate::import('huggingface_hub')
8
+ model <- hfhub$from_pretrained_keras("harpomaxx/dga-detector")
9
+ modelid="cacic-2018-model"
10
+ valid_characters <- "$abcdefghijklmnopqrstuvwxyz0123456789-_."
11
+ valid_characters_vector <- strsplit(valid_characters,split="")[[1]]
12
+ tokens <- 0:length(valid_characters_vector)
13
+ names(tokens) <- valid_characters_vector
14
+
15
+ # DGA prediction function
16
+ function(domain){
17
+ domain_encoded <-
18
+ sapply(
19
+ unlist(strsplit(tolower(domain),split="")), function(x) tokens [[x]]
20
+ )
21
+ domain_encoded<-pad_sequences(t(domain_encoded),maxlen=45,padding='post', truncating='post')
22
+
23
+ prediction<-predict(model,domain_encoded)
24
+ return(list(modelid=modelid,domain=domain,class=ifelse(prediction[1]>0.9,1,0),probability=prediction[1]))
25
+ }