Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
187 lines (151 loc) · 5.61 KB
/
Copy pathapp.js
File metadata and controls
187 lines (151 loc) · 5.61 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
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
let combinedCSV = null;
const BACKEND_URL = "https://gen-ai-dataset-finder-1.onrender.com";
async function searchDatasets() {
console.log("searchDatasets function triggered"); // Check if this is logged
const keyword = document.getElementById("keywordInput").value.trim(); // Get the search keyword
const output = document.getElementById("outputText");
const dropdown = document.getElementById("datasetDropdown");
if (!keyword) {
output.innerText = "⚠️ Please enter a keyword first.";
return;
}
output.innerText = "🔍 Searching for datasets...";
try {
// Send POST request with the search query
console.log("Sending request to backend...");
const response = await fetch(`${BACKEND_URL}/search`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query: keyword })
});
console.log("Response Status: ", response.status); // Log the response status
const data = await response.json();
console.log("Data received from backend:", data); // Log the data
// Process the response
dropdown.innerHTML = '<option disabled selected>Select a dataset...</option>';
data.results.forEach(item => {
const option = document.createElement("option");
option.value = item.url;
option.text = `${item.title} (${item.source})`;
dropdown.appendChild(option);
});
output.innerText = `✅ ${data.results.length} results found. Select a dataset.`;
} catch (err) {
console.error("Error during fetch:", err);
output.innerText = "❌ Could not fetch datasets.";
}
}
async function generateData() {
const url = document.getElementById("datasetDropdown").value;
const rows = document.getElementById("rowToggle").value;
const output = document.getElementById("outputText");
const downloadSection = document.getElementById("downloadSection");
// Step 1: Validate inputs
if (!url) {
output.innerText = "⚠️ Select a dataset first.";
return;
}
const parsedRows = parseInt(rows);
if (isNaN(parsedRows) || parsedRows <= 0) {
output.innerText = "⚠️ Enter a valid number of rows.";
return;
}
// Step 2: Prepare request
const requestBody = {
url: url,
rows: parsedRows,
};
console.log("📤 Sending request body:", requestBody);
output.innerText = `⏳ Generating ${parsedRows} synthetic rows...`;
downloadSection.style.display = "none";
try {
// Step 3: Send request
const response = await fetch(`${BACKEND_URL}/generate_rows_only`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(requestBody),
});
console.log(`🔁 Response Status: ${response.status} ${response.statusText}`);
// Step 4: Read response as raw text
const rawResponse = await response.text();
console.log("📥 Raw response:", rawResponse);
// Step 5: Check if status is bad
if (!response.ok) {
output.innerText = `❌ Server error: ${response.status}`;
return;
}
// Step 6: Try to parse the JSON
let data;
try {
data = JSON.parse(rawResponse);
} catch (parseError) {
console.error("❌ JSON parsing failed. Backend sent non-JSON:", rawResponse);
output.innerText = "❌ Response was not valid JSON.";
return;
}
// Step 7: Success!
console.log("✅ Parsed JSON:", data);
if (data.combined_csv) {
combinedCSV = data.combined_csv;
downloadSection.style.display = "block";
output.innerText = data.synthetic_csv;
} else if (data.error) {
output.innerText = `❌ Error: ${data.error}`;
downloadSection.style.display = "none";
} else {
output.innerText = "❌ No data returned from server.";
downloadSection.style.display = "none";
}
} catch (err) {
// Step 8: Catch network or code errors
console.error("❌ Network or unexpected error:", err);
output.innerText = "❌ Something went wrong while contacting the server.";
downloadSection.style.display = "none";
}
}
// fetch('/generate_rows_only', {
// method: 'POST',
// headers: { 'Content-Type': 'application/json' },
// body: JSON.stringify({ url: selectedUrl, rows: numRows })
// })
// .then(res => res.json())
// .then(data => {
// generatedCSV = data.synthetic_csv; // show this on screen
// combinedCSV = data.combined_csv; // use this for download
// // display generated rows
// document.getElementById("csvPreview").textContent = generatedCSV;
// // enable download button
// document.getElementById("downloadBtn").disabled = false;
// });
async function generateSummary() {
const url = document.getElementById("datasetDropdown").value;
const output = document.getElementById("outputText");
if (!url) {
output.innerText = "⚠️ Select a dataset first.";
return;
}
output.innerText = `⏳ Generating summary...`;
try {
const response = await fetch(`${BACKEND_URL}/summarize_only`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ url })
});
const data = await response.json();
output.innerText = data.summary;
} catch (err) {
console.error(err);
output.innerText = "❌ Something went wrong.";
}
}
function downloadCSV() {
if (!combinedCSV) return;
const blob = new Blob([combinedCSV], { type: "text/csv" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "merged_dataset.csv";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}