File size: 9,445 Bytes
b656ce8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en" data-bs-theme="light">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Educational Assistant</title>
  <!-- Bootstrap 5 -->
  <link
    href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
    rel="stylesheet"
    integrity="sha384-c7DF2uX692G1LB+AU6CtQbmu6Ubfbdr6FLllSVEW9v56MZPPiBYkoQ7RZ2hFOTg1"
    crossorigin="anonymous"
  />
  <!-- Animate.css -->
  <link
    rel="stylesheet"
    href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"
  />
  <!-- AOS -->
  <link
    href="https://cdnjs.cloudflare.com/ajax/libs/aos/2.3.4/aos.css"
    rel="stylesheet"
  />
  <style>
    body {
      background: #f8f9fa;
    }
    .hero {
      background: url('image.jpeg') center/cover no-repeat;
      height: 300px;
      position: relative;
    }
    .hero h1 {
      position: absolute;
      bottom: 20px;
      left: 20px;
      color: white;
      text-shadow: 0 0 10px rgba(0,0,0,0.7);
    }
    .offcanvas-body {
      padding-top: 1rem;
    }
  </style>
</head>
<body>
  <!-- Navbar with sidebar toggle -->
  <nav class="navbar navbar-light bg-white shadow-sm">
    <div class="container-fluid">
      <button
        class="btn btn-outline-primary"
        type="button"
        data-bs-toggle="offcanvas"
        data-bs-target="#sidebar"
        aria-controls="sidebar"
      >
        ☰ Instructions
      </button>
      <span class="navbar-brand mb-0 h1">Educational Assistant</span>
    </div>
  </nav>

  <!-- Offcanvas Sidebar -->
  <div
    class="offcanvas offcanvas-start"
    tabindex="-1"
    id="sidebar"
    aria-labelledby="sidebarLabel"
  >
    <div class="offcanvas-header">
      <h5 id="sidebarLabel">Instructions</h5>
      <button
        type="button"
        class="btn-close text-reset"
        data-bs-dismiss="offcanvas"
        aria-label="Close"
      ></button>
    </div>
    <div class="offcanvas-body">
      <ol>
        <li>Enter your OpenAI API key below (password field).</li>
        <li>Upload a PDF via the “Upload PDF” button.</li>
        <li>Select one:
          <ul>
            <li>Generate Summary</li>
            <li>Generate Quiz</li>
            <li>Ask a Question</li>
            <li>Generate Study Plan</li>
          </ul>
        </li>
        <li>If asking a question, type it in.</li>
        <li>Click “Generate” and then download your result as PDF.</li>
      </ol>
    </div>
  </div>

  <!-- Hero Section -->
  <section class="hero mb-4">
    <h1 class="animate__animated animate__fadeInDown">Study Smarter</h1>
  </section>

  <!-- Main Content -->
  <div class="container" data-aos="fade-up">
    <div class="row g-4">
      <!-- Controls Column -->
      <div class="col-lg-4">
        <div class="card shadow-sm">
          <div class="card-body">
            <div class="mb-3">
              <label for="apiKey" class="form-label">OpenAI API Key</label>
              <input
                type="password"
                class="form-control"
                id="apiKey"
                placeholder="sk-..."
              />
            </div>
            <div class="mb-3">
              <label for="pdfUpload" class="form-label">Upload PDF</label>
              <input
                class="form-control"
                type="file"
                id="pdfUpload"
                accept="application/pdf"
              />
            </div>
            <div class="mb-3">
              <label class="form-label">Option</label>
              <select class="form-select" id="optionSelect">
                <option>Generate Summary</option>
                <option>Generate Quiz</option>
                <option>Ask a Question</option>
                <option>Generate Study Plan</option>
              </select>
            </div>
            <div class="mb-3 d-none" id="questionGroup">
              <label for="questionInput" class="form-label">Your Question</label>
              <input
                type="text"
                class="form-control"
                id="questionInput"
                placeholder="Type your question..."
              />
            </div>
            <div class="d-grid">
              <button class="btn btn-primary" id="generateBtn">
                Generate
              </button>
            </div>
          </div>
        </div>
      </div>

      <!-- Output Column -->
      <div class="col-lg-8">
        <div class="card shadow-sm">
          <div class="card-body">
            <h5 class="card-title">Result</h5>
            <div id="result" style="white-space: pre-wrap; min-height: 200px;">
              <!-- AI response appears here -->
            </div>
            <div class="mt-3 text-end">
              <button
                class="btn btn-success d-none"
                id="downloadBtn"
              >
                Download as PDF
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- Scripts -->
  <!-- Bootstrap JS & dependencies -->
  <script
    src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
    integrity="sha384-E6mRv7/D+K0PtPpK0O1J3DRgIh+tp3loX3f7ogf20+RskmIOrthyxU2048gASBZd"
    crossorigin="anonymous"
  ></script>
  <!-- PDF.js -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/pdf.min.js"></script>
  <!-- jsPDF -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
  <!-- AOS -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/aos/2.3.4/aos.js"></script>

  <script>
    AOS.init();
    const pdfjsLib = window['pdfjs-dist/build/pdf'];
    pdfjsLib.GlobalWorkerOptions.workerSrc =
      'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/pdf.worker.min.js';

    let pdfText = '';

    // Show/hide question input
    document
      .getElementById('optionSelect')
      .addEventListener('change', (e) => {
        const qg = document.getElementById('questionGroup');
        qg.classList.toggle(
          'd-none',
          e.target.value !== 'Ask a Question'
        );
      });

    // Load PDF and extract text
    document
      .getElementById('pdfUpload')
      .addEventListener('change', async (e) => {
        const file = e.target.files[0];
        if (!file) return;
        const arrayBuffer = await file.arrayBuffer();
        const pdf = await pdfjsLib.getDocument({ data: arrayBuffer }).promise;
        let fullText = '';
        for (let i = 1; i <= pdf.numPages; i++) {
          const page = await pdf.getPage(i);
          const txt = await page.getTextContent();
          fullText += txt.items.map((it) => it.str).join(' ') + '\n\n';
        }
        pdfText = fullText.trim();
        alert('PDF loaded: ' + pdf.numPages + ' pages');
      });

    // Generate & display, then prepare PDF download
    document
      .getElementById('generateBtn')
      .addEventListener('click', async () => {
        const key = document.getElementById('apiKey').value.trim();
        if (!key) return alert('Enter your OpenAI API key.');
        if (!pdfText) return alert('Please upload a PDF first.');
        const option = document.getElementById('optionSelect').value;
        let systemPrompt = '';
        let userPrompt = pdfText;
        if (option === 'Generate Summary') {
          systemPrompt = 'You are a smart assistant. Give a summary of the PDF.';
        } else if (option === 'Generate Quiz') {
          systemPrompt =
            'You are a smart assistant. Generate 10 multiple-choice quiz questions with 4 options each (include the correct answer).';
        } else if (option === 'Ask a Question') {
          systemPrompt = 'You are a smart assistant. Answer the user’s question based on the PDF.';
          const q = document.getElementById('questionInput').value.trim();
          if (!q) return alert('Type your question.');
          userPrompt += '\n\nUser question: ' + q;
        } else if (option === 'Generate Study Plan') {
          systemPrompt =
            'You are a smart assistant. Generate a balanced 7-day study plan dividing PDF content into 7 topics.';
        }

        // Call OpenAI
        const responseDiv = document.getElementById('result');
        responseDiv.textContent = 'Generating…';
        const resp = await fetch('https://api.openai.com/v1/chat/completions', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            Authorization: 'Bearer ' + key,
          },
          body: JSON.stringify({
            model: 'gpt-4o-mini',
            messages: [
              { role: 'system', content: systemPrompt },
              { role: 'user', content: userPrompt },
            ],
          }),
        });
        const data = await resp.json();
        const text = data.choices[0].message.content.trim();
        responseDiv.textContent = text;

        // Generate PDF with jsPDF
        const { jsPDF } = window.jspdf;
        const doc = new jsPDF();
        const lines = doc.splitTextToSize(text, 180);
        doc.setFontSize(12);
        doc.text(lines, 10, 10);
        // Show download button
        const dl = document.getElementById('downloadBtn');
        dl.classList.remove('d-none');
        dl.onclick = () => doc.save(`${option.replace(/ /g, '_')}.pdf`);
      });
  </script>
</body>
</html>