-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
99 lines (83 loc) · 3.67 KB
/
script.js
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
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('question-form');
const outputDiv = document.getElementById('questions-output');
const loader = document.getElementById('loader');
const questionsContent = document.getElementById('questions-content');
form.addEventListener('submit', async (event) => {
event.preventDefault();
const jobDescription = document.getElementById('job-description').value;
// Show loader
loader.style.display = 'block';
questionsContent.innerHTML = '';
try {
// First, generate the questions
const response = await fetch('http://localhost:9000/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
description: jobDescription
})
});
if (!response.ok) {
throw new Error('Failed to generate questions.');
}
const data = await response.json();
// Process and display the questions
let html = '';
if (data.questions) {
html = `<pre>${data.questions}</pre>`;
questionsContent.innerHTML = html;
console.log('data.questions',data);
// Prepare the questions data to be an array of objects
const questionsArray = data.questions
.split('\n')
.filter(q => q.trim() !== '' && !q.includes(':')) // Remove empty lines and category headings
.map(q => {
// Assuming difficulty is somehow derived or assigned
const difficulty = 'Easy'; // Example: 'Easy', 'Medium', 'Hard'
return { question: q.trim(), difficulty: difficulty };
});
// console.log('Prepared questions array:', questionsArray); // Log to check format
// Save the generated questions to the database
await saveQuestionsToDatabase('12323',questionsArray,);
} else {
html = '<p>No questions generated.</p>';
questionsContent.innerHTML = html;
}
} catch (error) {
questionsContent.innerHTML = `<p>Error: ${error.message}</p>`;
} finally {
// Hide loader
loader.style.display = 'none';
}
});
async function saveQuestionsToDatabase(projectId, questionsData) {
try {
// Format the data to have projectId and questions array
const formattedData = {
projectId: projectId,
questions: questionsData.map(q => ({
question: q.question,
difficulty: q.difficulty
}))
};
const saveResponse = await fetch('http://localhost:9000/save', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(formattedData)
});
if (!saveResponse.ok) {
const errorResponse = await saveResponse.json();
throw new Error(`Failed to save questions. Server responded with: ${JSON.stringify(errorResponse.detail)}`);
}
const saveData = await saveResponse.json();
console.log('Questions saved successfully:', saveData);
} catch (error) {
console.error('Error saving questions:', error);
}
}
});