Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
// This calendar app will allow users to upload a PDF, extract events from it using OpenAI's GPT model,
// and display them on a big calendar. We'll use react-big-calendar for the calendar component,
// and pdf.js for PDF parsing. The server will handle PDF processing and event extraction.
/** @jsxImportSource https://esm.sh/react */
import React, { useState, useEffect } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
import { Calendar, momentLocalizer } from "https://esm.sh/react-big-calendar";
import moment from "https://esm.sh/moment";
// Set up the localizer for react-big-calendar
const localizer = momentLocalizer(moment);
function App() {
const [events, setEvents] = useState([]);
const [isLoading, setIsLoading] = useState(false);
const handleFileUpload = async (e) => {
const file = e.target.files[0];
if (file) {
setIsLoading(true);
const formData = new FormData();
formData.append('pdf', file);
try {
const response = await fetch('/upload', {
method: 'POST',
body: formData,
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
if (Array.isArray(data)) {
setEvents(data.map(event => ({
...event,
start: new Date(event.start),
end: new Date(event.end),
})));
} else {
throw new Error('Unexpected data format received from server');
}
} catch (error) {
console.error('Error uploading file:', error);
// Use a more user-friendly error message
setEvents([]);
alert('There was an error processing your file. Please try again or use a different PDF.');
} finally {
setIsLoading(false);
}
}
};
return (
<div style={{ height: '100vh', display: 'flex', flexDirection: 'column' }}>
<h1>Calendar Event Extractor</h1>
<input type="file" accept=".pdf" onChange={handleFileUpload} />
{isLoading && <p>Processing PDF and extracting events...</p>}
<div style={{ flex: 1 }}>
<Calendar
localizer={localizer}
events={events}
startAccessor="start"
endAccessor="end"
style={{ height: '100%' }}
/>
</div>
<p>
<a href={import.meta.url.replace("esm.town", "val.town")} target="_blank" rel="noopener noreferrer">
View Source
</a>
</p>
</div>
);
}
function client() {
createRoot(document.getElementById("root")).render(<App />);
}
if (typeof document !== "undefined") { client(); }
async function server(request: Request): Promise<Response> {
const { OpenAI } = await import("https://esm.town/v/std/openai");
const pdfExtractText = await import("https://esm.town/v/pdebieamzn/pdfExtractText");
if (request.method === 'POST' && new URL(request.url).pathname === '/upload') {
const formData = await request.formData();
const pdfFile = formData.get('pdf') as File;
if (!pdfFile) {
return new Response('No file uploaded', { status: 400 });
}
const arrayBuffer = await pdfFile.arrayBuffer();
const fullText = await pdfExtractText.default(arrayBuffer);
const openai = new OpenAI();
const completion = await openai.chat.completions.create({
messages: [
pdebieamzn-calendareventextractor.web.val.run
September 3, 2024