-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
47 lines (41 loc) · 1.69 KB
/
script.js
File metadata and controls
47 lines (41 loc) · 1.69 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
import API_KEY from './config.js';
const quoteText = document.getElementById('quote-text');
const generateBtn = document.getElementById('generate-btn');
const quoteType = document.getElementById('quote-type');
const quoteDisplay = document.querySelector('.quote-display');
async function generateQuote() {
try {
generateBtn.disabled = true;
quoteText.textContent = 'Generating your quote...';
const prompt = `Generate a short ${quoteType.value} quote (max 2 sentences). Make it engaging and meaningful.`;
const response = await fetch('https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}`
},
body: JSON.stringify({
contents: [{
parts: [{
text: prompt
}]
}],
generationConfig: {
temperature: 0.7,
topK: 1,
topP: 0.8,
maxOutputTokens: 50,
}
})
});
const data = await response.json();
const generatedQuote = data.candidates[0].content.parts[0].text;
quoteText.textContent = generatedQuote.replace(/^["']|["']$/g, '');
} catch (error) {
quoteText.textContent = 'Failed to generate quote. Please try again.';
console.error('Error:', error);
} finally {
generateBtn.disabled = false;
}
}
generateBtn.addEventListener('click', generateQuote);