ThanaritKanjanametawat commited on
Commit
57bafce
1 Parent(s): 2addb51

Deploying Roberta Sentinel

Browse files
Files changed (2) hide show
  1. ModelDriver.py +54 -0
  2. app.py +9 -8
ModelDriver.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import RobertaTokenizer, RobertaForSequenceClassification, RobertaModel
2
+ import torch
3
+ import torch.nn as nn
4
+
5
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
6
+ class MLP(nn.Module):
7
+ def __init__(self, input_dim):
8
+ super(MLP, self).__init__()
9
+ self.fc1 = nn.Linear(input_dim, 256)
10
+ self.fc2 = nn.Linear(256, 2)
11
+ self.gelu = nn.GELU()
12
+
13
+ def forward(self, x):
14
+ x = self.gelu(self.fc1(x))
15
+ x = self.fc2(x)
16
+ return x
17
+ def extract_features(text):
18
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
19
+
20
+ tokenizer = RobertaTokenizer.from_pretrained("roberta-base")
21
+ model = RobertaModel.from_pretrained("roberta-base").to(device)
22
+ tokenized_text = tokenizer.encode(text, truncation=True, max_length=512, return_tensors="pt")
23
+ outputs = model(tokenized_text)
24
+ last_hidden_states = outputs.last_hidden_state
25
+ TClassification = last_hidden_states[:, 0, :].squeeze().detach().numpy()
26
+ return TClassification
27
+
28
+ def RobertaSentinelOpenGPTInference(input_text):
29
+ features = extract_features(input_text)
30
+ loaded_model = MLP(768).to(device)
31
+ loaded_model.load_state_dict(torch.load("MLPDictStates/RobertaSentinelOpenGPT.pth"))
32
+
33
+ # Define the tokenizer and model for feature extraction
34
+ with torch.no_grad():
35
+ inputs = torch.tensor(features).to(device)
36
+ outputs = loaded_model(inputs.float())
37
+ _, predicted = torch.max(outputs, 1)
38
+
39
+ return predicted.item()
40
+
41
+ def RobertaSentinelCSAbstractInference(input_text):
42
+ features = extract_features(input_text)
43
+ loaded_model = MLP(768).to(device)
44
+ loaded_model.load_state_dict(torch.load("MLPDictStates/RobertaSentinelCSAbstract.pth"))
45
+
46
+ # Define the tokenizer and model for feature extraction
47
+ with torch.no_grad():
48
+ inputs = torch.tensor(features).to(device)
49
+ outputs = loaded_model(inputs.float())
50
+ _, predicted = torch.max(outputs, 1)
51
+
52
+ return predicted.item()
53
+
54
+
app.py CHANGED
@@ -1,25 +1,26 @@
1
  import streamlit as st
2
  from transformers import pipeline
 
3
 
4
  # Add a title
5
  st.title('GPT Detection Demo')
6
 
7
  # Add 4 options for 4 models
8
  option = st.sidebar.selectbox(
9
- 'Which pipeline do you want to use?',
10
- ('sentiment-analysis', 'ner', 'question-answering', 'text-generation'),
11
  )
12
 
13
- option2 = st.sidebar.selectbox(
14
- 'Which model do you want to use?',
15
- ('gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl'),
16
- )
17
 
18
- pipe = pipeline(option)
19
  text = st.text_area('Enter text here', '')
20
 
21
  if st.button('Generate'):
22
- st.write(pipe(text)[0])
 
 
 
 
 
23
 
24
 
25
 
 
1
  import streamlit as st
2
  from transformers import pipeline
3
+ from ModelDriver import RobertaSentinelOpenGPTInference, RobertaSentinelCSAbstractInference
4
 
5
  # Add a title
6
  st.title('GPT Detection Demo')
7
 
8
  # Add 4 options for 4 models
9
  option = st.sidebar.selectbox(
10
+ 'Which Model do you want to use?',
11
+ ('RobertaSentinelOpenGPT', 'RobertaSentinelCSAbstract'),
12
  )
13
 
 
 
 
 
14
 
 
15
  text = st.text_area('Enter text here', '')
16
 
17
  if st.button('Generate'):
18
+ if option == 'RobertaSentinelOpenGPT':
19
+ result = RobertaSentinelOpenGPTInference(text)
20
+ elif option == 'RobertaSentinelCSAbstract':
21
+ result = RobertaSentinelCSAbstractInference(text)
22
+ st.write(result)
23
+
24
 
25
 
26