-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
286 lines (242 loc) · 9.36 KB
/
Copy pathindex.html
File metadata and controls
286 lines (242 loc) · 9.36 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<!DOCTYPE html>
<style>
html,
body {
height: 95% !important;
margin: 0 !important;
}
footer {
/*max-height: 5% !important;*/
padding: 1em !important
}
section {
min-height: 95% !important
}
.gen-box {
background-color: #fff;
display: block;
padding: 1.25rem;
color: #4a4a4a;
font-size: 1rem;
font-weight: 400;
line-height: 1.25;
}
.gen-border {
height: 0.25em;
background-color: #d5d5d5;
}
.gen-box.warning {
color: red;
font-weight: 700;
}
.brand-text {
display: flex;
align-items: center;
font-size: 1.5em
}
#extra-buttons {
padding-top: 1em;
margin-top: 1em;
border-top: 0.5px solid #d5d5d5;
}
</style>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>AI-Generated Text with GPT-2 using Google Cloud Run</title>
<meta name="title" content="AI-Generated Text with GPT-2 using Google Cloud Run" />
<meta name="description" content="Generate text from OpenAI's GPT-2 model!" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.5/css/bulma.min.css">
<script defer src="https://use.fontawesome.com/releases/v5.3.1/js/all.js"></script>
</head>
<nav class="navbar" role="navigation" aria-label="main navigation">
<div class="navbar-brand">
<span class="icon is-large">
<span class="fa-stack">
<i class="fas fa-circle fa-stack-2x"></i>
<i class="fas fa-robot fa-stack-1x fa-inverse"></i>
</span>
</span>
<div class="brand-text is-family-code is-uppercase has-text-weight-bold">GPT-2</div>
</div>
<div id="navbarBasicExample" class="navbar-menu is-active">
<div class="navbar-start">
<div class="navbar-item">
Use AI to generate custom quotes, with OpenAI's GPT-2 model. Note that the authors shown did NOT say the generated quote.
<br>
The attributed author is simply generated by the model, based on the quote and topic.
</div>
</div>
<div class="navbar-end">
<div class="navbar-item is-hidden-touch">
<div class="buttons">
<a class="button is-light" href="https://github.com/basedrhys/quote-gen" target="_blank">
<span class="icon">
<i class="fab fa-lg fa-github"></i>
</span><span>GitHub</span>
</a>
</div>
</div>
</div>
</div>
</nav>
<body>
<section id="main" class="section">
<div class="container">
<div class="columns is-variable is-5">
<div class="column is-narrow" style="width: 250px;">
<form id="gen-form">
<div class="field">
<label class="label">Quote Topic</label>
<div class="select">
<select id="topic">
<option value="life">Life</option>
<option value="love">Love</option>
<option value="inspirational">Inspirational</option>
<option value="humor">Humor</option>
<option value="death">Death</option>
<option value="art">Art</option>
<option value="education">Education</option>
<option value="books">Books</option>
<option value="change">Change</option>
<option value="time">Time</option>
<option value="beauty">Beauty</option>
<option value="god">God</option>
<option value="happiness">Happiness</option>
<option value="children">Children</option>
<option value="work">Work</option>
<option value="faith">Faith</option>
<option value="funny">Funny</option>
<option value="family">Family</option>
</select>
</div>
</div>
<div class="field">
<label class="label">Generated Quote Length</label>
<div class="control">
<input id="length" class="input" type="text" placeholder="Text input" value="150">
</div>
<p class="help">Max word length of the quote to generate. <em>(max: 1023)</em></p>
</div>
<div class="field">
<label class="label">Temperature</label>
<div class="control">
<input id="temperature" class="input" type="text" placeholder="0.7" value="0.7">
</div>
<p class="help">Controls the generated text "creativity." <em>(the higher the temperature, the more
creative, 0.5 - 1.5 recommended)</em></p>
</div>
<div class="buttons">
<span class="control">
<button type="submit" name="submit" id="generate-text" class="button is-link">
<span class="icon">
<i class="fas fa-md fa-pen"></i>
</span><span>Generate Quote!</span></button>
</span>
</div>
</form>
<div id="extra-buttons" class="buttons">
<span class="control">
<button id="save-image" class="button is-success">
<span class="icon">
<i class="fas fa-md fa-save"></i>
</span><span>Save Image</span></button>
</span>
<span class="control">
<button id="clear-text" class="button is-danger">
<span class="icon">
<i class="fas fa-md fa-trash-alt"></i>
</span><span>Clear Texts</span</button> </span> </div> </div> <div id="model-output" class="column">
<p id="tutorial" class="subtitle"><em>Generated text will appear here!
Use the form to configure GPT-2 and press <strong>Generate Quote</strong>
to get your own quote!
</em></p>
</div>
</div>
</div>
</section>
</body>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous">
</script>
<script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js" crossorigin="anonymous">
</script>
<script type="text/javascript">
var genTopic;
$(function () {
$('#gen-form').submit(function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "https://gpt2-tmsaobdy7a-uc.a.run.app",
dataType: "json",
data: JSON.stringify(getInputValues()),
beforeSend: function (data) {
$('#generate-text').addClass("is-loading");
$('#generate-text').prop("disabled", true);
},
success: function (data) {
$('#generate-text').removeClass("is-loading");
$('#generate-text').prop("disabled", false);
$('#tutorial').remove();
var gentext = data.text;
if ($("#prefix").length & $("#prefix").val() != '') {
var pattern = new RegExp('^' + $("#prefix").val(), 'g');
var gentext = gentext.replace(pattern, '<strong>' + $("#prefix").val() + '</strong>');
}
var gentext = gentext.replace(/\n\n/g, "<div><br></div>").replace(/\n/g, "<div></div>");
gentext = gentext.split("_QUOTE_")[1] // Strip off the prefix
gentext = gentext.split("_END_")[0] // Strip off everything after the end of this quote
var split = gentext.split("_AUTHOR_") // Split it between the quote and the author
var html = '<div class=\"gen-box\"><h6><strong>' + genTopic +
'</strong></h6>' + split[0] + '<br>-' + split[1] + '</div><div class="gen-border"></div>';
$(html).appendTo('#model-output').hide().fadeIn("slow");
},
error: function (jqXHR, textStatus, errorThrown) {
$('#generate-text').removeClass("is-loading");
$('#generate-text').prop("disabled", false);
$('#tutorial').remove();
var html = '<div class="gen-box warning">There was an error generating the text! Please try again. Note that you may need to ' +
'use a different browser/incognito mode, as some browsers will throw a CORS error for the API request.</div><div class="gen-border"></div>';
$(html).appendTo('#model-output').hide().fadeIn("slow");
}
});
});
$('#clear-text').click(function (e) {
$('#model-output').text('')
});
// https://stackoverflow.com/a/51478809
$("#save-image").click(function () {
html2canvas(document.querySelector('#model-output')).then(function (canvas) {
saveAs(canvas.toDataURL(), 'gen_texts.png');
});
});
});
function getInputValues() {
var inputs = {};
$("textarea, input").each(function () {
inputs[$(this).attr('id')] = $(this).val();
});
genTopic = $('#topic option:selected').text();
inputs['prefix'] = "_TOPIC_ " + $('#topic').val() + "_QUOTE_"
return inputs;
}
// https://stackoverflow.com/a/51478809
function saveAs(uri, filename) {
var link = document.createElement('a');
if (typeof link.download === 'string') {
link.href = uri;
link.download = filename;
//Firefox requires the link to be in the body
document.body.appendChild(link);
//simulate click
link.click();
//remove the link when done
document.body.removeChild(link);
} else {
window.open(uri);
}
}
</script>
</html>