Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,37 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
### used to evaluate whether the LLM is jailbroken
|
3 |
+
from transformers import RobertaForSequenceClassification, RobertaTokenizer
|
4 |
+
import torch
|
5 |
|
6 |
+
# os.environ["CUDA_VISIBLE_DEVICES"] = "7"
|
7 |
+
# Load model and tokenizer
|
8 |
+
print("Loading RoBERTa Checkpoint...")
|
9 |
+
ckpt_path = 'hubert233/GPTFuzz'
|
10 |
+
model = RobertaForSequenceClassification.from_pretrained(ckpt_path)
|
11 |
+
tokenizer = RobertaTokenizer.from_pretrained(ckpt_path)
|
12 |
+
print("Loading Done!")
|
13 |
+
|
14 |
+
def predict(sequence):
|
15 |
+
sequences = [sequence]
|
16 |
+
# Encoding sequences
|
17 |
+
inputs = tokenizer(sequences, padding=True, truncation=True, max_length=512, return_tensors="pt")
|
18 |
+
|
19 |
+
# Compute token embeddings
|
20 |
+
with torch.no_grad():
|
21 |
+
outputs = model(**inputs)
|
22 |
+
|
23 |
+
# Get predictions
|
24 |
+
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
25 |
+
# print(predictions.shape)
|
26 |
+
|
27 |
+
# If you want the most likely classes:
|
28 |
+
_, predicted_classes = torch.max(predictions, dim=1)
|
29 |
+
|
30 |
+
# print("Predicted probabilities:", predictions)
|
31 |
+
# print("Predicted classes:", predicted_classes)
|
32 |
+
|
33 |
+
return predicted_classes[0]
|
34 |
+
|
35 |
+
|
36 |
+
iface = gr.Interface(fn=predict, inputs="text", outputs="text")
|
37 |
+
iface.launch()
|