mirror of
https://github.com/PlatypusPus/MushroomEmpire.git
synced 2026-02-07 22:18:59 +00:00
feat:Lib Files
This commit is contained in:
125
frontend/lib/api.ts
Normal file
125
frontend/lib/api.ts
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
/**
|
||||||
|
* API Client for Nordic Privacy AI Backend
|
||||||
|
* Base URL: http://localhost:8000
|
||||||
|
*/
|
||||||
|
|
||||||
|
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
|
||||||
|
|
||||||
|
export interface AnalyzeResponse {
|
||||||
|
status: string;
|
||||||
|
filename: string;
|
||||||
|
dataset_info: {
|
||||||
|
rows: number;
|
||||||
|
columns: number;
|
||||||
|
features: string[];
|
||||||
|
};
|
||||||
|
model_performance: {
|
||||||
|
accuracy: number;
|
||||||
|
precision: number;
|
||||||
|
recall: number;
|
||||||
|
f1_score: number;
|
||||||
|
};
|
||||||
|
bias_metrics: {
|
||||||
|
overall_bias_score: number;
|
||||||
|
disparate_impact: Record<string, any>;
|
||||||
|
statistical_parity: Record<string, any>;
|
||||||
|
violations_detected: any[];
|
||||||
|
};
|
||||||
|
risk_assessment: {
|
||||||
|
overall_risk_score: number;
|
||||||
|
privacy_risks: any[];
|
||||||
|
ethical_risks: any[];
|
||||||
|
compliance_risks: any[];
|
||||||
|
data_quality_risks: any[];
|
||||||
|
};
|
||||||
|
recommendations: string[];
|
||||||
|
report_file: string;
|
||||||
|
timestamp: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CleanResponse {
|
||||||
|
status: string;
|
||||||
|
filename: string;
|
||||||
|
dataset_info: {
|
||||||
|
original_rows: number;
|
||||||
|
original_columns: number;
|
||||||
|
cleaned_rows: number;
|
||||||
|
cleaned_columns: number;
|
||||||
|
};
|
||||||
|
gpu_acceleration: {
|
||||||
|
enabled: boolean;
|
||||||
|
device: string;
|
||||||
|
};
|
||||||
|
summary: {
|
||||||
|
columns_removed: string[];
|
||||||
|
columns_anonymized: string[];
|
||||||
|
total_cells_affected: number;
|
||||||
|
};
|
||||||
|
pii_detections: Record<string, {
|
||||||
|
action: string;
|
||||||
|
entity_types: string[];
|
||||||
|
num_affected_rows: number;
|
||||||
|
examples: Array<{ before: string; after: string }>;
|
||||||
|
}>;
|
||||||
|
gdpr_compliance: string[];
|
||||||
|
files: {
|
||||||
|
cleaned_csv: string;
|
||||||
|
audit_report: string;
|
||||||
|
};
|
||||||
|
timestamp: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Analyze dataset for bias and risk
|
||||||
|
*/
|
||||||
|
export async function analyzeDataset(file: File): Promise<AnalyzeResponse> {
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('file', file);
|
||||||
|
|
||||||
|
const response = await fetch(`${API_BASE_URL}/api/analyze`, {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json();
|
||||||
|
throw new Error(error.detail || 'Analysis failed');
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean dataset - detect and anonymize PII
|
||||||
|
*/
|
||||||
|
export async function cleanDataset(file: File): Promise<CleanResponse> {
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('file', file);
|
||||||
|
|
||||||
|
const response = await fetch(`${API_BASE_URL}/api/clean`, {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json();
|
||||||
|
throw new Error(error.detail || 'Cleaning failed');
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Download report file
|
||||||
|
*/
|
||||||
|
export function getReportUrl(reportPath: string): string {
|
||||||
|
return `${API_BASE_URL}${reportPath}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Health check
|
||||||
|
*/
|
||||||
|
export async function healthCheck() {
|
||||||
|
const response = await fetch(`${API_BASE_URL}/health`);
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
73
frontend/lib/indexeddb.ts
Normal file
73
frontend/lib/indexeddb.ts
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
/**
|
||||||
|
* IndexedDB utilities for caching uploaded files
|
||||||
|
*/
|
||||||
|
|
||||||
|
const DB_NAME = 'NordicPrivacyAI';
|
||||||
|
const STORE_NAME = 'uploads';
|
||||||
|
const DB_VERSION = 1;
|
||||||
|
|
||||||
|
function openDB(): Promise<IDBDatabase> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const request = indexedDB.open(DB_NAME, DB_VERSION);
|
||||||
|
|
||||||
|
request.onerror = () => reject(request.error);
|
||||||
|
request.onsuccess = () => resolve(request.result);
|
||||||
|
|
||||||
|
request.onupgradeneeded = (event) => {
|
||||||
|
const db = (event.target as IDBOpenDBRequest).result;
|
||||||
|
if (!db.objectStoreNames.contains(STORE_NAME)) {
|
||||||
|
db.createObjectStore(STORE_NAME);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UploadedFileMeta {
|
||||||
|
name: string;
|
||||||
|
size: number;
|
||||||
|
type: string;
|
||||||
|
contentPreview: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function saveLatestUpload(file: File, meta: UploadedFileMeta): Promise<void> {
|
||||||
|
const db = await openDB();
|
||||||
|
const transaction = db.transaction([STORE_NAME], 'readwrite');
|
||||||
|
const store = transaction.objectStore(STORE_NAME);
|
||||||
|
|
||||||
|
await new Promise((resolve, reject) => {
|
||||||
|
const request = store.put({ file, meta }, 'latest');
|
||||||
|
request.onsuccess = () => resolve(undefined);
|
||||||
|
request.onerror = () => reject(request.error);
|
||||||
|
});
|
||||||
|
|
||||||
|
db.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getLatestUpload(): Promise<{ file: File; meta: UploadedFileMeta } | { file: null; meta: null }> {
|
||||||
|
const db = await openDB();
|
||||||
|
const transaction = db.transaction([STORE_NAME], 'readonly');
|
||||||
|
const store = transaction.objectStore(STORE_NAME);
|
||||||
|
|
||||||
|
const result = await new Promise<any>((resolve, reject) => {
|
||||||
|
const request = store.get('latest');
|
||||||
|
request.onsuccess = () => resolve(request.result);
|
||||||
|
request.onerror = () => reject(request.error);
|
||||||
|
});
|
||||||
|
|
||||||
|
db.close();
|
||||||
|
return result || { file: null, meta: null };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteLatestUpload(): Promise<void> {
|
||||||
|
const db = await openDB();
|
||||||
|
const transaction = db.transaction([STORE_NAME], 'readwrite');
|
||||||
|
const store = transaction.objectStore(STORE_NAME);
|
||||||
|
|
||||||
|
await new Promise((resolve, reject) => {
|
||||||
|
const request = store.delete('latest');
|
||||||
|
request.onsuccess = () => resolve(undefined);
|
||||||
|
request.onerror = () => reject(request.error);
|
||||||
|
});
|
||||||
|
|
||||||
|
db.close();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user