-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
391 lines (371 loc) · 15.7 KB
/
index.tsx
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import Head from "next/head";
import { useReducer, Fragment, useState, useEffect } from "react";
import type { NextPage } from "next";
import { Configuration, OpenAIApi } from "openai";
import { reducerFunc, initFunc } from "../reducers";
import { stat } from "fs";
const Home: NextPage = () => {
const [state, dispatch] = useReducer(reducerFunc, initFunc);
const [btnText, setBtnText] = useState("Copy to Clipboard");
const [apiKey, setApiKey] = useState("");
const [config, setConfig]: any = useState();
const [apiKeyModal, setApiKeyModal] = useState(false);
useEffect(() => {
if (localStorage.getItem("secret-key")?.length) {
const configuration = new Configuration({
apiKey: localStorage.getItem("secret-key") ?? "",
});
setConfig(configuration);
} else {
setApiKeyModal(true);
}
}, [apiKey]);
const openai = new OpenAIApi(config);
const handleSubmit = async (e: any) => {
e.preventDefault();
dispatch({
type: "TOGGLE_LOADER",
});
dispatch({ type: "OPENAI_OUTPUT", payload: "" });
dispatch({ type: "OPENAI_OUTPUT_COPY2CLIPBOARD", payload: "" });
setBtnText("Copy to Clipboard");
const promptString = `Write a cover letter to ${state.emailingTo} from ${state.yourName} for a ${state.roleName} job at ${state.companyName}. I have experience in ${state.experienceIn} I am excited about the job because ${state.excitedAboutJobBecause} I am passionate about ${state.passionateAbout}`;
try {
const op = await openai.createCompletion({
model: "text-davinci-002",
prompt: promptString,
temperature: 0.7,
max_tokens: 2080,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
});
const prompt = op.data.choices[0].text
?.split("\n")
.map((str, i) => <p key={i}>{str}</p>);
dispatch({
type: "OPENAI_OUTPUT_COPY2CLIPBOARD",
payload: op.data.choices[0].text,
});
dispatch({
type: "OPENAI_OUTPUT",
payload: prompt,
});
dispatch({
type: "TOGGLE_LOADER",
});
dispatch({
type: "TOGGLE_OUTPUT",
});
} catch (err) {
console.log(err);
dispatch({
type: "TOGGLE_ERROR",
});
}
};
useEffect(() => {}, []);
return (
<Fragment>
<div className="flex min-h-screen flex-col items-center justify-center py-2">
<Head>
<title>unselected</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main className="flex w-full flex-1 flex-col items-center justify-center px-2 md:px-20 text-center">
<h1 className="text-3xl md:text-5xl font-medium text-blue-600 flex items-center">
<span className="font-light line-through text-gray-500">un</span>
<span className="bg-gray-200 p-1 rounded">selected</span>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
className="w-12 h-12 text-blue-700"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M9 12.75L11.25 15 15 9.75M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="currentColor"
className="w-12 h-12 text-blue-700"
>
<path
fillRule="evenodd"
d="M2.25 12c0-5.385 4.365-9.75 9.75-9.75s9.75 4.365 9.75 9.75-4.365 9.75-9.75 9.75S2.25 17.385 2.25 12zm13.36-1.814a.75.75 0 10-1.22-.872l-3.236 4.53L9.53 12.22a.75.75 0 00-1.06 1.06l2.25 2.25a.75.75 0 001.14-.094l3.75-5.25z"
clipRule="evenodd"
/>
</svg>
</h1>
<p className="mt-3 text-lg md:text-2xl">
Let AI write your cover letter{" "}
</p>
{!state.isError ? (
state.loader ? (
<div className="mt-6">
<p>Loading...(takes 1-2 mins)</p>
</div>
) : (
<div className="w-full lg:w-2/4 border border-slate-500 px-2 py-2 mx-2 my-2 rounded">
{state.renderResult ? (
<>
<div className="px-4 py-2 mx-2 my-4 rounded text-justify">
{state.openAIOutput.length > 0 ? state.openAIOutput : ""}
</div>
<button
onClick={() =>
dispatch({
type: "TOGGLE_OUTPUT",
})
}
className="border border-blue-700 px-6 py-2 rounded transition ease-in-out delay-100 hover:bg-blue-700 hover:text-white"
>
Start Over
</button>
<button
onClick={() => {
navigator.clipboard.writeText(
state.openAIOutputCopy2Clipboard
);
setBtnText("Copied!");
}}
className="mx-2 border border-blue-700 px-6 py-2 rounded transition ease-in-out delay-100 bg-blue-700 hover:bg-blue-600 text-white"
>
{btnText}
</button>
</>
) : (
<form onSubmit={handleSubmit}>
<div className="flex flex-col items-start px-3 py-1 w-full">
<label htmlFor="" className="text-xs text-gray-400">
Company Name
</label>
<input
type="text"
placeholder="Google"
value={state.companyName}
onChange={(e) => {
dispatch({
type: "COMPANY_NAME",
payload: e.target.value,
});
}}
className="border px-1 py-1 my-1 rounded bg-slate-100 w-full text-sm"
/>
</div>
<div className="flex flex-col items-start px-3 py-1 w-full">
<label htmlFor="" className="text-xs text-gray-400">
What role you are applying for?
</label>
<input
type="text"
placeholder="Machine Learning Engineer"
value={state.roleName}
onChange={(e) => {
dispatch({
type: "ROLE_NAME",
payload: e.target.value,
});
}}
className="border px-1 py-2 my-1 rounded bg-slate-100 w-full text-sm"
/>
</div>
<div className="flex flex-col items-start px-3 py-1 w-full">
<label htmlFor="" className="text-xs text-gray-400">
Who are you emailing?
</label>
<input
type="text"
placeholder="John Cena"
value={state.emailingTo}
onChange={(e) => {
dispatch({
type: "EMAILING_TO",
payload: e.target.value,
});
}}
className="border px-1 py-2 my-1 rounded bg-slate-100 w-full text-sm"
/>
</div>
<div className="flex flex-col items-start px-3 py-1 w-full">
<label htmlFor="" className="text-xs text-gray-400">
What is your name?
</label>
<input
type="text"
placeholder="Khushal Sharma"
value={state.yourName}
onChange={(e) => {
dispatch({
type: "YOUR_NAME",
payload: e.target.value,
});
}}
className="border px-1 py-2 my-1 rounded bg-slate-100 w-full text-sm"
/>
</div>
<div className="flex flex-col items-start px-3 py-1 w-full">
<label htmlFor="" className="text-xs text-gray-400">
I have experience in...
</label>
<input
type="text"
placeholder="natural language processing, fraud detection, statistical modeling, and machine learning algorithms."
value={state.experienceIn}
onChange={(e) => {
dispatch({
type: "EXPERIENCE_IN",
payload: e.target.value,
});
}}
className="border px-1 py-2 my-1 rounded bg-slate-100 w-full text-sm"
/>
</div>
<div className="flex flex-col items-start px-3 py-1 w-full">
<label htmlFor="" className="text-xs text-gray-400">
I am excited about the job because...
</label>
<input
type="text"
placeholder="this role will allow me to work on technically challenging problems and create impactful solutions while working with an innovative team."
value={state.excitedAboutJobBecause}
onChange={(e) => {
dispatch({
type: "EXCITED_ABOUT_JOB_BECAUSE",
payload: e.target.value,
});
}}
className="border px-1 py-2 my-1 rounded bg-slate-100 w-full text-sm"
/>
</div>
<div className="flex flex-col items-start px-3 py-1 w-full">
<label htmlFor="" className="text-xs text-gray-400">
I am passionate about...
</label>
<input
type="text"
placeholder="solving problems at the intersection of technology and social good."
value={state.passionateAbout}
onChange={(e) => {
dispatch({
type: "PASSIONATE_ABOUT",
payload: e.target.value,
});
}}
className="border px-1 py-2 my-1 rounded bg-slate-100 w-full text-sm"
/>
</div>
<button
type="submit"
className="border border-blue-700 px-6 py-2 rounded transition ease-in-out delay-100 hover:bg-blue-700 hover:text-white"
>
Submit
</button>
</form>
)}
</div>
)
) : (
<div className="mt-6 w-full lg:w-2/5 bg-red-300 px-2 py-6 text-xl rounded">
<p>There is an error. Please try again later</p>
</div>
)}
{apiKeyModal && (
<div className="fixed z-10 inset-0 overflow-y-auto flex items-center justify-center min-h-screen bg-slate-50 bg-opacity-75 backdrop-blur-sm">
<div className="flex flex-col gap-1 w-2/5 h-18 bg-[#ffffff] shadow-lg rounded-md ">
<div className="text-left px-4 py-2 flex justify-between w-full items-center">
<p className="font-semibold text-lg">Setup your API key</p>
<p
className="text-sm cursor-pointer text-red-500"
onClick={() => setApiKeyModal(false)}
>
❌
</p>
</div>
<hr />
<div className="flex gap-1 items-center px-4">
<label
htmlFor=""
className="text-sm w-20 font-bold text-slate-600
"
>
API Key
</label>
<input
type="password"
className="mt-2 w-full border border-2 border-slate-400 text-sm rounded px-3 py-1 outline-none"
placeholder="your OpenAI secret key"
onChange={(e) => setApiKey(e.target.value)}
/>
</div>
<div className="flex flex-col gap-3 text-left px-5 mt-2 text-sm text-justify text-slate-600">
<p>
Get your personal API key{" "}
<a
href="https://platform.openai.com/account/api-keys"
className="underline"
>
here.
</a>
</p>
<p className="mb-2">
We prioritize the security of your API key and handle it
with utmost care. Your key is exclusively stored on your
browser and never shared with any third-party entity. It is
solely used for the intended purpose of accessing the OpenAI
API and not for any other unauthorized use.
</p>
</div>
<hr />
<div className="px-4 py-2 w-fit mx-auto">
<button
className="bg-blue-600 rounded shadow text-white py-1 px-4"
onClick={() => {
localStorage.setItem("secret-key", apiKey);
setApiKeyModal((modal) => !modal);
}}
>
Submit
</button>
</div>
</div>
</div>
)}
</main>
<footer className="flex h-16 w-full items-center justify-around border-t text-sm lg:text-lg">
<p className="flex gap-3">
<span className="border-b-2 border-gray-700 hover:border-blue-600 hover:border-b-4 transition ease-in-out delay-100">
<a href="/about">about</a>
</span>
<span className="border-b-2 border-gray-700 hover:border-blue-600 hover:border-b-4 transition ease-in-out delay-100">
<p
onClick={() => {
setApiKeyModal(true);
}}
>
API
</p>
</span>
</p>
<p>
a project by{" "}
<span className="border-b-2 border-gray-700 hover:border-blue-600 hover:border-b-4 transition ease-in-out delay-100">
<a href="https://twitter.com/herkuch"> @herkuch</a>
</span>{" "}
and{" "}
<span className="border-b-2 border-gray-700 hover:border-blue-600 hover:border-b-4 transition ease-in-out delay-100">
<a href="https://twitter.com/parthpandyappp"> @parthpandyappp</a>
</span>
</p>
</footer>
</div>
</Fragment>
);
};
export default Home;