Spaces:
Running
Running
File size: 17,003 Bytes
56bf851 826a975 56bf851 826a975 56bf851 826a975 56bf851 826a975 56bf851 826a975 56bf851 826a975 56bf851 826a975 56bf851 |
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
import React, { useState } from "react";
import { ApiService } from "../utils/apiService";
import { debugLog } from "../utils/config";
import DataViewer from "./DataViewer";
const Step4 = ({
apiKey,
s3Link,
config: generationConfig,
fileMetadata,
generatedDataLink,
setGeneratedDataLink,
stepNumber,
stepTitle,
stepIcon,
enabled = true,
}) => {
const [isGenerating, setIsGenerating] = useState(false);
const [generationStatus, setGenerationStatus] = useState("");
const [generationProgress, setGenerationProgress] = useState(0);
const [hasError, setHasError] = useState(false);
const [errorMessage, setErrorMessage] = useState("");
const generateSyntheticData = async () => {
// Prevent action if step is disabled
if (!enabled) {
debugLog("Generation attempted but step is disabled");
return;
}
setIsGenerating(true);
setGenerationStatus("Initializing generation...");
setGenerationProgress(0);
setHasError(false);
setErrorMessage("");
try {
debugLog("Starting synthetic data generation", {
s3Link,
config: generationConfig,
fileMetadata,
apiKeyPrefix: apiKey.substring(0, 8) + "...",
});
// Use the ApiService with retry logic
const result = await ApiService.retryRequest(async () => {
// Update progress during API call
setGenerationStatus("Sending request to AI model...");
setGenerationProgress(10);
return await ApiService.generateSyntheticData(apiKey, s3Link, {
...generationConfig,
fileSizeBytes: fileMetadata?.fileSizeBytes || 0,
sourceFileRows: fileMetadata?.sourceFileRows || 0,
});
});
// If the API returns progress updates, handle them
if (result.jobId) {
// For now, simulate progress since polling is not implemented
await simulateProgress();
// In a real implementation, you would poll for job status here
setGeneratedDataLink(
result.data?.fileUrl ||
result.fileUrl ||
result.download_link ||
result.link ||
result.s3_url
);
} else {
// Simulate progress for immediate results
await simulateProgress();
// Handle the specific response format: { "status": "success", "data": { "fileUrl": "..." } }
const dataLink =
result.data?.fileUrl ||
result.fileUrl ||
result.s3_url ||
result.download_link ||
result.link;
if (!dataLink) {
throw new Error(
"No download link received from the API. The generation may have failed on the server side."
);
}
setGeneratedDataLink(dataLink);
}
setGenerationStatus("Generation completed successfully!");
setGenerationProgress(100);
debugLog("Synthetic data generation completed", {
downloadLink:
result.data?.fileUrl ||
result.fileUrl ||
result.s3_url ||
result.download_link ||
result.link,
status: result.status,
message: result.message,
fullResult: result,
});
} catch (error) {
debugLog("Synthetic data generation failed", error);
setHasError(true);
setGenerationProgress(0);
// Create a user-friendly error message
let friendlyErrorMessage = "An error occurred during generation.";
if (error.message.includes("403") || error.message.includes("401")) {
friendlyErrorMessage =
"Authentication failed. Please check your API key and try again.";
} else if (error.message.includes("404")) {
friendlyErrorMessage =
"Generation service not found. Please try again later.";
} else if (error.message.includes("500")) {
friendlyErrorMessage =
"Server error occurred. The service may be temporarily unavailable.";
} else if (
error.message.includes("timeout") ||
error.message.includes("TimeoutError")
) {
friendlyErrorMessage =
"Request timed out. The generation process may take longer than expected. Please try again.";
} else if (
error.message.includes("Network") ||
error.message.includes("fetch")
) {
friendlyErrorMessage =
"Network error. Please check your internet connection and try again.";
} else if (error.message.includes("No download link")) {
friendlyErrorMessage =
"Generation completed but no download link was provided. Please try generating again.";
} else if (error.message) {
friendlyErrorMessage = error.message;
}
setErrorMessage(friendlyErrorMessage);
setGenerationStatus(`β Generation failed: ${friendlyErrorMessage}`);
} finally {
setIsGenerating(false);
}
};
const simulateProgress = async () => {
const steps = [
{ progress: 25, message: "Analyzing data structure..." },
{ progress: 50, message: "Training AI model..." },
{ progress: 75, message: "Generating synthetic data..." },
{ progress: 90, message: "Finalizing output..." },
];
for (const step of steps) {
setGenerationProgress(step.progress);
setGenerationStatus(step.message);
await new Promise((resolve) => setTimeout(resolve, 1500));
}
};
const handleDownload = () => {
if (generatedDataLink) {
debugLog("Downloading generated data", { link: generatedDataLink });
// Open in new tab so user can see if download works
const newWindow = window.open(generatedDataLink, "_blank");
// If popup blocked, provide fallback
if (!newWindow) {
// Fallback: create a temporary download link
const link = document.createElement("a");
link.href = generatedDataLink;
link.download = `synthetic_data_${Date.now()}.csv`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
};
const handleCopyLink = async () => {
if (generatedDataLink) {
try {
await navigator.clipboard.writeText(generatedDataLink);
// You could add a toast notification here
debugLog("Link copied to clipboard");
} catch (error) {
debugLog("Failed to copy link:", error);
// Fallback for older browsers
const textArea = document.createElement("textarea");
textArea.value = generatedDataLink;
document.body.appendChild(textArea);
textArea.select();
document.execCommand("copy");
document.body.removeChild(textArea);
}
}
};
const isReadyForGeneration =
apiKey &&
s3Link &&
generationConfig.targetColumn &&
generationConfig.numRows;
return (
<div className="step-container fade-in">
<div className="step-header">
<h2>
<span className="step-number">{stepNumber}</span>
{stepIcon} {stepTitle}
</h2>
<p>Generate high-quality synthetic data based on your configuration</p>
</div>
<div className="step-body">
{!generatedDataLink && !isGenerating && !hasError && (
<div className="generate-section">
<div
className={`status-message ${
isReadyForGeneration ? "info" : "warning"
}`}
>
<div className="status-message-icon">
{isReadyForGeneration ? "β
" : "β οΈ"}
</div>
<div className="status-message-content">
<h4>
{isReadyForGeneration
? "Ready for Generation"
: "Configuration Required"}
</h4>
{!isReadyForGeneration && (
<p
style={{
marginTop: "0.5rem",
fontSize: "0.875rem",
opacity: 0.9,
}}
>
Please complete all previous steps before generating data.
</p>
)}
<div className="status-summary">
<div className="status-row">
<span className="status-label">π API Key:</span>
<span
className={`status-badge ${
apiKey ? "success" : "warning"
}`}
>
{apiKey ? "β Valid" : "β Required"}
</span>
</div>
<div className="status-row">
<span className="status-label">π Data:</span>
<span
className={`status-badge ${
s3Link ? "success" : "warning"
}`}
>
{s3Link ? "β Uploaded" : "β Required"}
</span>
</div>
<div className="status-row">
<span className="status-label">π― Target:</span>
<span
className={`status-badge ${
generationConfig.targetColumn ? "success" : "warning"
}`}
>
{generationConfig.targetColumn
? `β ${generationConfig.targetColumn}`
: "β Not set"}
</span>
</div>
<div className="status-row">
<span className="status-label">π₯ Source:</span>
<span className="status-badge info">
{fileMetadata?.sourceFileRows || 0} rows
</span>
</div>
<div className="status-row">
<span className="status-label">π€ Generate:</span>
<span className="status-badge info">
{generationConfig.numRows} rows
</span>
</div>
</div>
</div>
</div>
<button
className="btn btn-primary generate-btn"
onClick={generateSyntheticData}
disabled={!isReadyForGeneration || !enabled}
style={{ marginTop: "1.5rem" }}
>
π― Generate Synthetic Data
</button>
</div>
)}
{isGenerating && (
<div className="generation-progress">
<div className="spinner"></div>
<div className="file-upload-text" style={{ marginTop: "1rem" }}>
{generationStatus}
</div>
<div className="progress-bar">
<div
className="progress-fill"
style={{ width: `${generationProgress}%` }}
></div>
</div>
<div
className="file-upload-subtext"
style={{ marginTop: "0.75rem" }}
>
{generationProgress}% Complete β’ This may take a few minutes
</div>
</div>
)}
{hasError && !isGenerating && (
<div className="error-section">
<div className="status-message error">
<div className="status-message-icon">β</div>
<div className="status-message-content">
<h4>Generation Failed</h4>
<p>There was an error generating your synthetic data.</p>
<div className="error-details">
<strong>Error:</strong> {errorMessage}
</div>
<div
className="error-help"
style={{ marginTop: "0.75rem", fontSize: "0.875rem" }}
>
<strong>What you can try:</strong>
<ul
style={{
marginTop: "0.5rem",
paddingLeft: "1.5rem",
textAlign: "left",
}}
>
<li>Check your internet connection and try again</li>
<li>
Verify your API key is valid and has sufficient credits
</li>
<li>Try reducing the number of rows to generate</li>
<li>Contact support if the problem persists</li>
</ul>
</div>
</div>
</div>
<div style={{ marginTop: "1.5rem", textAlign: "center" }}>
<button
className="btn btn-primary"
onClick={() => {
setHasError(false);
setErrorMessage("");
setGenerationStatus("");
setGenerationProgress(0);
}}
style={{ marginRight: "1rem" }}
>
π Try Again
</button>
<button
className="btn btn-secondary"
onClick={() => {
// Reset to initial state
setHasError(false);
setErrorMessage("");
setGenerationStatus("");
setGenerationProgress(0);
setGeneratedDataLink("");
}}
>
π Start Over
</button>
</div>
</div>
)}
{generatedDataLink && !isGenerating && (
<div className="results-section">
<div
className="status-message success"
style={{ marginBottom: "1.5rem" }}
>
<div className="status-message-icon">π</div>
<div className="status-message-content">
<h4>Generation Complete!</h4>
<p style={{ margin: "0.5rem 0" }}>
Successfully generated {generationConfig.numRows} rows of
synthetic data targeting the{" "}
<strong>{generationConfig.targetColumn}</strong> column.
</p>
</div>
</div>
<div
className="data-preview-section"
style={{ marginTop: "1.5rem" }}
>
<div
className="preview-header"
style={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
marginBottom: "1rem",
flexWrap: "wrap",
gap: "1rem",
}}
>
<h4 style={{ margin: 0, color: "var(--text-primary)" }}>
π Data Preview
</h4>
<div
className="action-buttons"
style={{
display: "flex",
gap: "0.75rem",
flexWrap: "wrap",
}}
>
<button
className="btn btn-success"
onClick={handleDownload}
title="Download the generated synthetic data file"
>
π₯ Download
</button>
<button
className="btn btn-outline"
onClick={handleCopyLink}
title="Copy download link to clipboard"
>
π Copy Link
</button>
<button
className="btn btn-secondary"
onClick={() => {
setGeneratedDataLink("");
setGenerationProgress(0);
setGenerationStatus("");
setHasError(false);
setErrorMessage("");
}}
title="Generate new synthetic data with different parameters"
>
π New Generation
</button>
</div>
</div>
<DataViewer s3Url={generatedDataLink} showPreviewOnly={true} />
<div
className="preview-info"
style={{
marginTop: "1rem",
padding: "0.75rem",
background: "var(--bg-tertiary)",
borderRadius: "8px",
fontSize: "0.875rem",
color: "var(--text-secondary)",
textAlign: "center",
}}
>
π‘ Showing preview of first few rows. Download the complete file
({generationConfig.numRows} rows) using the button above.
</div>
</div>
</div>
)}
</div>
</div>
);
};
export default Step4;
|