-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.html
175 lines (153 loc) · 6.36 KB
/
extract.html
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE html>
<html>
<head>
<title>GPT-2 Output Detector - File Upload</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.2.2/jszip.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style type="text/css">
* {
box-sizing: border-box;
}
body {
font-family: sans-serif;
margin: 1em;
}
h1 {
font-weight: lighter;
}
a {
text-decoration: none;
color: #666;
}
a:hover {
text-decoration: underline;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/pdf.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/pdf.worker.js"></script>
</head>
<body>
<div id="container">
<a href="index.html">Check an individual file instead</a>
<h1>GPT-2 Output Detector - File Upload</h1>
<p>
Upload a zip file containing text or pdf files to be processed by the GPT-2 output detector model.
</p>
<input class="btn" type="file" id="file-input" accept=".zip">
<div id="results-table">
<table class="table">
<thead class="thead-dark">
<tr>
<th>File Name</th>
<th>Real probability</th>
<th>Fake probability</th>
</tr>
</thead>
<tbody id="results-body">
</tbody>
</table>
</div>
</div>
<script>
// Process the raw text by breaking it down and sending requests
function processText(student, text) {
let words = text.split(" ");
let groups = [];
let word_count = 0;
let current_group = []
let results = []
for (let i = 0; i < words.length; i++) {
let currentWord = words[i];
current_group.push(currentWord);
word_count++;
if ((word_count >= 300 && currentWord[currentWord.length - 1] == '.') || word_count == 400) {
groups.push(current_group);
current_group = [];
word_count = 0;
}
}
groups.push(current_group);
let group_count = groups.length;
for (let i = 0; i < group_count; i++) {
let req = new XMLHttpRequest();
var text = groups[i].join(" ");
req.open('GET', 'https://openai-openai-detector.hf.space/' + '?' + text, true);
req.onreadystatechange = () => {
if (req.readyState !== 4) return;
if (req.status !== 200) throw new Error("HTTP status: " + req.status);
results.push(JSON.parse(req.responseText));
if (results.length == group_count) {
let total_tokens = text.split(" ").length;
let weighted_total_prob = 0;
let total_words = 0;
for (let j = 0; j < group_count; j++) {
let group_words = groups[j].length;
weighted_total_prob += group_words * results[j].real_probability;
total_words += group_words;
}
let avg_real_prob = weighted_total_prob / total_words;
console.log(student, "got real probability of ", avg_real_prob)
let row = document.createElement("tr");
let file_name_cell = document.createElement("td");
file_name_cell.className = "file-name";
file_name_cell.innerHTML = student;
row.appendChild(file_name_cell);
let real_prob_cell = document.createElement("td");
real_prob_cell.className = "real-probability"
real_prob_cell.innerHTML = (avg_real_prob * 100).toFixed(2) + "%";
row.appendChild(real_prob_cell);
let fake_prob_cell = document.createElement("td");
fake_prob_cell.className = "fake-probability"
fake_prob_cell.innerHTML = (100 - (avg_real_prob * 100)).toFixed(2) + "%";
row.appendChild(fake_prob_cell);
document.getElementById("results-body").appendChild(row);
}
};
req.send();
}
}
document.getElementById('file-input').addEventListener('change', function() {
let file = this.files[0];
let filenames = [];
let zip = new JSZip();
zip.loadAsync(file).then(function(zip) {
let promises = [];
for (let fileName in zip.files) {
let file = zip.files[fileName];
if (!file.dir) {
filenames.push(fileName)
if (fileName.split('.').pop() === 'pdf') {
promises.push(file.async("uint8array").then(function(uint8array) {
return pdfjsLib.getDocument({data: uint8array}).promise;
}))
} else {
promises.push(file.async("string"));
}
}
}
Promise.all(promises).then(function(files) {
let results = [];
// Outer loop: Go over all files
console.log("files is ", files)
for (let i = 0; i < files.length; i++) {
let file = files[i];
console.log("file is ", file)
if (filenames[i].split('.').pop() === 'pdf') {
let pdf = file;
files[i].getPage(1).then(function(page) {
var textContent = page.getTextContent();
textContent.then(function (text) {
var text = text.items.filter(i => i.str !== '●').map(i => i.str).join('')
processText(filenames[i], text)
});
});
} else {
processText(filenames[i], file)
}
}
})
})
})
</script>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
</body>